Let's say I have an android app, and in that app I want to animate a graphic of a ball in an "L" shape. This would require me to define a Tween Animation where I create an animation xml file (written below) and apply it to a View which has the ball graphic in it.
<translate>
Ydelta = 20;
offset = 0;
duration = 100;
</translate>
<translate>
Xdelta = 20;
offset = 100;
duration = 100;
</translate>
Now lets say I want to control the speed of this animation dynamically so that over time, the L animation goes faster and faster and faster. How can I dynamically control the speed of this whole animation? I have tried myAnimation.setDuration($var) but this only seems to work on the first part of the animation. Any part of the animation that has an offset greater than 0 doesn't have its duration adjusted by the setDuration() method.
Does anyone know a way that I can scale a multi-setp animation uniformly?
Thanks
ps - I know there are ways around solving this such as creating a 2 part animation and scaling each part independently or generating an animation using code, but if there's a simpler solution then that would be preferred.
I've solved this on my own. To scale the whole animation you need to programmatically create the animations, and when you set the animation duration using setDuration();
you pass it a variable, instead of hardcoding in xml.
Now each time I call my animation, I pass it the animation duration mAnimDuration
and then create the new time scaled animation on-the-fly. You'll notice that the offset of the second part of the animation is set to the duration of the first part of the animation.
Here is an example from my code:
AnimationSet rootSet = new AnimationSet(true); TranslateAnimation transX = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, 0,0,0,0); transX.setStartOffset(0); transX.setDuration(mAnimDuration/2); rootSet.addAnimation(transX); TranslateAnimation transX2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, 0,0,0,0); transX2.setStartOffset(mAnimDuration/2); transX2.setDuration(mAnimDuration/2); rootSet.addAnimation(transX2);
来源:https://stackoverflow.com/questions/3619663/how-to-dynamically-adjust-the-duration-speed-of-a-tween-animation-in-android