Android 打印机一样显示TextView

来源:互联网 发布:js左右滑动特效 编辑:程序博客网 时间:2024/04/29 08:45

这里写图片描述

package com.example.ylwang.myapplication;import android.animation.ValueAnimator;import android.content.Context;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.animation.LinearInterpolator;import android.widget.TextView;/** * Created by ylWang on 2017/6/17. * <p> * AppendTextView appendTextView = (AppendTextView) findViewById(R.id.tv_fade); * appendTextView * .setTextString("可以像打印机一样动态显示的TextView...") * .startFadeInAnimation() * .setTextAnimationListener(new AppendTextView.TextAnimationListener() { * * @Override public void animationFinish() { * //TODO:打印完成之后的逻辑 * } * }); */public class AppendTextView extends TextView {    private StringBuffer stringBuffer = new StringBuffer();    private String[] arr;    private int textCount;    private int currentIndex = -1;    //每个字出现的时间    private static final int DURATION = 300;    private ValueAnimator textAnimation;    //自定义接口用于监听动画完成    private TextAnimationListener textAnimationListener;    public AppendTextView setTextAnimationListener(TextAnimationListener textAnimationListener) {        this.textAnimationListener = textAnimationListener;        return this;    }    public AppendTextView(Context context) {        this(context, null);    }    public AppendTextView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);    }    /**     * 文字逐个显示动画  通过插值的方式改变数据源     */    private void initAnimation() {        //从0到textCount - 1  是设置从第一个字到最后一个字的变化因子        textAnimation = ValueAnimator.ofInt(0, textCount - 1);        //执行总时间就是每个字的时间乘以字数        textAnimation.setDuration(textCount * DURATION);        //匀速显示文字        textAnimation.setInterpolator(new LinearInterpolator());        textAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator valueAnimator) {                int index = (int) valueAnimator.getAnimatedValue();                //过滤去重,保证每个字只重绘一次                if (currentIndex != index) {                    stringBuffer.append(arr[index]);                    currentIndex = index;                    //所有文字都显示完成之后进度回调结束动画                    if (currentIndex == (textCount - 1)) {                        if (textAnimationListener != null) {                            textAnimationListener.animationFinish();                        }                    }                    setText(stringBuffer.toString());                }            }        });    }    /**     * 设置逐渐显示的字符串     *     * @param textString     * @return     */    public AppendTextView setTextString(String textString) {        if (textString != null) {            //总字数            textCount = textString.length();            //存放单个字的数组            arr = new String[textCount];            for (int i = 0; i < textCount; i++) {                arr[i] = textString.substring(i, i + 1);            }            initAnimation();        }        return this;    }    /**     * 开启动画     *     * @return     */    public AppendTextView startFadeInAnimation() {        if (textAnimation != null) {            stringBuffer.setLength(0);            currentIndex = -1;            textAnimation.start();        }        return this;    }    /**     * 停止动画     *     * @return     */    public AppendTextView stopFadeInAnimation() {        if (textAnimation != null) {            textAnimation.end();        }        return this;    }    /**     * 回调接口     */    public interface TextAnimationListener {        void animationFinish();    }}
原创粉丝点击