TextView实现打印机效果 ,字符串逐字显示

https://github.com/lygttpod/AndroidCustomView/blob/master/app/src/main/java/com/allen/androidcustomview/widget/FadeInTextView.java

上述是自定义好的Textview,可以直接使用,具体如何实现,我们一起学习一下

  1 public class FadeInTextView extends TextView {
  2 
  3 
  4     private Rect textRect = new Rect();
  5 
  6     private StringBuffer stringBuffer = new StringBuffer();
  7 
  8     private String[] arr;
  9 
 10     private int textCount;
 11 
 12     private int currentIndex = -1;
 13 
 14     /**
 15      * 每个字出现的时间
 16      */
 17     private int duration = 300;
 18     private ValueAnimator textAnimation;
 19 
 20     private TextAnimationListener textAnimationListener;
 21 
 22 
 23     public FadeInTextView setTextAnimationListener(TextAnimationListener textAnimationListener) {
 24         this.textAnimationListener = textAnimationListener;
 25         return this;
 26     }
 27 
 28     public FadeInTextView(Context context) {
 29         this(context, null);
 30     }
 31 
 32     public FadeInTextView(Context context, @Nullable AttributeSet attrs) {
 33         super(context, attrs);
 34 
 35     }
 36 
 37     @Override
 38     protected void onDraw(final Canvas canvas) {
 39         super.onDraw(canvas);
 40         if (stringBuffer != null) {
 41             drawText(canvas, stringBuffer.toString());
 42         }
 43     }
 44 
 45     /**
 46      * 绘制文字
 47      *
 48      * @param canvas 画布
 49      */
 50     private void drawText(Canvas canvas, String textString) {
 51         textRect.left = getPaddingLeft();
 52         textRect.top = getPaddingTop();
 53         textRect.right = getWidth() - getPaddingRight();
 54         textRect.bottom = getHeight() - getPaddingBottom();
 55         Paint.FontMetricsInt fontMetrics = getPaint().getFontMetricsInt();
 56         int baseline = (textRect.bottom + textRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
 57         //文字绘制到整个布局的中心位置
 58         canvas.drawText(textString, getPaddingLeft(), baseline, getPaint());
 59     }
 60 
 61     /**
 62      * 文字逐个显示动画  通过插值的方式改变数据源
 63      */
 64     private void initAnimation() {
 65 
 66         //从0到textCount - 1  是设置从第一个字到最后一个字的变化因子
 67         textAnimation = ValueAnimator.ofInt(0, textCount - 1);
 68         //执行总时间就是每个字的时间乘以字数
 69         textAnimation.setDuration(textCount * duration);
 70         //匀速显示文字
 71         textAnimation.setInterpolator(new LinearInterpolator());
 72         textAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
 73             @Override
 74             public void onAnimationUpdate(ValueAnimator valueAnimator) {
 75                 int index = (int) valueAnimator.getAnimatedValue();
 76                 //过滤去重,保证每个字只重绘一次
 77                 if (currentIndex != index) {
 78                     stringBuffer.append(arr[index]);
 79                     currentIndex = index;
 80                     //所有文字都显示完成之后进度回调结束动画
 81                     if (currentIndex == (textCount - 1)) {
 82                         if (textAnimationListener != null) {
 83                             textAnimationListener.animationFinish();
 84                         }
 85                     }
 86 
 87                     invalidate();
 88                 }
 89             }
 90         });
 91     }
 92 
 93     /**
 94      * 设置逐渐显示的字符串
 95      *
 96      * @param textString
 97      * @return
 98      */
 99     public FadeInTextView setTextString(String textString) {
100         if (textString != null) {
101             //总字数
102             textCount = textString.length();
103             //存放单个字的数组
104             arr = new String[textCount];
105             for (int i = 0; i < textCount; i++) {
106                 arr[i] = textString.substring(i, i + 1);
107             }
108             initAnimation();
109         }
110 
111         return this;
112     }
113 
114     /**
115      * 开启动画
116      *
117      * @return
118      */
119     public FadeInTextView startFadeInAnimation() {
120         if (textAnimation != null) {
121             stringBuffer.setLength(0);
122             currentIndex = -1;
123             textAnimation.start();
124         }
125         return this;
126     }
127 
128     /**
129      * 停止动画
130      *
131      * @return
132      */
133     public FadeInTextView stopFadeInAnimation() {
134         if (textAnimation != null) {
135             textAnimation.end();
136         }
137         return this;
138     }
139 
140     /**
141      * 回调接口
142      */
143     public interface TextAnimationListener {
144         void animationFinish();
145     }
146 }
147 
148     Contact GitHub API Training Shop Blog About 
149 
150     © 2017 GitHub, Inc. Terms Privacy Security Status Help 

在此说明,俺们也是看大神写的代码学习学习,并非自己写的,在此是供大家学习!

原文地址:https://www.cnblogs.com/wangying222/p/7007334.html