欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年04月28日 09:41:08   作者:zhangmeiwen95  
這篇文章主要介紹了使用Kotlin實(shí)現(xiàn)文字漸變TextView的代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

實(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],作用是指定某個(gè)位置的顏色值,如果傳null,漸變就線性變化。
      //tile:用于指定控件區(qū)域大于指定的漸變區(qū)域時(shí),空白區(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android實(shí)現(xiàn)閃屏頁效果

    Android實(shí)現(xiàn)閃屏頁效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)閃屏頁效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Android切面編程知識(shí)點(diǎn)詳解

    Android切面編程知識(shí)點(diǎn)詳解

    這篇文章給大家整理了關(guān)于Android進(jìn)階資深開發(fā)必備技能-切面編程的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友可以參考學(xué)習(xí)下。
    2018-07-07
  • 超實(shí)用的Android手勢鎖制作實(shí)例教程

    超實(shí)用的Android手勢鎖制作實(shí)例教程

    這篇文章主要介紹了一個(gè)超實(shí)用的Android手勢鎖制作實(shí)例教程,普通的圓環(huán)形圖標(biāo)變換,在App和系統(tǒng)的鎖屏界面中都可以調(diào)用,需要的朋友可以參考下
    2016-04-04
  • andriod如何搭建自己的輪詢框架

    andriod如何搭建自己的輪詢框架

    很多時(shí)候Android應(yīng)用需要每間隔一段時(shí)間向服務(wù)器請(qǐng)求數(shù)據(jù),如果服務(wù)器數(shù)據(jù)有更新則通知界面變化。Android中最常用的紅點(diǎn)一般采用的就是輪詢,本文主要介紹了Android如何搭建自己的輪詢框架,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Android14(U)適配攻略

    Android14(U)適配攻略

    本文主要介紹了Android14(U)適配攻略,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Android應(yīng)用退出登錄的實(shí)現(xiàn)方法

    Android應(yīng)用退出登錄的實(shí)現(xiàn)方法

    每一個(gè)app都會(huì)有一個(gè)”退出登陸”的功能,當(dāng)點(diǎn)擊退出之后需要將所有的Activity都finish掉,開始是想將棧中的所有Activity清除掉,但是沒有找到方法,后來用廣播實(shí)現(xiàn)了。下面小編給大家分享android應(yīng)用退出登錄的實(shí)現(xiàn)方法,需要的朋友參考下
    2017-04-04
  • 最新評(píng)論