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

2023-12-21 05:08

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



http://www.chinasem.cn/article/518732

相关文章

使用Python和PaddleOCR实现图文识别的代码和步骤

《使用Python和PaddleOCR实现图文识别的代码和步骤》在当今数字化时代,图文识别技术的应用越来越广泛,如文档数字化、信息提取等,PaddleOCR是百度开源的一款强大的OCR工具包,它集成了... 目录一、引言二、环境准备2.1 安装 python2.2 安装 PaddlePaddle2.3 安装

一文带你搞懂Redis Stream的6种消息处理模式

《一文带你搞懂RedisStream的6种消息处理模式》Redis5.0版本引入的Stream数据类型,为Redis生态带来了强大而灵活的消息队列功能,本文将为大家详细介绍RedisStream的6... 目录1. 简单消费模式(Simple Consumption)基本概念核心命令实现示例使用场景优缺点2

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

CentOS7更改默认SSH端口与配置指南

《CentOS7更改默认SSH端口与配置指南》SSH是Linux服务器远程管理的核心工具,其默认监听端口为22,由于端口22众所周知,这也使得服务器容易受到自动化扫描和暴力破解攻击,本文将系统性地介绍... 目录引言为什么要更改 SSH 默认端口?步骤详解:如何更改 Centos 7 的 SSH 默认端口1

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指