Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android view animation stops when view out of screen

I'm having a recyclerView with several views and one is the animation view which has an animation applied to it. Once the view is out of the screen the animation is no longer active, even though the animation still exists.

The data:

rotate_around_center_point.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <rotate
        android:duration="2500"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toDegrees="360" />

</set>

Applying animation:

                animation = AnimationUtils.loadAnimation(this.getContext(),
                        R.anim.rotate_around_center_point);
                loadingRotatingCircleIV.startAnimation(animation);

I could not find any way to catch an event when the animation is interrupted so I'm able to restart the animation once it was out of the screen.

like image 470
Arny Avatar asked Nov 17 '14 18:11

Arny


People also ask

What can be used to animate between two or more views?

Drawable Animation Drawable Animation is used if you want to animate one image over another. The simple way to understand is to animate drawable is to load the series of drawable one after another to create an animation.

How do I turn off animator on Android?

Open Settings . Scroll down and select Accessibility. Scroll down to Display and tap Text and display. Tap the toggle switch for Remove animations to turn it on.


2 Answers

RecyclerView is incorrectly stopping animation on the view when view goes out from screen only if you apply View Animation to that view. If you will use Property Animation everything works properly. So the following solution will work:

ObjectAnimator animator = ObjectAnimator.ofFloat(loadingRotatingCircleIV, "rotation", 0.0f, 360.0f);
animator.setDuration(1000L);
animator.setRepeatCount(ObjectAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.start(); 
like image 168
michal.z Avatar answered Nov 15 '22 20:11

michal.z


If you want to use a ViewAnimation for a RecyclerView item, you have to restore animation in overriden method of RecyclerView - onViewAttachedToWindow(ItemViewHolder holder), like:

@Override
public void onViewAttachedToWindow(ItemViewHolder holder) {
    super.onViewAttachedToWindow(holder);

    if (holder.animated) 
    {
        Animation anim = new ScaleAnimation(0.8f, 1.2f, 
            0.8f, 1.2f,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setFillAfter(true);
        anim.setInterpolator(new BounceInterpolator());
        anim.setDuration(1100);
        anim.setRepeatCount(Animation.INFINITE);
        anim.setRepeatMode(Animation.REVERSE);
        holder.animatedView.startAnimation(anim);
    }
}
like image 21
Alex_297 Avatar answered Nov 15 '22 20:11

Alex_297