Question

I have a RotateAnimation running on an imageview object.

This animation makes the object rotate at the rate of (myAnimation.setDuration(x)) per rotation.

I have quite a few of the objects and animations rotating at different rates, all objects set to repeatCount(INFINITY).

I want to be make all of them do their thing for a fixed duration (ie: 3 rotating objects, object 1 does 5 turns, object 2 does 20.33 turns, and object 3 does 0.4 turns).

/e It is also important that the position of each object at the end of the countdown is saved somewhere/returned, as I will start another rotate animation from those coordinates.

Also note, all these rotations are done about each object's own center, so by coordinates I mean degrees!

Anyone have any ideas?

Thanks!

Was it helpful?

Solution

I am fairly new to android but I have had a similar problem so I will attempt to answer your question based on my understanding of what you want to achieve.

So I am thinking if you want your objects to rotate for fixed amount of turns you don't need to rotate them for infinite count. Instead maybe you can rotate each object once for the amount of turns you need with the rate you like. So let's say for argument's sake you want your objects to rotate at rate of 5 seconds per rotation or in other words take 5 seconds to complete one complete 360 degree revolution. I will use as example object 3 from your question that needs to make 0.4 turns:

// define animation for 0.4 turn object
final RotateAnimation rotateObj3_part1 = new RotateAnimation(0, 360*0.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // 360*0.4 = 144 deg
rotateObj3_part1.setDuration((long) (5000*0.4)); // 5 sec for full circle, 2 sec for 0.4 of circle
rotateObj3_part1.setFillAfter(true); // this will make object stay in rotated position
rotateObj3_part1.setRepeatCount(0);

ImageView object3 = (ImageView)findViewById(R.id.object3);
object3.startAnimation(rotateObj3_part1);

Now to rotate the same object more starting from previous position, define new RotateAnimation that starts where old one ended:

final RotateAnimation rotateObj3_part2 = new RotateAnimation(360*0.4f, newEndPoint, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // so now rotate from 144 degrees to newEndPoint degrees
rotateObj3_part2.setDuration((long) (5000*((newEndPoint-360*0.4f))/360); // scale 5 sec to new change in degrees
rotateObj3_part2.setFillAfter(true);
rotateObj3_part2.setRepeatCount(0);
object3.startAnimation(rotateObj3_part2);

I hope this makes sense and gets you the result you want to achieve.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top