I am trying to play several animations in a row, one after the other. These animations will be either simple transforms (moving imageviews a certain distance) or more complex frame animations.
In researching the issue, AnimatorSet seemed to be a good solution at first. It allows you to make a queue of animations to be played, and then execute them in a particular order.
// defining an example animation
Animator anim = ObjectAnimator.ofFloat(v, "alpha", 0f)
.setDuration(100);
AnimatorSet set = new AnimatorSet();
// dictating the order in which the animations will be played
set.playSequentially(anim, skewAnim, wobbleAnim);
set.start();
The issue is that, as far as I can tell, you cannot define an Animator object to put in the AnimatorSet from an animation which is defined in an xml file.
Also, AnimationSet (notice different name) only allows you to play several animations all at once, so this class would not work for my purposes.
Does anybody know of a way to define an Animator object from an xml object in your project? Alternatively, is there possibly another way to go about playing a sequence of animations altogether?
UPDATE:
AnimatorSet set = new AnimatorSet();
Animator anim1 = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.sample);
anim1.setTarget(myView1);
Animator anim2 = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.sample);
anim2.setTarget(myView2);
anim1.setDuration(3000);
anim2.setDuration(3000);
anim1.setInterpolator(new CycleInterpolator(5));
anim2.setInterpolator(new CycleInterpolator(5));
set.playSequentially(anim1,anim2);
set.start();
To load animations into AnimatorSet from xml, your xml should contain AnimatorObjects and not animations. Then you can use AnimatorInflater to load those animatorObjects into your AnimatorSet like this:
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(getActivity(), R.animator.sample);
xml would like something like this:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially"
>
<objectAnimator
android:propertyName="alpha"
android:duration="500"
android:valueTo="1f"
/>
<objectAnimator
android:propertyName="y"
android:duration="500"
android:valueTo="300"
android:valueType="intType"/>
</set>
Refer this link for more detail on AnimatorInflater
For playing sequential animations on different views you can use AnimationListeners.
anim1.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
// start anim2 from here
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With