重写onTouchEvent
package com.example.myapplication.feedback
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.content.Context
import android.graphics.Color
import android.util.Log
import android.util.TypedValue.COMPLEX_UNIT_DIP
import android.view.MotionEvent
import com.example.myapplication.R
class FeedBackAniTextView constructor(context: Context) :
androidx.appcompat.widget.AppCompatTextView(context) {
val DEFAULT_SCALE =0.98F
val TAG="FeedBackAniTextView"
val DEFAULT_TEXT_SIZE = 10F
var mDefalutBG :Int = R.drawable.textview_default_bg
var mPressBG :Int = R.drawable.textview_press_bg
var mDefaultTextSize :Float = DEFAULT_TEXT_SIZE
var mDefalutTextColor :Int = Color.YELLOW
var mPressTextColor :Int = Color.RED
private val scaleZoomIn by lazy {
val scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f, DEFAULT_SCALE)
val scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f, DEFAULT_SCALE)
ObjectAnimator.ofPropertyValuesHolder(this@FeedBackAniTextView, scaleY, scaleX)
.setDuration(100)
}
private val scaleZoomOut by lazy {
val scaleX = PropertyValuesHolder.ofFloat("scaleX", DEFAULT_SCALE, 1.0f)
val scaleY = PropertyValuesHolder.ofFloat("scaleY", DEFAULT_SCALE, 1.0f)
ObjectAnimator.ofPropertyValuesHolder(this@FeedBackAniTextView, scaleY, scaleX)
.setDuration(100)
}
override fun onTouchEvent(event: MotionEvent?): Boolean {
Log.i(TAG,""+event?.action )
when (event?.action) {
MotionEvent.ACTION_DOWN -> {
Log.i(TAG,"ACTION_DOWN"+width)
scaleZoomIn.start()
setBackgroundResource(mPressBG)
setTextColor(mPressTextColor)
setTextSize(COMPLEX_UNIT_DIP,DEFAULT_SCALE*mDefaultTextSize)
Log.i(TAG,"ACTION_DOWN2"+width)
}
MotionEvent.ACTION_UP -> {
Log.i(TAG,"ACTION_UP1"+width)
scaleZoomOut.start()
setBackgroundResource(mDefalutBG)
setTextColor(mDefalutTextColor)
setTextSize(COMPLEX_UNIT_DIP,mDefaultTextSize)
Log.i(TAG,"ACTION_UP"+width)
}
MotionEvent.ACTION_CANCEL -> {
Log.i(TAG,"ACTION_CANCEL1"+width)
scaleZoomOut.start()
setBackgroundResource(mDefalutBG)
setTextColor(mDefalutTextColor)
setTextSize(COMPLEX_UNIT_DIP,mDefaultTextSize)
Log.i(TAG,"ACTION_CANCEL"+width)
}
}
return super.onTouchEvent(event)
}
fun setDefaultTextSize(size:Float){
mDefaultTextSize = size
setTextSize(COMPLEX_UNIT_DIP,mDefaultTextSize)
}
fun setAnimationBg(defaultBG:Int,pressBg:Int){
mDefalutBG = defaultBG
mPressBG = pressBg
setBackgroundResource(defaultBG)
}
fun setAnimationTextColor(defaultTextColor:Int,pressextColor:Int){
mDefalutTextColor = defaultTextColor
mPressTextColor = pressextColor
setTextColor(defaultTextColor)
}
}
使用:
val feedTextView = FeedBackAniTextView(baseContext).apply {
gravity = Gravity.CENTER
setDefaultTextSize(10F)
setAnimationBg(R.drawable.textview_default_bg, R.drawable.textview_press_bg)
setAnimationTextColor(Color.RED, Color.BLUE)
}
feedTextView.setTextColor(Color.GREEN)
feedTextView.text = "文案"
feedTextView.setOnClickListener {
Log.i("tag", "吐司")
}
binding.ll.addView(feedTextView, 0)
feedTextView.layoutParams.width = 200
feedTextView.layoutParams.height = 100
feedTextView.setAnimationTextColor(Color.RED, Color.BLUE)
val drawable = resources.getDrawable(R.drawable.ic_nv)
drawable.setBounds(0, 0, 10, 10)
feedTextView.setCompoundDrawables(drawable, null, drawable, null)
feedTextView.compoundDrawablePadding = 10
}