直播间 点赞组件附带动画效果

package com.example.myapplication.upvote

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Path
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import androidx.appcompat.widget.AppCompatImageView
import com.tong.live.R
import com.tong.live.util.dp
import java.util.Random


class FloatingAnimationView(context: Context) : AppCompatImageView(context) {

    init {
        setImageResource(randomImageResource()) // 随机设置图片
        layoutParams = ViewGroup.LayoutParams(IMAGE_SIZE, IMAGE_SIZE)
        alpha = 0.6f // 透明度初始值
    }

    fun startAnimation(parent: ViewGroup) {
        parent.addView(this)

        // 初始位置(在 bottomRightView 底部)
        val startX = parent.width/2f  - IMAGE_SIZE/2f
        val startY = (parent.height - 100).toFloat()

        x = startX
        y = startY

        // 计算动画路径
        val path = Path()
        val endX: Float = startX + Random().nextInt(20) - 10
        val endY = startY - 400

        val controlX: Float = startX - (Random().nextInt(20) - 10)
        val controlY: Float = startY - (Random().nextInt(40) + 25)
        path.moveTo(startX, startY)
        path.quadTo(controlX, controlY, endX, endY)

        // 位置动画
        val positionAnimator = ObjectAnimator.ofFloat(this, "x", "y", path)
        positionAnimator.interpolator = LinearInterpolator()
        positionAnimator.setDuration(ANIMATION_DURATION.toLong())

        val scaleAnimator = ObjectAnimator.ofFloat(this, SCALE_X, 0.5f, 1.0f)
        scaleAnimator.setDuration(100)
        // 透明度动画
        val alphaAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.6f, 0f)
        alphaAnimator.setDuration(ANIMATION_DURATION.toLong())
        alphaAnimator.interpolator = LinearInterpolator()

        // 动画结束后移除视图
        positionAnimator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                parent.removeView(this@FloatingAnimationView)
            }
        })

        val animatorSet = AnimatorSet()
        animatorSet.playTogether(positionAnimator, scaleAnimator, alphaAnimator)
        animatorSet.start()
    }

    private fun randomImageResource(): Int {
        val images = intArrayOf(
            R.drawable.live_like1_icon,
            R.drawable.live_like2_icon,
            R.drawable.live_like3_icon,
            R.drawable.live_like4_icon,
            R.drawable.live_like5_icon
        )
        return images[Random().nextInt(images.size)]
    }

    companion object {
        private  val ANIMATION_DURATION = 2000
        private  var IMAGE_SIZE = 24.dp // 与 Swift 代码等效,图片大小 24dp
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值