一行代码使TextView变成打字机模式或更改字体。
一行代码使TextView变成打字机模式或更改字体。
扯蛋:玩过《开眼》APP的都知道,里面几乎所有的TextView都是类似于打字机模式。本文是采用RXjava2几行代码实现了打字机功能,外加自定义了字体
效果图
github欢迎start 支持: https://github.com/KomoriWu/TypeWriter.git
具有TextView本身所有的属性
- 自定义的属性有
<declare-styleable name="TypeWrite"> //设置打字机速度(毫秒不能为0)<attr name="setSpeed" format="integer" /> //是否设置字体<attr name="isEnableTtf" format="boolean" /> //是否开启打字机模式<attr name="isEnableTypeWrite" format="boolean" /></declare-styleable>
项目中引用:
Step 1. Add the JitPack repository to your build file
allprojects {repositories {maven { url 'https://jitpack.io' }}}
Step 2. Add the dependency
dependencies {compile 'com.github.KomoriWu:TypeWriter:1.0'}
Step 3.布局中引用
<com.komoriwu.typewriter.TypeWriteTextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hint"android:textColor="@color/colorAccent"android:textSize="20sp"custom:setSpeed="100"custom:isEnableTtf="true"custom:isEnableTypeWrite="true"/>
源码:
源码很简单,想自定义的,直接拷贝加以修改即可。
public class TypeWriteTextView extends TextView {public static final int UPDATE_DELAY = 10;private String mTextStr;private int mLength;private int mIndex;public TypeWriteTextView(Context context) {super(context);init(context, null, 0);}public TypeWriteTextView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);init(context, attrs, 0);}public TypeWriteTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs, defStyleAttr);}private void init(Context context, AttributeSet attrs, int defStyleAttr) {mTextStr = (String) getText();mLength = mTextStr.length();int speed = 100;boolean isEnableTypeWrite = true;boolean isEnableTtf = true;if (attrs != null) {TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TypeWrite, defStyleAttr, 0);int n = typedArray.getIndexCount();for (int i = 0; i < n; i++) {int attr = typedArray.getIndex(i);switch (attr) {case R.styleable.TypeWrite_setSpeed:speed = typedArray.getInteger(attr, 100);break;case R.styleable.TypeWrite_isEnableTypeWrite:isEnableTypeWrite = typedArray.getBoolean(attr, true);break;case R.styleable.TypeWrite_isEnableTtf:isEnableTtf = typedArray.getBoolean(attr, true);break;}}typedArray.recycle();}if (isEnableTtf) {String fontName = "Heavy.ttf";super.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/" + fontName), defStyleAttr);}if (isEnableTypeWrite) {Flowable.interval(UPDATE_DELAY, speed, TimeUnit.MILLISECONDS).take(mLength + 1).map(new Function<Long, String>() {@Overridepublic String apply(Long aLong) throws Exception {return mTextStr.substring(0, mIndex);}}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<String>() {@Overridepublic void accept(String str) throws Exception {mIndex++;setText(str);}});}}}
- github欢迎start 支持: https://github.com/KomoriWu/TypeWriter.git
一行代码使TextView变成打字机模式或更改字体。相关推荐
- 一行代码让网页变成暗黑模式
让自己的网站变成暗黑模式只需要一行简简单单的CSS代码就可以完成啦: html[theme='dark-mode'] {filter: invert(1) hue-rotate(180deg); } ...
- 一行代码解决IE6~IE8以及IE兼容模式下的兼容问题
之前写代码直接是 ! + Tab, 接手一个项目,在任何浏览器都没有问题,只有在 IE 的兼容模式下,页面全都乱套.对比了一下之前写的代码发现没有这么一行代码:<meta http-equiv= ...
- 一行代码实现底部导航栏TabLayout
欢迎关注公众号:JueCode app中底部导航栏已经是很常见的控件了,比如微信,简书,QQ等都有这类控件,都是点击底部标签切换界面.主要的实现手段有 RadioGroup FragmentTabLa ...
- 使TextView在Android上可滚动
我在textview中显示的文本似乎太长,无法容纳在一个屏幕中. 我需要使TextView可滚动. 我怎样才能做到这一点? 这是代码: final TextView tv = new TextView ...
- 一行代码解决ie浏览器的兼容
x-ua-compatible 头标签大小写不敏感,必须用在 head 中,必须在除 title 外的其他 meta 之前使用. 1.使用一行代码来指定浏览器使用特定的文档模式. <meta h ...
- 一行代码轻松搞定各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE给出了解决方案Google也给出了解决方案百度也应用了这种方案去解决IE的兼容问题? 百度源代码如下: <!Doctype html& ...
- 快速实现免费的个人免签收款功能(不写一行代码)
最近一直在研究使用冰狐智能辅助的"自动构建"功能在不编程的情况如何实现各种好玩的东东,由于自己利用业余时间开发的小工具需要用到收款功能,于是自然想到用"自动构建" ...
- 一行代码实现IOS 3DES加密解密
3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES加密算法.由于计 ...
- PyTorch 2.0 重磅发布:一行代码提速 30%
在今天的 PyTorch 2022 开发者大会上,PyTorch 团队发布了一个新特性`torch.compile`,这个新特性将 PyTorch 的性能推向了新高度,并开始将 PyTorch 的部分 ...
最新文章
- 一文了解四种软件架构:Serverless架构、微服务架构、分布式架构、单体架构
- 台哥原创:java 扫雷源码
- python读取文件_python这么受欢迎,你知道如何以正确的方式来读取文件内容吗
- TensorFlow中报错 module ‘tensorflow_core._api.v2.train‘ has no attribute ‘GradientDescentOptimize
- K8S_Google工作笔记0012---通过二进制方式_部署master组件
- 使用C语言进行面向对象的开发--GObject入门[4]
- 146.LRU缓存机制
- yaws mysql_MySQL入门之C语言操作MySQL
- java常用框架集合
- 【人类简史】从动物到上帝 [以色列-尤瓦尔 · 赫拉利](阅读笔记)
- linux ftp客户端 pasv 227,FTP连接时出现“227 Entering Passive Mode”的解决
- java外卖项目介绍_JavaWeb网上订餐系统项目
- 关于css3中的2d样式skew倾斜详解
- Error response from daemon: removal of container XXXXXis already in progress解决方法
- 一台电脑如何开俩虚拟机_虚拟机轻松实现一台电脑两人用
- 【寻找最佳小程序】04期 :探访“小打卡”产品打磨细节及线下场景真实应用...
- OpenWRT初次进入设置联网
- html5 js(Javascript-Barcode-Reader)实现上传图片或拍照识别条形码的功能
- 分布式数据库实战第五节 保证分布式系统中的数据库可靠
- 请求大佬帮忙解决一下vue脚本架安装有问题
热门文章
- iNFTnews丨在元宇宙中占据优势地位的5种营销策略
- P3817 小A的糖果
- 【Java编程学习】案例 5-5 二月天
- SourceInSight无法访问网络驱动器的解决方案
- CONVERT()函数
- 软件测试,谱尼为网络安全护航2.10
- java中private string_java 中private static final String string的介绍final的意思
- 北京内推 | 微软亚洲研究院机器学习组招聘说话人脸生成方向研究实习生
- MVC+Repository+UOW+EntityFrmeWork的使用
- cgb2107-day18