Reverse AnimatorSet - android

Is there a way to run an AnimatorSet in reverse on Android? The ValueAnimator API does provide a reverse method on the individual animators but not on a set of animators.

If your AnimatorSet is being played sequentially then you could use the method mentioned by #blackbelt:
public static AnimatorSet reverseSequentialAnimatorSet(AnimatorSet animatorSet) {
ArrayList<Animator> animators = animatorSet.getChildAnimations();
Collections.reverse(animators);
AnimatorSet reversedAnimatorSet = new AnimatorSet();
reversedAnimatorSet.playSequentially(animators);
reversedAnimatorSet.setDuration(animatorSet.getDuration());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
// getInterpolator() requires API 18
reversedAnimatorSet.setInterpolator(animatorSet.getInterpolator());
}
return reversedAnimatorSet;
}
The caveat being that this only works for simple sequential animations as any dependencies setup in the original AnimatorSet will be lost. Also, if an interpolator was used on the AnimatorSet it will only carry over on API 18 or newer (per method mentioned above, you could alternatively manually add the interpolator back to the new reversed animator set).
The individual animations within the AnimatorSet will not play in reverse, if that is desirable then you'll also have to iterate over the animations of the AnimatorSet and set a ReverseInterpolator on each, see answer to Android: Reversing an Animation.

You can take the initial input values from i.e ValueAnimator.ofFloat(0, 1) and switch them around with yourAnimator.setFloatValues(1, 0) before calling yourAnimatorSet.start() when you want to reverse animation

The reverse() method has been added in API 26:
Plays the AnimatorSet in reverse. If the animation has been seeked to a specific play time using setCurrentPlayTime(long), it will play backwards from the point seeked when reverse was called. Otherwise, then it will start from the end and play backwards. This behavior is only set for the current animation; future playing of the animation will use the default behavior of playing forward.
Note: reverse is not supported for infinite AnimatorSet.

Related

Animator set inside the Animator set

var anim = AnimatorSet()
var anim1 = AnimatorSet()
anim1.playSequentially(mutablelist)
anim.play(anim1)
var anim2 = AnimatorSet()
anim2.playTogether(mutablelist)
anim.play(anim2)
anim.start()
Like this I have So many animator inside the Base Animator set.Now My question is ,I want to play the ( anim ) sequentially. But here it runs parallely.Kindly suggest good way to slove this.
There are two different way to adding animations to an AnimatorSet: either the AnimatorSet#playTogether(Animator[]) or AnimatorSet#playSequentially(Animator[]) this methods can called to add a set of animations all at once, or the AnimatorSet#play(Animator) can used in conjunction with methods in the AnimatorSet.Builder class to add animations one by one.
It is possible to set up AnimatorSet with circular dependencies between its animations. For example, an animation a1 could be set up to start before animation a2, a2 before a3, and a3 before a1.
more information about it click hear

Trying to reset values from Property Animator to be used in recycler view

Been doing some animation inside of a row in RecyclerView (not the row itself. Imagine expanding text) and there are times when the animation leaks to other recycled views which should not have that animation in them.
Since I use property animation the scale action resizes the inner view and the leak can be seen in 2 aspects:
1) The animation will go on (This I could overcome with some guards)
2) The view has been resized and stopped in its tracks and so it will be reflected in the recycled view.
How can I reset the views to their original state? I have tried many approaches from posts but none solved it. The closest definition I got was in this unanswered post:
How to reset view to original state after using animators to animates its some properties?
Here is a sample of how I set up my animation in onBind (this one has an attempt to use onAnimationEnd which I found in one post but did not work)
ObjectAnimator scaleXUp = ObjectAnimator.ofFloat(mView, View.SCALE_X, 10f);
scaleXUp.setRepeatCount(ValueAnimator.INFINITE);
scaleXUp.setRepeatMode(ValueAnimator.REVERSE);
scaleXUp.setDuration(700);
ObjectAnimator scaleYUp = ObjectAnimator.ofFloat(mView, View.SCALE_Y, 10f);
scaleYUp.setRepeatCount(ValueAnimator.INFINITE);
scaleYUp.setRepeatMode(ValueAnimator.REVERSE);
scaleYUp.setDuration(700);
mTotalAnimation = new AnimatorSet();
mTotalAnimation.play(scaleXUp).with(scaleYUp);
mTotalAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
mTotalAnimation.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
animation.removeListener(this);
animation.setDuration(0);
for(Animator va : ((AnimatorSet)animation).getChildAnimations()) {
((ValueAnimator)va).reverse();
}
}
});
mTotalAnimation.start();
And here is what I do in the onUnbindData:
if (mTotalAnimation != null) {
mTotalAnimation.end();
mTotalAnimation = null;
}
And as I saw many people like the clearAnimation approach - tried and did not work either.
4 days and not one response but meanwhile I solved it myself.
So the approach was close, just wrongly placed.
I added this method:
private void stopAnimation() {
for (Animator anim : mTotalAnimation.getChildAnimations()) {
((ObjectAnimator) anim).reverse();
anim.end();
}
}
and I call that when I want to reset the view.
Here I am getting the animations from the AnimatorSet and reversing and ending each. I did not understand why I had to do it manually but it looks like this ability will be added in Android O:
https://developer.android.com/preview/api-overview.html#aset
Starting in Android O, the AnimatorSet API now supports seeking and playing in reverse. Seeking lets you set the position of the animation set to a specific point in time. Playing in reverse is useful if your app includes animations for actions that can be undone. Instead of defining two separate animation sets, you can play the same one in reverse.

nineoldandroids - setDuration doesnt work on AnimatorSet

I have the following code which splits an image in half and animate each part in different direction:
final AnimatorSet mSetAnim = new AnimatorSet();
final Animator topAnim = ObjectAnimator.ofFloat(topImage, "translationY", (bmp.getHeight() / 2) * -1);
final Animator bottomAnim = ObjectAnimator.ofFloat(bottomImage, "translationY", bmp.getHeight() / 2);
mSetAnim.setDuration(duration);
mSetAnim.playTogether(topAnim, bottomAnim);
mSetAnim.start();
I'm using NineOldAndroids in order to support old android versions but for some reason no matter what value i put in 'duration' it doesnt seem to have any affect. The animation duration stays the same - half a second more or less.
When i use android's animation apis i can see that the duration is changing.
Any idea how to workarround this ?
Using mSetAnim.getDuration(), Try printing in a toast to see what value exists at runtime.
Use mSetAnim.setDuration(duration); after playTogether() as is done in nineoldandroids.com.

Setting end values without animation with AnimatorSet

According to the documentation http://developer.android.com/reference/android/animation/AnimatorSet.html#end() end() method should assign the end values for all animations inside the set. However ending does nothing if the set was not started. This is different from ValueAnimator i.e. which does not care and artifically starts the animation, fires onAnimationStart() event and then ends it which results in setting the end values properly.
Let me tell you why this is a problem. If you want to have the ability to update your View with and without animation (depending on the situation) you don't want to have two separate methods for it. Instead you could create an animation and start it normally or immediately set the end values. With ValueAnimator this works nicely. However AnimatorSet does not play ball and if you try to end it before calling start() it simply exits.
One workaround for this is to call start() and end() immediately after but it does not cover the situation with nested AnimatorSets like this:
AnimatorSet mainSet = new AnimatorSet();
AnimatorSet scaleSet = new AnimatorSet();
scaleSet.playTogether(
ObjectAnimator.ofFloat(view, "scaleX", 0.5f),
ObjectAnimator.ofFloat(view, "scaleY", 0.5f));
mainSet.playSequentially(
ObjectAnimator.ofFloat(view, "alpha", 0.5f),
scaleSet);
mainSet.start();
mainSet.end();
This causes the mainSet to call end() on all its children. But since AnimatorSet.end() is broken the scaleSet does not set ending values. I attempted to extend AnimatorSet class and fix this but soon found out it is marked final.
In my app I mostly use animations but have some few cases where animations are not needed. Does anyone have an idea how to achieve the proper behaviour?
You can do that on all child animations with:
for (Animator animation : mainSet.getChildAnimations())
{
animation.start();
animation.end();
}
I know this is an old question but a recursive approach can solve this:
void endAnimationsRecursively(final Animator mainAnimator){
if (mainAnimator instanceof AnimatorSet) {
for (final Animator animator : ((AnimatorSet)
mainAnimator).getChildAnimations()) {
endAnimationsRecursively(animator);
}
mainAnimator.end();
mainAnimator.cancel();
}
}

How to resume and pause ObjectAnimator in Android for API Levels below 19?

I am aware that API level 19 supports pause() and resume() on ObjectAnimators. But in my project at API level 14, I have an ObjectAnimator which is applied to an Image view to rotate it. I want to pause the animation provided by the ObjectAnimator on touch and resume it from the place where the image view was (before touch down).
So I attempted to save the current play time and cancel the object animator on my stopAnimation() function.
private void stopAnimation(){
currentTime = mGlobeAnimator.getCurrentPlayTime();
mGlobeAnimator.cancel();
}
In the startAnimation() function, I recreate the animator, set its target to the image view, set the saved play time and start it.
private void startAnimation(Context context, View view, float startAngle) {
ObjectAnimator globeAnimatorClone = (ObjectAnimator)AnimatorInflater.loadAnimator(context, R.animator.rotate_globe);
globeAnimatorClone.setTarget(mImageView);
globeAnimatorClone.setCurrentPlayTime(currentTime);
globeAnimatorClone.start();
}
This does not work. Would anybody help please with any pointers to pause and resume animation provided by animator for API level before 19?
I think I got it working by starting the animator and then setting the currentPlayTime(). The documentation clearly tells (which I just stumbled upon) that if the animation has not been started, the currentPlayTime set using this method will not advance the forward!
Sets the position of the animation to the specified point in time. This time should be between 0 and the total duration of the animation, including any repetition. If the animation has not yet been started, then it will not advance forward after it is set to this time; it will simply set the time to this value and perform any appropriate actions based on that time. If the animation is already running, then setCurrentPlayTime() will set the current playing time to this value and continue playing from that point. http://developer.android.com/reference/android/animation/ValueAnimator.html#setCurrentPlayTime(long)
private void stopAnimation(){
mCurrentPlayTime = mRotateAntiClockwiseAnimator.getCurrentPlayTime();
mRotateAntiClockwiseAnimator.cancel();
}
private void startAnimation() {
mRotateAntiClockwiseAnimator.start();
mRotateAntiClockwiseAnimator.setCurrentPlayTime(mCurrentPlayTime);
}
What you are doing is that it will just restart your animation, instead you can create a custom class for animation with pause and resume method.
You need to first check if the device is 19 api and above if it is then use the native pauses and resume of the object animator else use the regular Animation designed from api 1, you can follow this thread for pause and resume below api 19.
Its probably too late for me to answer, i just came across this problem and i solved it using your method.
i just added two things to your approach
1. In the start of animation check if the ** mCurrentPlayTime** is greater than zero.if its >0 then setCurrentPlayTime. otherwise its useless.
2. when your animation ends make it zero again.

Categories

Resources