Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I repeat a property animation?

I'm doing an animation of bubbles on the screen, but the bubbles stop after finishing the animation time. How do I repeat the animation or make it infinite?

bub.animate();
bub.animate().x(x2).y(y2);
bub.animate().setDuration(animationTime);       
bub.animate().setListener(new AnimatorListenerAdapter() {

    @Override
    public void onAnimationStart(Animator animation) {
        animators.add(animation); 
    } 

    @Override
    public void onAnimationRepeat(Animator animation) {
    }

    @Override
    public void onAnimationEnd(Animator animation) {
    }
});
like image 528
user3901032 Avatar asked Aug 17 '14 00:08

user3901032


People also ask

How do you repeat an animation in Kotlin?

Set the number of times the animation will repeat by setting the object's repeatCount value. Then you set its repeatMode to define what the animation does when it reaches the end. More on this soon! Set duration and start the animation.

Which category of animation allows you to animate an object's properties?

The property animation system is a robust framework that allows you to animate almost anything. You can define an animation to change any object property over time, regardless of whether it draws to the screen or not.

What are the two different types of view animations?

There are two types of animations that you can do with the view animation framework: Tween animation: Creates an animation by performing a series of transformations on a single image with an Animation. Frame animation: or creates an animation by showing a sequence of images in order with an AnimationDrawable .


2 Answers

Here is an example in Kotlin with a simple way to repeat the animation by recursively calling it in withEndAction

Example

private var animationCount = 0

private fun gyrate() {
    val scale = if (animationCount++ % 2 == 0) 0.92f else 1f
    animate().scaleX(scale).scaleY(scale).setDuration(725).withEndAction(::gyrate)
}

This repeatedly animates the size of a view to get smaller, return to normal, get smaller, return to normal, etc. This is a pretty simple pattern to repeat whatever animation you'd like.

like image 147
Trevor Avatar answered Oct 15 '22 06:10

Trevor


You can use CycleInterpolator. For example, like this:

    int durationMs = 60000;
    int cycleDurationMs = 1000;
    view.setAlpha(0f);
    view.animate().alpha(1f)
            .setInterpolator(new CycleInterpolator(durationMs / cycleDurationMs))
            .setDuration(durationMs)
            .start();
like image 25
Dmide Avatar answered Oct 15 '22 04:10

Dmide