Animation.kt 3.08 KB
Newer Older
1 2
package chat.rocket.android.util.extensions

3 4 5 6 7 8 9 10
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
11 12
import android.view.View
import android.view.ViewAnimationUtils
13
import android.view.animation.AccelerateInterpolator
14
import android.view.animation.DecelerateInterpolator
15
import androidx.fragment.app.Fragment
16

17
fun View.rotateBy(value: Float, duration: Long = 100) {
18 19 20 21 22 23
    animate()
        .rotationBy(value)
        .setDuration(duration)
        .start()
}

24 25
fun View.fadeIn(startValue: Float = 0f, finishValue: Float = 1f, duration: Long = 200) {
    if (alpha == finishValue) {
26 27 28
        setVisible(true)
        return
    }
29

30 31
    animate()
        .alpha(startValue)
32
        .setDuration(duration / 2)
33
        .setInterpolator(DecelerateInterpolator())
34
        .withEndAction {
35 36
            animate()
                .alpha(finishValue)
37
                .setDuration(duration / 2)
38
                .setInterpolator(AccelerateInterpolator()).start()
39
        }.start()
40

41 42 43
    setVisible(true)
}

44 45
fun View.fadeOut(startValue: Float = 1f, finishValue: Float = 0f, duration: Long = 200) {
    if (alpha == finishValue) {
46 47 48 49
        setVisible(false)
        return
    }

50 51 52 53
    animate()
        .alpha(startValue)
        .setDuration(duration)
        .setInterpolator(DecelerateInterpolator())
54
        .withEndAction {
55 56 57 58
            animate()
                .alpha(finishValue)
                .setDuration(duration)
                .setInterpolator(AccelerateInterpolator()).start()
59
        }.start()
60

61
    setVisible(false)
62 63
}

64 65 66 67 68 69 70 71 72
fun View.circularRevealOrUnreveal(
    centerX: Int,
    centerY: Int,
    startRadius: Float,
    endRadius: Float,
    duration: Long = 200
) {
    val anim =
        ViewAnimationUtils.createCircularReveal(this, centerX, centerY, startRadius, endRadius)
73 74 75 76 77 78 79 80 81
    anim.duration = duration

    if (startRadius < endRadius) {
        setVisible(true)
    } else {
        setVisible(false)
    }

    anim.start()
82 83
}

84
fun View.shake(x: Float = 2F, num: Int = 0) {
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    if (num == 6) {
        this.translationX = 0.toFloat()
        return
    }

    val animatorSet = AnimatorSet()
    animatorSet.playTogether(ObjectAnimator.ofFloat(this, "translationX", this.context.dp(x)))
    animatorSet.duration = 50
    animatorSet.addListener(object : AnimatorListenerAdapter() {
        override fun onAnimationEnd(animation: Animator) {
            shake(if (num == 5) 0.toFloat() else -x, num + 1)
        }
    })
    animatorSet.start()
}

fun Context.dp(value: Float): Float {
    val density = this.resources.displayMetrics.density
    val result = Math.ceil(density.times(value.toDouble()))
    return result.toFloat()
}

fun Fragment.vibrateSmartPhone() {
    val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
    if (Build.VERSION.SDK_INT >= 26) {
        vibrator.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE))
    } else {
        vibrator.vibrate(200)
    }
114
}