一行代码使TextView变成打字机模式或更改字体。
来源:互联网 发布:中国网络日报T大漠鱼T 编辑:程序博客网 时间:2024/05/21 15:04
一行代码使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.TypeWriteTextView android: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>() { @Override public String apply(Long aLong) throws Exception { return mTextStr.substring(0, mIndex); } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<String>() { @Override public void accept(String str) throws Exception { mIndex++; setText(str); } }); } }}
- github欢迎start 支持: https://github.com/KomoriWu/TypeWriter.git
阅读全文
0 0
- 一行代码使TextView变成打字机模式或更改字体。
- 更改TextView字体颜色
- 在代码中更改TextView中字体的颜色
- FTP上传代码变成一行
- TextView更改为多样的字体
- TextView打字机效果
- TextView java 代码字体加粗
- 代码修改TextView字体颜色
- WinEdt 6.0更改字体或字号技巧
- textView出现最后一行字体缺少一部分的情况解决办法
- Android TextView部分字体变色或字体变大小
- Android代码中更改TextView颜色
- android 更改TextView部分字体颜色、大小、背景
- Android TextView 丰富多彩的字体样式代码
- Android TextView丰富多彩的字体样式代码
- Android TextView丰富多彩的字体样式代码
- Android TextView丰富多彩的字体样式代码
- 代码控制textview字体 单位和大小
- [Linux][RK3399] 以太网调试 — 利用 ping 和 pathping 工具
- 关于C#中在key不固定且不明的情况下遍历objec并取其值的方法
- php 调用现成类实现163邮箱邮件发送
- 全面升级Android面试之组件化与插件化
- ubuntu下搭建appium+robotframework自动化测试环境
- 一行代码使TextView变成打字机模式或更改字体。
- 安装测试环境
- beanfactory 和 applicationContext
- 神经网络知识点汇总——CNN
- 美团前端二面+三面+HR
- ${pageContext.request.contextPath} JSP取得绝对路径
- mysql match against 全文搜索
- 【Android】sweetDialog 看着都是甜的
- FZU2275KMP(字符串匹配)