I'd like to make simple animation in Android project. I've got an image in my activity:
<ImageView
android:id="@+id/pointer_png"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:src="@drawable/pointer_400" />
And here is my onClick method in activity class:
public void onStartButtonClick(View view){
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new LinearInterpolator());
animationSet.setFillAfter(true);
RotateAnimation anim = new RotateAnimation(0.0f, -45.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim.setDuration(4000);
animationSet.addAnimation(anim);
RotateAnimation anim2 = new RotateAnimation(0.0f, 90.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim2.setDuration(4000);
animationSet.addAnimation(anim2);
RotateAnimation anim3 = new RotateAnimation(0.0f, -135.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim3.setDuration(4000);
animationSet.addAnimation(anim3);
RotateAnimation anim4 = new RotateAnimation(0.0f, 180.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim4.setDuration(4000);
animationSet.addAnimation(anim4);
final ImageView pointer = (ImageView) findViewById(R.id.pointer_png);
pointer.startAnimation(animationSet);
}
Unfortunately, the effect is unexpected. I'd like to rotate image in this sequence:
- Rotate 180 degrees within 4 seconds.
- Rotate -135 degrees within next 4 seconds.
- Rotate 90 degrees within next 4 seconds.
- Rotate -45 degrees within last 4 seconds.
But with this code animation is definitely shorter than 16 seconds and it's composed of one part only - 90 degrees from 0 point and it's over. Probably AnimationSet checks all animations and counts last position in my sequence. I've tried to set AnimationSet(false) and add separate LinearInterpolator to each RotateAnimation but it doesn't work.
What should I do to make my animation longer and with all rotations separated (4 steps, 4 seconds for every step)?
From my experience an AnimationSet does not always work as expected and can be a pain in the a**. I would try using ViewPropertyAnimator.
Here is an example on how to use it. You can either set a startDelay like this :
or set an AnimationListener and start the next Animation in onAnimationEnd() when the one before has finished.
Spontaneously, if I had to do this I'd write my own method, something like this (not tested) :
and then call it four times like this :