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

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

 public class FadeInTextView extends TextView {

     private Rect textRect = new Rect();

     private StringBuffer stringBuffer = new StringBuffer();

     private String[] arr;

     private int textCount;

     private int currentIndex = -1;

     /**
* 每个字出现的时间
*/
private int duration = 300;
private ValueAnimator textAnimation; private TextAnimationListener textAnimationListener; public FadeInTextView setTextAnimationListener(TextAnimationListener textAnimationListener) {
this.textAnimationListener = textAnimationListener;
return this;
} public FadeInTextView(Context context) {
this(context, null);
} public FadeInTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs); } @Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
if (stringBuffer != null) {
drawText(canvas, stringBuffer.toString());
}
} /**
* 绘制文字
*
* @param canvas 画布
*/
private void drawText(Canvas canvas, String textString) {
textRect.left = getPaddingLeft();
textRect.top = getPaddingTop();
textRect.right = getWidth() - getPaddingRight();
textRect.bottom = getHeight() - getPaddingBottom();
Paint.FontMetricsInt fontMetrics = getPaint().getFontMetricsInt();
int baseline = (textRect.bottom + textRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
//文字绘制到整个布局的中心位置
canvas.drawText(textString, getPaddingLeft(), baseline, getPaint());
} /**
* 文字逐个显示动画 通过插值的方式改变数据源
*/
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();
}
} invalidate();
}
}
});
} /**
* 设置逐渐显示的字符串
*
* @param textString
* @return
*/
public FadeInTextView 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 FadeInTextView startFadeInAnimation() {
if (textAnimation != null) {
stringBuffer.setLength(0);
currentIndex = -1;
textAnimation.start();
}
return this;
} /**
* 停止动画
*
* @return
*/
public FadeInTextView stopFadeInAnimation() {
if (textAnimation != null) {
textAnimation.end();
}
return this;
} /**
* 回调接口
*/
public interface TextAnimationListener {
void animationFinish();
}
} Contact GitHub API Training Shop Blog About © 2017 GitHub, Inc. Terms Privacy Security Status Help

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

TextView实现打印机效果 ,字符串逐字显示的更多相关文章

  1. 《Python CookBook2》 第一章 文本 - 去字符串两端的空格 && 合并字符串 && 将字符串逐字符或者逐词反转

    去字符串两端的空格 任务: 获得一个开头和末尾都没有多余空格的字符串. 解决方案: 字符串对象的lstrip.rstrip和strip 方法正是为这种任务而设计的.这几个方法都不需要参数,它们会直接返 ...

  2. element-ui select组件中复选时以字符串形式显示

    我使用的element-ui的版本是1.4.13. 如上图所示,使用el-select组件,要实现可搜索.可复选.可创建条目时,展示样式是如上图所示,输入框的高度会撑开,影响页面布局,按照产品的需求, ...

  3. SpannableString实现TextView的链接效果

    SpannableString实现TextView的链接效果 一.简介 TextView使用SpannableString设置复合文本TextView通常用来显示普通文本,但是有时候需要对其中某些文本 ...

  4. Android源码分析(十二)-----Android源码中如何自定义TextView实现滚动效果

    一:如何自定义TextView实现滚动效果 继承TextView基类 重写构造方法 修改isFocused()方法,获取焦点. /* * Copyright (C) 2015 The Android ...

  5. jQuery 效果 —— 隐藏和显示

    jQuery 效果 -- 隐藏和显示 1.隐藏和显示 (1)在jQuery中我们可以使用hide()和show()分别隐藏和显示HTML元素: //隐藏元素 $("button") ...

  6. TextView drawablePadding没有效果

    1.当TextView 设置宽度设置为match_parent的时候 TextView drawablePadding没有效果 ,字设置了center位置,但是和左边的图片离开很远 2.当TextVi ...

  7. Function:html结构转字符串形式显示

    //Html结构转字符串形式显示 支持<br>换行 function ToHtmlString(htmlStr) { return toTXT(htmlStr).replace(/\&am ...

  8. 查找常用字符(给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。)

    给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表. 例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 ...

  9. android一个倾斜的TextView,适用于标签效果

    描述: android一个倾斜的TextView,适用于标签效果 应用截图: 使用说明: <com.haozhang.lib.SlantedTextView android:layout_wid ...

随机推荐

  1. 解决413 Request Entity Too Large

    修改nginx配置    这是最简单的一个做法,着报错原因是nginx不允许上传配置过大的文件,那么件把nginx的上传大小配置调高就好. 1.打开nginx主配置文件nginx.conf,一般在/u ...

  2. Linux文件压缩与解压命令

    1  .zip 格式压缩与解压 压缩命令 zip 压缩文件名 源文件 zip  -r   压缩目录名       源目录 解压命令 unzip 文件名 td@td-Lenovo-IdeaPad-Y41 ...

  3. UVA 1665 Islands

    题意:输入一个n*m矩阵,每一个格子都有一个正整数,再输入T个整数ti,对于每一个ti,输出大于ti的正整数组成多少个四连快 思路:正着做的话事实上相当于删除连通块,而假设反着做的话就相当于变成添加连 ...

  4. http://www.360doc.com/content/14/0313/17/16070877_360315087.shtml

    http://www.360doc.com/content/14/0313/17/16070877_360315087.shtml

  5. Java 中 ConcurrentHashMap 原理分析

    一.Java并发基础 当一个对象或变量可以被多个线程共享的时候,就有可能使得程序的逻辑出现问题. 在一个对象中有一个变量i=0,有两个线程A,B都想对i加1,这个时候便有问题显现出来,关键就是对i加1 ...

  6. JWT笔记(2)

    上文对JWT模块进行了一个简单的分析.这篇文章稍微做出一些深入的了解. 一,Header篡改攻击 因为JWT的Header是强制有效并且是明文传输(Base64URL编码,几乎等同于明文).那么恶意用 ...

  7. java根据身份证号和获取用户年龄和性别的工具类

    import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util ...

  8. Excel 数据导入SQL XML 自动生成表头

    去出差的时候应客户要求要要将Excel 文件内的数据批量导入到数据库中,而且有各种不同种类的表格,如果每一个表格多对应一个数据表的话, 按照正常的方法应该是创建数据表,创建数据库中映射的数据模型,然后 ...

  9. 什么是ICE (Internet Communications Engine)

    http://user.qzone.qq.com/77811970 直接在google上搜索ICE,出来的结果并不多,所以很多人就认为ICE是个神秘的东西,其实,国内已经有很多大型应用在使用ICE了. ...

  10. Weblogic OutOfMemory exception的误解 -- thread limitation

    不是全部的OutofMemory exception都是内存问题... 前几天有个客户的site报了下面错误: [ERROR][thread ] Could not start thread Time ...