日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android自定義Span實現文字漸變效果_Android

作者:JulyYu ? 更新時間: 2022-08-05 編程語言

前言

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

欄目分類
最近更新