本文主要是介绍一行代码使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变成打字机模式或更改字体。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!