直播间礼物连送组件

package com.tong.live.ui.weiget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.os.Handler
import android.os.SystemClock
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import android.widget.ProgressBar
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.Nullable
import com.tong.live.R
import com.tong.live.ui.video.view.RoundProgressBar
import java.util.*


class ComboTextView @JvmOverloads constructor(
    context: Context,
    @Nullable attrs: AttributeSet? = null,
    defStyleAttr: Int = -1
) : RelativeLayout(context, attrs, defStyleAttr) {

    private val mHandler = Handler()
    private lateinit var mCountdown: RoundProgressBar
    private lateinit var mTvTime: TextView
    private val ms = 3000 / 60
    private var mTimer: Timer? = null
    private var progress = 60 // 进度
    private var mComboListener: ComboListener? = null
    private var canCombo = true
    private var canClick = true // 是否可点击
    private var downMil: Long = 0 // 按下的时间
    private var upMil: Long = 0 // 抬起的时间
    private var isCombo = false // 是否是连击的
    private var comboCount = 0 // 连击次数

    init {
        initView()
    }

    private fun initView() {
        inflate(context, R.layout.view_combo_layout, this)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
    }

    private val scaleValue = 0.99f

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (!canClick) {
            return false
        }
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                downMil = System.currentTimeMillis()
                val scaleAnimation = ScaleAnimation(
                    1f, scaleValue, 1f, scaleValue,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f
                )
                scaleAnimation.duration = 100
                scaleAnimation.fillAfter = true
                startAnimation(scaleAnimation)
            }
            MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> {
                upMil = System.currentTimeMillis()
                isCombo = false
                if (upMil - downMil < 400) {
                    if (canCombo) {
                        if (mTimer == null) {
                            mTimer = Timer()
                        } else {
                            try {
                                mTimer?.cancel()
                                mTimer = null
                                mTimer = Timer()
                            } catch (e: Exception) {
                                e.printStackTrace()
                                mTimer = null
                                mTimer = Timer()
                            }
                        }
                        if (progress > 0 && progress < 60) {
                            isCombo = true
                            comboCount++
                        } else {
                            comboCount = 0
                            isCombo = false
                        }

                        progress =60
                        progress--
                        mTimer?.schedule(object : TimerTask() {
                            override fun run() {
                                //SystemClock.sleep(ms.toLong())
                                mHandler.post {
                                    if (progress <=0) {
                                        if (mTimer == null) {
                                            return@post
                                        }
                                        mComboListener?.comboOver(comboCount)
                                        comboCount = 0
                                        mTimer?.cancel()
                                        mTimer = null
                                        //mTvTime?.text = ""

                                        val scale2 = ScaleAnimation(
                                            scaleValue, 1f, scaleValue, 1f,
                                            Animation.RELATIVE_TO_SELF, 0.5f,
                                            Animation.RELATIVE_TO_SELF, 0.5f
                                        )
                                        scale2.duration = 100
                                        scale2.fillAfter = false
                                        scale2.setAnimationListener(object : Animation.AnimationListener {
                                            override fun onAnimationStart(animation: Animation) {}
                                            override fun onAnimationEnd(animation: Animation) {
                                                visibility = View.GONE
                                                invalidate()
                                                requestLayout()
                                            }
                                            override fun onAnimationRepeat(animation: Animation) {}
                                        })
                                        startAnimation(scale2)
                                        return@post
                                    }
                                    progress--
                                    mCountdown?.setProgress(progress)
                                    mTvTime?.text = comboCount.toString()
                                }
                            }
                        }, 0, 3000 / 60)
                    }
                    mComboListener?.click(isCombo, comboCount)
                }
            }
        }
        return true
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        // 加载完成,设置开始倒计时
        mCountdown = findViewById(R.id.pb_countdown)
        mCountdown?.max1 = 60
        mCountdown?.progress1 = 0
        mTvTime = findViewById(R.id.tv_time)
        mTvTime.text = ""
    }

    interface ComboListener {
        /**
         * 点击事件回调
         *
         * @param isCombo    该次点击是否是连击
         * @param comboCount 连击次数
         */
        fun click(isCombo: Boolean, comboCount: Int)

        /**
         * 连击结束
         *
         * @param comboCount 连击次数
         */
        fun comboOver(comboCount: Int)
    }

    fun setComboListener(comboListener: ComboListener) {
        mComboListener = comboListener
    }

    /**
     * 设置按钮类型
     *
     * @param type 1:可连击  2:不可连击 (默认可连击)
     */
    fun setType(type: Int) {
        when (type) {
            1 -> {
                // 可连击
                canCombo = true
            }
            2 -> {
                // 不可连击
                canCombo = false
            }
        }
    }

    /**
     * 设置按钮状态
     *
     * @param state 1:不可点击状态  2:可点击状态  (默认可点击)
     */
    fun setState(state: Int) {
        when (state) {
            1 -> {
                // 不可点击状态
                canClick = false
                mTimer?.cancel()
                mTimer = null
                mTvTime.text = "发送"
                mTvTime.setTextColor(Color.parseColor("#a7acb2"))
                mCountdown.setBackgroundColor(Color.parseColor("#ee000000"))
                if (progress > 0 && progress < 60) {
                    // 还在连击状态
                    mComboListener?.comboOver(comboCount)
                    comboCount = 0
                    progress = 0
                    mCountdown.progress1 = progress
                }
            }
            2 -> {
                // 可点击状态
                canClick = true
                mTimer?.cancel()
                mTimer = null
                mTvTime.text = "发送"
                mTvTime.setTextColor(Color.parseColor("#ffffff"))
                mCountdown.setBackgroundColor(Color.parseColor("#12b06b"))
                if (progress > 0 && progress < 60) {
                    // 还在连击状态
                    mComboListener?.comboOver(comboCount)
                    comboCount = 0
                    progress = 0
                    mCountdown.progress1 = progress
                }
            }
        }
    }

    /**
     * 取消 不会回调任何方法
     */
    fun cancel() {
        mTimer?.cancel()
        mTimer = null
        comboCount = 0
        progress = 0
        mCountdown.progress1 = progress
    }

    fun setCount( count:Int){
        comboCount= count
        mTvTime.text = comboCount.toString()
    }

    fun start(){
        if (mTimer == null) {
            mTimer = Timer()
        } else {
            try {
                mTimer?.cancel()
                mTimer = null
                mTimer = Timer()
            } catch (e: Exception) {
                e.printStackTrace()
                mTimer = null
                mTimer = Timer()
            }
        }
//        if (progress > 0 && progress < 60) {
//            // 是连击
//            isCombo = true
//            comboCount++
//        } else {
//            comboCount = 0
//            isCombo = false
//        }

        progress = 60
        --progress
        mTimer?.schedule(object : TimerTask() {
            override fun run() {
                //SystemClock.sleep(ms.toLong())
                mHandler.post {
                    if (progress <=0) {
                        if (mTimer == null) {
                            // 这里不能删,因为上面有一个等待时间,肯定会出现已经执行过这里的情况的
                            return@post
                        }
                        mComboListener?.comboOver(comboCount)
                        comboCount = 0
                        mTimer?.cancel()
                        mTimer = null
                        mTvTime.text = ""

                        return@post
                    }
                    progress--
                    mCountdown.setProgress(progress)
                    mTvTime.text = comboCount.toString()
                }
            }
        }, 0, (3000 / 60).toLong())
    }

    fun destory() {
        mTimer?.cancel()
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值