How to use setDelay for animation within an AnimatorSet? - android

I'm trying to implement a set of animations, but the start delay value does not seem to be respected. Example:
ObjectAnimator a1 = ObjectAnimator.ofFloat(view1, View.ALPHA, 1f, 0f);
a1.setDuration(500);
a1.setStartDelay(500);
ObjectAnimator a2 = ObjectAnimator.ofFloat(view2, "translationX", ...);
a2.setDuration(500);
AnimatorSet set = new AnimatorSet();
set.playTogether(a1, a2);
set.start();
I can see the alpha animation starts immediately. If I don't try to play them together, the delay is respected and works fine. Can the delay not be used if part of an AnimatorSet?

If you want to play the animations in sequence, don't use ObjectAnimator.setStartDelay(). It's not designed for that use case.
Use AnimatorSet.playSequentially() instead:
AnimatorSet set = new AnimatorSet();
set.playSequentially(a2, a1);
set.start();

Related

Use one animation object for multi views

I want set alpha animation to views but with one object without create many object like this fadeIn1 , fadeIn2 , fadeIn3 etc
View1 , View2 , View3
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setStartOffset(1000);
fadeIn.setDuration(1000);
view1.startAnimation(fadeIn); //I want show this after 1 second
fadeIn.setStartOffset(1500);
view2.startAnimation(fadeIn); //I want show this after 1.5 second
fadeIn.setStartOffset(2000);
view3.startAnimation(fadeIn); //I want show this after 2 second
But all views shows after 2 second together , Why ?
Use AnimatiorSet for this
Note: The below code was in kotlin.
First create an function which returns an animator by taking view
private fun getAnimation(view: View): Animator {
return ObjectAnimator.ofFloat(view, "alpha", 0f, 1f).apply {
duration = 1000
}
}
Then after user AnimatorSet.playSequentially to play one after other.
AnimatorSet().apply {
playSequentially(listOf(getAnimation(btn_1),getAnimation(btn_2),getAnimation(btn_3)))
interpolator = AccelerateInterpolator()
start()
}
Please free to ask any query in comment section, I'll try to answer as soon as possible :)
You need AnimationSet ex:
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(1000);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
AnimationSet animation = new AnimationSet(true);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
view.startAnimation(animation);

How to add a delay between animations in AnimatorSet playSequentially()?

I'm using AnimatorSet playSequentially method like this:
AnimatorSet set = new AnimatorSet();
ObjectAnimator in = ObjectAnimator.ofFloat(splash, "alpha", 0f, 1f);
in.setDuration(2500);
in.setInterpolator(new AccelerateInterpolator());
ObjectAnimator out = ObjectAnimator.ofFloat(splash, "alpha", 1f, 0f);
out.setDuration(2500);
out.setInterpolator(new AccelerateInterpolator());
set.playSequentially(in,out);
I whould like to add a delay between animation 1 and 2, like this:
set.playSequentially(in,1000,out);
It is possible to add delays between animations using playSequentially method?
Thank you.
Add this line of code:
out.setStartDelay(1000);
You could set a start delay on the 2nd Animator (i.e. out.setStartDelay(1000)) before adding it to the set.
Rather than setting a start delay on the individual animations,
use the AnimatorSet.after(long delay) function.
AnimatorSet s = new AnimatorSet();
s.play(anim1).after(1000).after(anim2);
AnimatorSet.Builder

How to cancel a single animation that has been run as part of an animation set

I have an animation set similar to this:
ObjectAnimator animator1 = ...;
ObjectAnimator animator2 = ...;
ObjectAnimator animator3 = ...;
AnimatorSet set = new AnimatorSet();
set.playTogether(animator1, animator2, animator3);
set.addListener(mylistener);
set.start();
Is it possible for me to cancel only one of the animators above without canceling the rest of the set, say for example animator2?
Thanks very much for any replies.
Sorry for simple guess, but does animator2.cancel() help?

How to change animation speed when is playing?

I have Animation and i need to change duration or speed in run.
How to do this?
Animation fadeIn = new AlphaAnimation(1, 0);
fadeIn.setInterpolator(new AccelerateInterpolator()); //add this
fadeIn.setDuration(800);
fadeIn.setFillEnabled(true);
fadeIn.setFillAfter(true);
bt1.startAnimation(fadeIn);
You can use Interpolators to have a variable speed during animation. Example can be found here.
I think you need two animations with different speeds. After the first has finished, the second gets played.
Animation anim1 = new AlphaAnimation (1, 0.5F);
anim1.setDuration (600); //The slow part.
//set other things for anim1
Animation anim2 = new AlphaAnimation (0.5F, 0);
anim2.setDuration (200); //The fast part.
//set other things for anim2

Reverting animation done with AnimatorSet

I am creating an animation with AnimatorSet and when it ends I would like to leave the View where it was.
The code is something like this:
mLastAnimation = new AnimatorSet();
mLastAnimation.playTogether(
ObjectAnimator.ofFloat(mImageView, "scaleX", 1.5f, 1f),
ObjectAnimator.ofFloat(mImageView, "translationY", 40f, 0f));
mLastAnimation.setDuration(3000);
mLastAnimation.addListener(this);
mLastAnimation.start();
// The Activity implements the AnimatorListener interface
#Override
public void onAnimationEnd(Animator animator) {
// Undo the animation changes to the view.
}
EDIT:
I am using the new animation API so setFillAfter() will not work here.
If you are using nineoldandroids there is a function called reverse. You can set the duration to be 0 and call reverse to reverse the animation.
You have to set the properties of the View back to its original values.
For example if you translateY 40 pixels forward, you need to translateY 40 pixels backwards. This is because the actual properties of the View have changed during property animation, not just the way it is rendered.
http://developer.android.com/guide/topics/graphics/prop-animation.html#property-vs-view
While using ObjectAnimator you can simply set
android:repeatMode="reverse"
You can use this:
ObjectAnimator scaleX = ObjectAnimator.ofFloat(mImageView, "scaleX", 1.5f, 1f);
// here is the important part!
scaleX.setRepeatMode(ValueAnimator.REVERSE);
scaleX.setRepeatCount(1);
ObjectAnimator transl = ObjectAnimator.ofFloat(mImageView, "translationY", 40f, 0f));
// here is the important part!
transl.setRepeatMode(ValueAnimator.REVERSE);
transl.setRepeatCount(1);
mLastAnimation = new AnimatorSet();
mLastAnimation.playTogether(scaleX, transl);
// the rest is the same...

Categories

Resources