網站首頁 編程語言 正文
前言
Android
提供一些Span設置文本樣式外,開發者若希望實現一些新特性也是能自定義開發實現的。只要了解原生Android
底層是如何實現特殊樣式,在其基礎之上做一些創新就能夠把文本內容玩出花來。
PS:本次先不探究TextView是如何解析加載底層實現文本樣式繪制,只介紹CharacterStyle類的實現。
ForegroundColorSpan解析
這里以ForegroundColorSpan
文本顏色樣式Span
舉例,其繼承CharacterStyle
也就是字符樣式頂層抽象類以及其樣式同樣也是繼承自它。
public class ForegroundColorSpan extends CharacterStyle implements UpdateAppearance, ParcelableSpan { private final int mColor; public ForegroundColorSpan(@ColorInt int color) { mColor = color; } ....... 忽略無關代碼 @ColorInt public int getForegroundColor() { return mColor; } /// 重要函數 @Override public void updateDrawState(@NonNull TextPaint textPaint) { textPaint.setColor(mColor); } }
從ForegroundColorSpan
類源碼中可知文本顏色由入參int color
決定,同時文本的繪制顏色更新是由textPaint
設置了入參color
實現。從而得知文本樣式繪制主要由textPaint
實現,textPaint
又是繼承自Paint
。只要對textPaint
做一些改變和參數設置也能實現自定義功能能力了。
文本顏色動畫漸變樣式實現
如圖所示實現文本顏色漸變效果,只需要自定義ForegroundColorSpan
在其基礎之上增加顏色更新變化即可實現如上效果。已知關鍵點是入參int color
實時更新,因此只要修改顏色值并且在updateDrawState
函數上修改顏色值滿足以上兩點就能夠達到期望效果。
class MutableForegroundColorSpan : ForegroundColorSpan { // 動畫漸變值預設 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 } // 原構造函數繼承 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) } // 動畫設置 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 } // 更新畫筆顏色 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) ) } } // 樣式設置 監聽動畫回調重新設置樣式從而刷新文本 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 }
以上是實現文本漸變效果代碼,增加實時刷新文本顏色之后就能夠讓文本顏色變化呈現動畫效果。其也能拓展功能例如從透明逐步可見也是另外一種特別效果。此外內置動畫效果同樣也能以動畫類的能力來設置循環次數等其他動畫應有的功能。
小結
改造ForegroundColorSpan
類外結合動畫能力還能為文本樣式創作做更多好玩效果。在此不一一例舉開發者可以根據業務需要和想象力自行設置實現更有趣的內容效果。
原文鏈接:https://juejin.cn/post/7108738457412829198
相關推薦
- 2023-02-02 C++示例講解觀察者設計模式_C 語言
- 2022-08-23 一文搞懂Python中函數的定義與使用_python
- 2022-05-10 電商后臺開發之商品規格組合算法
- 2022-09-15 C++關于/2和>>1的區別說明_C 語言
- 2022-09-12 python中的集合及集合常用的使用方法_python
- 2023-04-01 Python使用pptx實現復制頁面到其他PPT中_python
- 2022-04-21 Android圖表庫HelloChart繪制多折線圖_Android
- 2023-12-12 Mybatis中一些優化
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支