How to dynamically adjust the duration/speed of a Tween Animation in Android

℡╲_俬逩灬. 提交于 2019-12-02 04:57:46

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!