繼承TextView,在它的onDraw方法中添加自己的邏輯
- 代碼比較少,其實就復(fù)寫了onDraw方法和onSizeChange方法,甚至都不用復(fù)寫onSizeChange方法,只要能在onDraw前獲取到該TextView的寬高即可,比如在onMeasure中直接獲取寬度神馬的
- 下面是實現(xiàn)的代碼,已添加相關(guān)注釋
package com.gsy.gsylearning.view
import android.content.Context
import android.graphics.*
import android.text.TextPaint
import android.util.AttributeSet
import android.widget.TextView
/**
* Created by gsy on 17/10/9.
* 自定義閃動的textView
*/
class SplashTextView : TextView {
// 構(gòu)造方法,不這樣寫不認,也是醉了
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, style: Int) : super(context, attrs, style)
private var mPaint: TextPaint? = null // 畫筆
private var mLinearGradient: LinearGradient? = null // 線線漸變器
private var mViewWidth = 0 // 保存textView的寬度
private var mGradientMatrix: Matrix? = null // 矩陣
private var mTranslate: Int = 0 // 平移值
/**
* 復(fù)寫onDraw方法,在繪制時進行自定義操作?
*/
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
if (mGradientMatrix != null) {
// 平移值默認為0,每100ms增加一次,每次增加五分之一的textView的寬度
mTranslate += mViewWidth / 5
// 當矩陣平移值超過textView的寬度時,將其置為負的寬度,也可以置為0,隨意
if (mTranslate > mViewWidth) mTranslate = -mViewWidth
mGradientMatrix!!.setTranslate(mTranslate.toFloat(), 0f) // 兩個感嘆號代表"我確定這個一定不為空"
mLinearGradient?.setLocalMatrix(mGradientMatrix) // 問好代表"可能為空,如果不為空則執(zhí)行操作"
postInvalidateDelayed(100)
}
}
/**
* 復(fù)寫onSizeChanged方法,當textView大小有改動時初始化寬度的信息
*/
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
mViewWidth = measuredWidth // 獲取到textView的寬高
if (mViewWidth > 0) {
mPaint = paint // 得到該textView的畫筆
mLinearGradient = LinearGradient(0f // 初始化漸變器
, 0f
, mViewWidth.toFloat()
, 0f
, intArrayOf(Color.BLUE, Color.RED, Color.BLUE)
, null
, Shader.TileMode.CLAMP)
mPaint?.shader = mLinearGradient // 設(shè)置該textView的paint的shader
mGradientMatrix = Matrix() // 初始化矩陣
}
}
}
- 其實這段代碼是參照了《android群英傳》中的自定義控件部分,只是將代碼轉(zhuǎn)成了kotlin而已,這本書挺不錯,學(xué)習(xí)它并且使用kotlin實現(xiàn),一舉兩得
- 其實那個構(gòu)造方法應(yīng)該這樣寫的, 但是這樣寫系統(tǒng)不承認,在kotlin中,這種才是標準寫法
class SplashTextView(context: Context, attrs: AttributeSet? = null, style: Int = 0) : TextView(context, attrs, style)
- 此時在layout中引用自定義控件即可,這里我直接用系統(tǒng)生成的activity和對應(yīng)的layout了
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.gsy.gsylearning.view.SplashTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a splash text view"
android:textColor="@android:color/black"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
- 在activity中setContentView即可
package com.gsy.gsylearning
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
- 下面是效果圖,由于做gif圖比較麻煩,我就截了幾張

image.png