一行代码使TextView变成打字机模式或更改字体。

一行代码使TextView变成打字机模式或更改字体。

扯蛋:玩过《开眼》APP的都知道,里面几乎所有的TextView都是类似于打字机模式。本文是采用RXjava2几行代码实现了打字机功能,外加自定义了字体

<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);
                        }
                    });
        }

    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值