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

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

Related

Android object animation not working smoothly

I am using ObjectAnimator to move the position of the RelativeLayout from center of the screen to above.The animation works on the RelativeLayout,but it does not move smoothly.It jumps between the start and end position.
Code:
ObjectAnimator animY = ObjectAnimator.ofFloat(logolayout, "y", 350f);
animY.setInterpolator(new LinearInterpolator());
animY.setDuration(1500);
animY.start();
Use ViewPropertyAnimator instead.
animate(view).y(350f).setInterpolator(new LinearInterpolator()).setDuration(1500).start();
Just remove the following:
animY.setInterpolator(new LinearInterpolator());

How to use setDelay for animation within an AnimatorSet?

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();

Fade in and out an image constantly in Android app

I'm trying to make an image fade in and out constantly, but it just fade in and out one time.
How can I make it repeat constantly?
Here's the code:
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);
ImageView loading = (ImageView)findViewById(R.id.loading);
loading.startAnimation(animation);
With Animator, it's pretty easy :
Animator alphaAnimator = ObjectAnimator.ofFloat(loading, View.ALPHA, 0f, 1f);
alphaAnimator.setDuration(1000);
alphaAnimator.setRepeatMode(ValueAnimator.REVERSE);
alphaAnimator.setRepeatCount(ValueAnimator.INFINITE);
alphaAnimator.start();
You should repeat your animation:
animation.setRepeatCount(Animation.INFINITE);
Try to use the method setRepeatMode(int repeatMode) from Animation class.

Animation set for image view

I want multiple animations to be played on the same ImageView. I'm using an animation set but it never fades in. But it does rotate. Can somebody tell me what I'm missing ?
AnimationSet s = new AnimationSet(false);//false mean dont share interpolators
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeInDuration);
iv.setAnimation(fadeIn);
iv.startAnimation(fadeIn);
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(800);
s.addAnimation(fadeIn);
s.addAnimation(anim);
iv.startAnimation(s);
You are missing:
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeInDuration);
fadeIn.setFillEnabled(true); // to apply setFillBefore and setFillAfter
fadeIn.setFillBefore(true); // it means that at start of animation you set alpha="0"
fadeIn.setFillAfter(false);
iv.setAnimation(fadeIn);
iv.startAnimation(fadeIn);
Also, in your styles you must remove alpha="0"
AlphaAnimation is working on your image as it is. So if you set alpha="0" it works between this 0. If alpha="1" or without it (when style default is alpha="1"), it works between 0-1.

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