Android语音输入打字效果渐变以及纠错效果

Android渐变纠错效果

package com.example.myapplication4;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class SkipWordActivityOne extends AppCompatActivity {

    TextView mAsrTextView1;
    TextView mAsrTextView2;
    Button startBtn;

    int count = 0;
    String last;
    String current;
    boolean isFinish;
    int duration = 150;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                if (count < strings1.length - 1) {
                    isFinish = false;
                    last = strings1[count];
                    current = strings1[count + 1];
                    mAsrTextView1.setText(last);
                    mAsrTextView2.setText(current);
                    startAnimation(1, 0, mAsrTextView1);
                    startAnimation(0, 1, mAsrTextView2);
                    count++;
                    if(count == strings1.length - 1){
                        isFinish = true;
                    }
                    mHandler.sendEmptyMessageDelayed(0, duration);
                } else {
                    isFinish = true;
                }

            }
        }
    };

    private String[] strings1 = new String[]{"甚至", "设置八点", "设置八点脑中", "设置八点闹钟"};
    private String[] strings2 = new String[]{"今天", "今天的", "今天的天气", "今天的天气,今天的天气"};
    private String[] strings3 = new String[]{"你好,我在听...", "打开设置", "打开音乐,打开音乐"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_skip_word_one);
        mAsrTextView1 = findViewById(R.id.asr_textView1);
        mAsrTextView2 = findViewById(R.id.asr_textView2);
        startBtn = findViewById(R.id.start_btn);
        mHandler.sendEmptyMessageDelayed(0, duration);
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count = 0;
                mAsrTextView1.setText("");
                mAsrTextView2.setText("");
                mHandler.sendEmptyMessageDelayed(0, duration);
            }
        });
    }

    private void startAnimation(int start, int end, View view) {
        AlphaAnimation alphaAnimation = new AlphaAnimation(start, end);
        alphaAnimation.setDuration(500);
        view.startAnimation(alphaAnimation);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (isFinish) {
                    mAsrTextView1.setText(current);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
}

你可能感兴趣的:(android)