TextView实现打印机效果 ,字符串逐字显示
上述是自定义好的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实现打印机效果 ,字符串逐字显示的更多相关文章
- 《Python CookBook2》 第一章 文本 - 去字符串两端的空格 && 合并字符串 && 将字符串逐字符或者逐词反转
去字符串两端的空格 任务: 获得一个开头和末尾都没有多余空格的字符串. 解决方案: 字符串对象的lstrip.rstrip和strip 方法正是为这种任务而设计的.这几个方法都不需要参数,它们会直接返 ...
- element-ui select组件中复选时以字符串形式显示
我使用的element-ui的版本是1.4.13. 如上图所示,使用el-select组件,要实现可搜索.可复选.可创建条目时,展示样式是如上图所示,输入框的高度会撑开,影响页面布局,按照产品的需求, ...
- SpannableString实现TextView的链接效果
SpannableString实现TextView的链接效果 一.简介 TextView使用SpannableString设置复合文本TextView通常用来显示普通文本,但是有时候需要对其中某些文本 ...
- Android源码分析(十二)-----Android源码中如何自定义TextView实现滚动效果
一:如何自定义TextView实现滚动效果 继承TextView基类 重写构造方法 修改isFocused()方法,获取焦点. /* * Copyright (C) 2015 The Android ...
- jQuery 效果 —— 隐藏和显示
jQuery 效果 -- 隐藏和显示 1.隐藏和显示 (1)在jQuery中我们可以使用hide()和show()分别隐藏和显示HTML元素: //隐藏元素 $("button") ...
- TextView drawablePadding没有效果
1.当TextView 设置宽度设置为match_parent的时候 TextView drawablePadding没有效果 ,字设置了center位置,但是和左边的图片离开很远 2.当TextVi ...
- Function:html结构转字符串形式显示
//Html结构转字符串形式显示 支持<br>换行 function ToHtmlString(htmlStr) { return toTXT(htmlStr).replace(/\&am ...
- 查找常用字符(给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。)
给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表. 例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 ...
- android一个倾斜的TextView,适用于标签效果
描述: android一个倾斜的TextView,适用于标签效果 应用截图: 使用说明: <com.haozhang.lib.SlantedTextView android:layout_wid ...
随机推荐
- 【微信】微信小程序 新建页面目录后,怎么自动生成目中的的四个基本文件呢? 新建目录报错如下VM458:2 未找到 app.json 中的定义的 pages "pages/module/module" 对应的 WXML 文件
如下图,在使用微信开发者工具过程中,新创建了页面目录,想要页面文件夹中自动生成四个基本文件 但是新创建了一个页面文件夹,里面的四个基本文件并没有展示出来 然后在app.json添加这个路径,ctrl+ ...
- 部署步骤“回收 IIS 应用程序池”中出现错误: 无法将通信对象 System.ServiceModel.InstanceContext 用于通信,因为它已经被中止。
??? 重启iis应用程序池和网站都不管用,重启vs就好了,一脸懵逼. 有没有更好的方法?
- 设计模式之原型模式(php实现)
github地址:https://github.com/ZQCard/design_pattern1.先了解什么是浅拷贝与深拷贝 //深拷贝:赋值时值完全复制,完全的copy,对其中一个作出改变,不会 ...
- Struts2实现登录权限访问控制
目录: Ⅰ 条件 Ⅱ 目的 Ⅲ 分析 Ⅳ 实现 Ⅴ 具体代码实现 ------------------------------------------------------------------- ...
- 第一章 初识shiro
shiro学习教程来自开涛大神的博客:http://jinnianshilongnian.iteye.com/blog/2018936 第一章 初识shiro 简单了解shiro主要记住三张图即可. ...
- Hive图形化界面客户端
通过JDBC连接HiveServer2的图形界面工具,包括:SQuirrel SQL Client.Oracle SQL Developer以及DbVisualizer SQuirrel SQL Cl ...
- UVa 10192 - Vacation & UVa 10066 The Twin Towers ( LCS 最长公共子串)
链接:UVa 10192 题意:给定两个字符串.求最长公共子串的长度 思路:这个是最长公共子串的直接应用 #include<stdio.h> #include<string.h> ...
- PS如何用制作BMP 256位色非压缩图片,供Easyboot作为背景
可以先把图片转换为gif格式,然后用Windows自带的画图工具打开,并另存为BMP格式的图片. 但是这样制作完成的图片失真相当严重 再如下面,简直无法不堪入目. 也可以使用PS.准备好图片之后点 ...
- defer,panic,recover
Go语言不支持传统的 try…catch…finally 这种异常,因为Go语言的设计者们认为,将异常与控制结构混在一起会很容易使得代码变得混乱.因为开发者很容易滥用异常,甚至一个小小的错误都抛出一个 ...
- Laravel创建项目和安装PHPStorm IDE插件
一.win10下安装composer1.下载composer.phar,放入php的安装目录https://getcomposer.org/download/1.4.2/composer.phar 2 ...