android textview按压,点击缩放,背景字体颜色变化

重写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
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zz白龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值