如何动态调整吐温动画在Android上的时间/速度 [英] How to dynamically adjust the duration/speed of a Tween Animation in Android

查看:259
本文介绍了如何动态调整吐温动画在Android上的时间/速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个Android应用程序,并在该应用程序我想在一个L形动画一球的图形。这需要我来定义吐温动画,我创建一个动画的XML文件(以下写的),并将其应用到一个视图,它有球的图形在里面。

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>

现在可以说,我想控制这个动画的动态的速度,这样日积月累的L动画进入更快的速度越来越快。我怎样才能动态地控制这整个动画的速度?我曾尝试myAnimation.setDuration($ VAR),但这似乎只工作在动画的第一部分。动画已经偏移大于0的任何部分不具有它的持续时间由setDuration()方法进行调整。

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.

有谁知道我能均匀扩展的多值存取动画的方式?

感谢

PS - 我知道有围绕解决此例如创建2部动画和独立缩放每个部分或生成使用code动画的方式,但如果有一个简单的解决方案,那么这将是preferred。

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.

推荐答案

我对我自己解决了这个。要缩放整个动画,你需要以编程方式创建动画,当您设置动画时长使用 setDuration(); 传递,而不是硬编码在XML是一个变量。

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.

现在每次我打电话给我的动画时间,我通过它的动画时长 mAnimDuration ,然后创建即时新的时间标度的动画。你会发现动画的第二部分的偏移量被设置为动画的第一部分的持续时间。

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.

下面是我的code的例子:

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);

这篇关于如何动态调整吐温动画在Android上的时间/速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆