package com.adealink.weparty.commonui.ext

import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.RotateAnimation
import android.view.animation.ScaleAnimation

/**
 * 动画动画
 * 参考: https://www.jianshu.com/p/7d262563edda
 */
fun View.shake(scaleSmall: Float, scaleLarge: Float, shakeDegrees: Float, duration: Long) {
    //由小变大
    val scaleAnim = ScaleAnimation(
        scaleSmall,
        scaleLarge,
        scaleSmall,
        scaleLarge,
        Animation.RELATIVE_TO_SELF,
        0.5f,
        Animation.RELATIVE_TO_SELF,
        0.5f
    ).apply {
        this.duration = duration
    }
    //从左向右
    val rotateAnim = RotateAnimation(
        -shakeDegrees,
        shakeDegrees,
        Animation.RELATIVE_TO_SELF,
        0.5f,
        Animation.RELATIVE_TO_SELF,
        0.5f
    ).apply {
        this.repeatMode = Animation.REVERSE
        this.repeatCount = 6
        this.duration = duration
    }

    val shakeAnim = AnimationSet(false)
    shakeAnim.addAnimation(scaleAnim)
    shakeAnim.addAnimation(rotateAnim)
    startAnimation(shakeAnim)
}