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(); }}
阅读全文
1 0
- Android 打印机一样显示TextView
- textview和网页显示一样
- unity GUI 如打印机一样一字字显示一篇文章!
- Android EditText像TextView一样的样式
- android textview一行显示
- Android TextView显示图片
- Android文本显示:TextView
- android TextView 显示HTML
- android textview 竖排显示
- android竖排显示TextView
- TextView实现打印机效果
- android TextView setTextColor没有显示
- android TextView 中不同颜色显示
- Android TextView显示文章详情
- Android textview 多行显示
- Android textview显示多行
- android TextView setSingleLinexia显示问题
- Android TextView显示文字对齐
- 产生10个长度为10的不能重复的字符串(里面只能出现大写字母、小写字母、0-9的数字),并遍历打印输出
- 使用kaldi进行语音自动切分、模型训练和强制对齐
- Bootstrap 3.3.7学习笔记13
- List<String>不能赋值给List<Object>
- 配置Spring AOP面向切面编程
- Android 打印机一样显示TextView
- 身份证扫一扫识别软件api应用
- nyoj 412 Same binary weight
- Elasticsearch写入性能优化
- PyQt的导出
- Scikit-learn Preprocessing 预处理
- 大数据-十五
- 现在开始学习C语言
- PAT-A-1057. Stack (30)