網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言
Android
提供一些Span設(shè)置文本樣式外,開(kāi)發(fā)者若希望實(shí)現(xiàn)一些新特性也是能自定義開(kāi)發(fā)實(shí)現(xiàn)的。只要了解原生Android
底層是如何實(shí)現(xiàn)特殊樣式,在其基礎(chǔ)之上做一些創(chuàng)新就能夠把文本內(nèi)容玩出花來(lái)。
PS:本次先不探究TextView是如何解析加載底層實(shí)現(xiàn)文本樣式繪制,只介紹CharacterStyle類(lèi)的實(shí)現(xiàn)。
ForegroundColorSpan解析
這里以ForegroundColorSpan
文本顏色樣式Span
舉例,其繼承CharacterStyle
也就是字符樣式頂層抽象類(lèi)以及其樣式同樣也是繼承自它。
public class ForegroundColorSpan extends CharacterStyle implements UpdateAppearance, ParcelableSpan { private final int mColor; public ForegroundColorSpan(@ColorInt int color) { mColor = color; } ....... 忽略無(wú)關(guān)代碼 @ColorInt public int getForegroundColor() { return mColor; } /// 重要函數(shù) @Override public void updateDrawState(@NonNull TextPaint textPaint) { textPaint.setColor(mColor); } }
從ForegroundColorSpan
類(lèi)源碼中可知文本顏色由入?yún)?code>int color決定,同時(shí)文本的繪制顏色更新是由textPaint
設(shè)置了入?yún)?code>color實(shí)現(xiàn)。從而得知文本樣式繪制主要由textPaint
實(shí)現(xiàn),textPaint
又是繼承自Paint
。只要對(duì)textPaint
做一些改變和參數(shù)設(shè)置也能實(shí)現(xiàn)自定義功能能力了。
文本顏色動(dòng)畫(huà)漸變樣式實(shí)現(xiàn)
如圖所示實(shí)現(xiàn)文本顏色漸變效果,只需要自定義ForegroundColorSpan
在其基礎(chǔ)之上增加顏色更新變化即可實(shí)現(xiàn)如上效果。已知關(guān)鍵點(diǎn)是入?yún)?code>int color實(shí)時(shí)更新,因此只要修改顏色值并且在updateDrawState
函數(shù)上修改顏色值滿(mǎn)足以上兩點(diǎn)就能夠達(dá)到期望效果。
class MutableForegroundColorSpan : ForegroundColorSpan { // 動(dòng)畫(huà)漸變值預(yù)設(shè) companion object { val MUTABLE_FOREGROUND_COLOR_SPAN_FC_PROPERTY: Property<MutableForegroundColorSpan, Int> = object : Property<MutableForegroundColorSpan, Int>( Int::class.java, "MUTABLE_FOREGROUND_COLOR_SPAN_FC_PROPERTY" ) { override operator fun set(span: MutableForegroundColorSpan, value: Int) { span.foregroundColor = value } override operator fun get(span: MutableForegroundColorSpan): Int { return span.foregroundColor } } } private var mAlpha = 255 private var mForegroundColor: Int constructor(alpha: Int, color: Int) : super(color) { mAlpha = alpha mForegroundColor = color } // 原構(gòu)造函數(shù)繼承 constructor(src: Parcel): super(src) { mForegroundColor = src.readInt() mAlpha = src.readInt() } override fun writeToParcel(dest: Parcel, flags: Int) { super.writeToParcel(dest, flags) dest.writeInt(mForegroundColor) dest.writeInt(mAlpha) } // 動(dòng)畫(huà)設(shè)置 fun animationColorChange(startColor: Int,endColor:Int) : ObjectAnimator{ val objectAnimator: ObjectAnimator = ObjectAnimator.ofInt( this, MutableForegroundColorSpan.MUTABLE_FOREGROUND_COLOR_SPAN_FC_PROPERTY, startColor, endColor ) objectAnimator.setEvaluator(ArgbEvaluator()) objectAnimator.duration = 3000 return objectAnimator } // 透明度 fun setAlpha(alpha: Int) { mAlpha = alpha } fun setForegroundColor(foregroundColor: Int) { mForegroundColor = foregroundColor } // 更新畫(huà)筆顏色 override fun updateDrawState(ds: TextPaint) { ds.color = foregroundColor } // 獲取文本顏色 override fun getForegroundColor(): Int { return Color.argb( mAlpha, Color.red(mForegroundColor), Color.green(mForegroundColor), Color.blue(mForegroundColor) ) } } // 樣式設(shè)置 監(jiān)聽(tīng)動(dòng)畫(huà)回調(diào)重新設(shè)置樣式從而刷新文本 private var mutableForegroundColorSpan = MutableForegroundColorSpan(255, Color.BLACK) mutableForegroundColorSpan.animationColorChange( Color.BLACK, Color.RED ).run { addUpdateListener { mutableForegroundColorView.text = animationColor() } start() } // 文本樣式配置 private fun animationColor(): SpannableStringBuilder { var spannableString = SpannableStringBuilder("") spannableString.also { span -> span.append(SpannableString("xxxxMutableForegroundColorSpanyyyy").also { it.setSpan( mutableForegroundColorSpan, 4, "MutableForegroundColorSpan".length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ) }) } return spannableString }
以上是實(shí)現(xiàn)文本漸變效果代碼,增加實(shí)時(shí)刷新文本顏色之后就能夠讓文本顏色變化呈現(xiàn)動(dòng)畫(huà)效果。其也能拓展功能例如從透明逐步可見(jiàn)也是另外一種特別效果。此外內(nèi)置動(dòng)畫(huà)效果同樣也能以動(dòng)畫(huà)類(lèi)的能力來(lái)設(shè)置循環(huán)次數(shù)等其他動(dòng)畫(huà)應(yīng)有的功能。
小結(jié)
改造ForegroundColorSpan
類(lèi)外結(jié)合動(dòng)畫(huà)能力還能為文本樣式創(chuàng)作做更多好玩效果。在此不一一例舉開(kāi)發(fā)者可以根據(jù)業(yè)務(wù)需要和想象力自行設(shè)置實(shí)現(xiàn)更有趣的內(nèi)容效果。
原文鏈接:https://juejin.cn/post/7108738457412829198
相關(guān)推薦
- 2023-10-11 MP、MybatisPlus、聯(lián)表查詢(xún)、自定義sql、Constants.WRAPPER、ew (一
- 2022-10-29 前端rem適配如何具體去使用
- 2023-05-16 移動(dòng)端開(kāi)發(fā)之Jetpack?Hilt技術(shù)實(shí)現(xiàn)解耦_Android
- 2022-12-02 C語(yǔ)言鏈表案例學(xué)習(xí)之通訊錄的實(shí)現(xiàn)_C 語(yǔ)言
- 2022-09-12 nginx訪(fǎng)問(wèn)報(bào)403錯(cuò)誤的幾種情況詳解_nginx
- 2023-07-10 匿名內(nèi)部類(lèi)、Lambda表達(dá)式、方法引用對(duì)比分析
- 2022-11-10 使用python的pandas讀取excel文件中的數(shù)據(jù)詳情_(kāi)python
- 2022-10-16 python列表倒序的幾種方法(切片、reverse()、reversed())_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支