使用Kotlin實(shí)現(xiàn)文字漸變TextView的代碼
實(shí)現(xiàn)效果:

實(shí)現(xiàn)代碼:
import android.content.Context
import android.graphics.*
import android.support.annotation.ColorInt
import android.support.annotation.ColorRes
import android.text.TextPaint
import android.util.AttributeSet
import android.widget.TextView
import com.ans.utilactivity.R
class GradientTextView @JvmOverloads constructor(
context: Context?,
attrs: AttributeSet? = null
) : TextView(context, attrs) {
private var mPaint: TextPaint? = null
private var mLinearGradient: LinearGradient? = null
private var mMeasureWidth = 0
private var mTextMatrix: Matrix? = null
@ColorInt
private var mStartColor: Int = 0xFF333333.toInt()
@ColorInt
private var mEndColor: Int = 0xFF333333.toInt()
init {
if (attrs != null) {
val attrArray = getContext().obtainStyledAttributes(attrs, R.styleable.GradientTextView)
mStartColor = attrArray.getColor(R.styleable.GradientTextView_startColor, mStartColor)
mEndColor = attrArray.getColor(R.styleable.GradientTextView_endColor, mEndColor)
}
}
/**
* 復(fù)寫onSizeChanged方法
*
*/
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
mMeasureWidth = measuredWidth
if (mMeasureWidth > 0) {
mPaint = paint
//(x0,y0):漸變起始點(diǎn)坐標(biāo)
//(x1,y1):漸變結(jié)束點(diǎn)坐標(biāo)
//color0:漸變開始點(diǎn)顏色,16進(jìn)制的顏色表示,必須要帶有透明度
//color1:漸變結(jié)束顏色
//colors:漸變數(shù)組
//positions:位置數(shù)組,position的取值范圍[0,1],作用是指定某個位置的顏色值,如果傳null,漸變就線性變化。
//tile:用于指定控件區(qū)域大于指定的漸變區(qū)域時,空白區(qū)域的顏色填充方法。
mLinearGradient = LinearGradient(
0f
, 0f
, mMeasureWidth.toFloat()
, 0f
, intArrayOf(mStartColor, mEndColor)
, null
, Shader.TileMode.CLAMP
)
mPaint?.shader = mLinearGradient
mTextMatrix = Matrix()
}
}
}
attr.xml 引用
<declare-styleable name="GradientTextView"> <attr name="startColor" format="color"/> <attr name="endColor" format="color"/> </declare-styleable>
引用:
<前綴.GradientTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:startColor="@color/colorPrimary"
app:endColor="@color/colorAccent"
/>
到此這篇關(guān)于使用Kotlin實(shí)現(xiàn)文字漸變TextView的文章就介紹到這了,更多相關(guān)Kotlin文字漸變TextView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android仿微信朋友圈實(shí)現(xiàn)滾動條下拉反彈效果
這篇文章主要為大家介紹了Android仿微信朋友圈實(shí)現(xiàn)滾動條下拉反彈效果,感興趣的小伙伴們可以參考一下2016-01-01
Android網(wǎng)格布局GridView實(shí)現(xiàn)漂亮的多選效果
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)格布局GridView實(shí)現(xiàn)漂亮的多選效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android滑動動態(tài)分頁實(shí)現(xiàn)方法
這篇文章主要介紹了Android滑動動態(tài)分頁實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)滑動動態(tài)分頁的操作步驟與核心實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-10-10
Android頂部工具欄和底部工具欄的簡單實(shí)現(xiàn)代碼
Android頂部工具欄和底部工具欄的簡單實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-05-05
Android中HorizontalScrollView使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android中HorizontalScrollView使用方法,感興趣的小伙伴們可以參考一下2016-05-05
Android?XML數(shù)據(jù)解析要點(diǎn)介紹
這篇文章主要為大家介紹了Android?XML數(shù)據(jù)解析要點(diǎn)介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Android實(shí)現(xiàn)底部彈出的對話框功能
這篇文章主要介紹了Android實(shí)現(xiàn)底部彈出的對話框功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

