Redes Sociais

 Classe de exemplo pra animação

18 de setembro de 2021

Uma simples ideia para uma classe de animação em kotlin.

package br.com.uware.leutzlib.animation

import android.view.View
import android.view.animation.*


/**
 * AnimUtils(time, {onStartBlock}, {onEndBlock}, {onRepeatBlock})
 *
 * Classe de animações de views.
 *
 * @author Rodrigo Leutz
 * @param time Long com o tempo de duração da animação.
 * @param onStartBlock Bloco executado ao iniciar.
 * @param onEndBlock Bloco executado ao terminar.
 * @param onRepeatBlock Bloco executado ao repetir.
 */
class AnimUtils(
    private val time: Long,
    private val onStartBlock: () -> Unit = {},
    private val onEndBlock: () -> Unit = {},
    private val onRepeatBlock: () -> Unit = {}
) {

    /**
     * animTranslate(view,xi,xd,yi,yd)
     *
     * Move uma view na tela.
     *
     * @author Rodrigo Leutz
     * @param view View que se moverá.
     * @param xi Double com o valor de x inicial.
     * @param xd Double com o valor de x destino.
     * @param yi Double com o valor de y inicial.
     * @param yd Double com o valor de y destino.
     */
    fun animTranslate(
        view: View,
        xi: Double = 0.0, xd: Double = 0.0,
        yi: Double = 0.0, yd: Double = 0.0
    ) {
        val interpolator = DecelerateInterpolator()
        val animation = TranslateAnimation(
            Animation.RELATIVE_TO_SELF, xi.toFloat(),
            Animation.RELATIVE_TO_SELF, xd.toFloat(),
            Animation.RELATIVE_TO_SELF, yi.toFloat(),
            Animation.RELATIVE_TO_SELF, yd.toFloat()
        )
        animation.duration = time
        animation.repeatMode = Animation.REVERSE
        animation.repeatCount = 0
        animation.interpolator = interpolator
        animation.setAnimationListener(animationListner)
        view.startAnimation(animation)
    }

    /**
     * animRotate(view)
     *
     * Rotaciona uma view.
     *
     * @author Rodrigo Leutz
     * @param view View que será rotacionada.
     */
    fun animRotate(view: View) {
        val interpolator = AccelerateDecelerateInterpolator()
        val animation = RotateAnimation(
            0f,
            360f,
            Animation.RELATIVE_TO_SELF,
            0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f
        )
        animation.duration = time
        animation.repeatMode = Animation.REVERSE
        animation.repeatCount = 0
        animation.interpolator = interpolator
        animation.setAnimationListener(animationListner)
        view.startAnimation(animation)
    }

    /**
     * animTranslateRotateLeftToDefault(view, time)
     *
     * Gira e se move de fora da tela do canto esquerdo 
     * para posição original girando.
     *
     * @author Rodrigo Leutz
     * @param view View que recebera a animação.
     */
    fun animTranslateRotateLeftToDefault(view: View) {
        val animation = AnimationSet(true)
        val interpolator = AccelerateDecelerateInterpolator()
        val rotate = RotateAnimation(
            0f,
            360f,
            Animation.RELATIVE_TO_SELF,
            0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f
        )
        val translate = TranslateAnimation(
            Animation.RELATIVE_TO_SELF, -1f,
            Animation.RELATIVE_TO_SELF, 0f,
            Animation.RELATIVE_TO_SELF, 0f,
            Animation.RELATIVE_TO_SELF, 0f
        )
        animation.addAnimation(rotate)
        animation.addAnimation(translate)
        animation.duration = time
        animation.repeatMode = Animation.RESTART
        animation.repeatCount = 0
        animation.interpolator = interpolator
        animation.setAnimationListener(animationListner)
        view.startAnimation(animation)
    }

    /**
     * animationListener
     *
     * @author Rodrigo Leutz
     */
    private val animationListner = object : Animation.AnimationListener {
        override fun onAnimationStart(animation: Animation?) {
            onStartBlock()
        }

        override fun onAnimationEnd(animation: Animation?) {
            onEndBlock()
        }

        override fun onAnimationRepeat(animation: Animation?) {
            onRepeatBlock()
        }
    }
}

Desenvolvedor Web e Android ( Kotlin e Java )