繼承TextView,在它的onDraw方法中添加自己的邏輯
- 代碼比較少荐糜,其實(shí)就復(fù)寫(xiě)了onDraw方法和onSizeChange方法,甚至都不用復(fù)寫(xiě)onSizeChange方法,只要能在onDraw前獲取到該TextView的寬高即可,比如在onMeasure中直接獲取寬度神馬的
- 下面是實(shí)現(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.
* 自定義閃動(dòng)的textView
*/
class SplashTextView : TextView {
// 構(gòu)造方法,不這樣寫(xiě)不認(rèn)丙笋,也是醉了
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 // 畫(huà)筆
private var mLinearGradient: LinearGradient? = null // 線線漸變器
private var mViewWidth = 0 // 保存textView的寬度
private var mGradientMatrix: Matrix? = null // 矩陣
private var mTranslate: Int = 0 // 平移值
/**
* 復(fù)寫(xiě)onDraw方法谢澈,在繪制時(shí)進(jìn)行自定義操作?
*/
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
if (mGradientMatrix != null) {
// 平移值默認(rèn)為0,每100ms增加一次御板,每次增加五分之一的textView的寬度
mTranslate += mViewWidth / 5
// 當(dāng)矩陣平移值超過(guò)textView的寬度時(shí)锥忿,將其置為負(fù)的寬度,也可以置為0怠肋,隨意
if (mTranslate > mViewWidth) mTranslate = -mViewWidth
mGradientMatrix!!.setTranslate(mTranslate.toFloat(), 0f) // 兩個(gè)感嘆號(hào)代表"我確定這個(gè)一定不為空"
mLinearGradient?.setLocalMatrix(mGradientMatrix) // 問(wèn)好代表"可能為空敬鬓,如果不為空則執(zhí)行操作"
postInvalidateDelayed(100)
}
}
/**
* 復(fù)寫(xiě)onSizeChanged方法,當(dāng)textView大小有改動(dòng)時(shí)初始化寬度的信息
*/
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的畫(huà)筆
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() // 初始化矩陣
}
}
}
- 其實(shí)這段代碼是參照了《android群英傳》中的自定義控件部分笙各,只是將代碼轉(zhuǎn)成了kotlin而已钉答,這本書(shū)挺不錯(cuò),學(xué)習(xí)它并且使用kotlin實(shí)現(xiàn)杈抢,一舉兩得
- 其實(shí)那個(gè)構(gòu)造方法應(yīng)該這樣寫(xiě)的数尿, 但是這樣寫(xiě)系統(tǒng)不承認(rèn),在kotlin中惶楼,這種才是標(biāo)準(zhǔn)寫(xiě)法
class SplashTextView(context: Context, attrs: AttributeSet? = null, style: Int = 0) : TextView(context, attrs, style)
- 此時(shí)在layout中引用自定義控件即可右蹦,這里我直接用系統(tǒng)生成的activity和對(duì)應(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