Android - 同一个按钮上的系列动画
我有一个带有几个按钮的简单应用程序。 在某些时候,动画会成功地按顺序应用于这些按钮。 然而,在所有动画按钮都是唯一的情况下(IE 按钮 1、2、3),一切正常。
另一方面,如果要设置动画的按钮重复(IE 1,1,2,3,2),则即使我使用不同时间(以秒为单位)的animation.SetStatOffset,相同按钮的动画也会同时开始)。
public void Flash(int delay,int time) {
Animation animation=AnimationUtils.loadAnimation(this.getContext(),R.anim.gametextbuttonrotation);
animation.setStartOffset(delay);
animation.setDuration(time);
this.startAnimation(animation);
}
我试图
private void Flash(List<Integer> sequence)
{
int i=0;
int COOLDOWN=1000;
for (Integer integer : sequence) {
buttons[integer].Flash(i++*COOLDOWN,COOLDOWN/2);
}
}
阅读 cod/google 但没有帮助。
好的,如果有人尝试这样做,解决方案如下:
1)在视图类中创建动画集的局部变量 2)当开始一个新序列时,使用构造函数初始化AnimationSet(AFAIK没有AnimationSet.empty()这样的东西...... 3)在foreach循环中,创建动画然后播放它们
private void flash(List<Integer> sequence)
{
int i=0;
int COOLDOWN=1000;
for(Button button : buttons)
{
button.clearAnimation();
button.animSet=new AnimationSet(); //is inside a private method, here All-in-1 to be simple
}
for (Integer integer : sequence) {
buttons[integer].addFlash(i++*COOLDOWN,COOLDOWN/2);
}
for (Integer integer : sequence) {
buttons[integer].flash();
}
}
I have a simple application with a few buttons.
At some point, animations are successfully applied to those buttons in a sequence.
Hovewer, in the case all the animated buttons are unique (IE buttons 1,2,3) everything works fine.
On the other hand, if the buttons to animate are repeating (IE 1,1,2,3,2), then the animations for the same buttons start at the same time even through i uses animation.SetStatOffset with diferend times (by seconds).
public void Flash(int delay,int time) {
Animation animation=AnimationUtils.loadAnimation(this.getContext(),R.anim.gametextbuttonrotation);
animation.setStartOffset(delay);
animation.setDuration(time);
this.startAnimation(animation);
}
called by
private void Flash(List<Integer> sequence)
{
int i=0;
int COOLDOWN=1000;
for (Integer integer : sequence) {
buttons[integer].Flash(i++*COOLDOWN,COOLDOWN/2);
}
}
I tried to read cod/google but to no help.
OK, if anyone tries to do this heres the solution:
1) create a local variable of AnimationSet in the View Class
2) when starting a new sequence, initialize the AnimationSet by using constructor (AFAIK there is no such thing as AnimationSet.empty().....
3) inside foreach loop, create the animations and then play them
private void flash(List<Integer> sequence)
{
int i=0;
int COOLDOWN=1000;
for(Button button : buttons)
{
button.clearAnimation();
button.animSet=new AnimationSet(); //is inside a private method, here All-in-1 to be simple
}
for (Integer integer : sequence) {
buttons[integer].addFlash(i++*COOLDOWN,COOLDOWN/2);
}
for (Integer integer : sequence) {
buttons[integer].flash();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想着想着。如果您在视图上设置两个动画,第二个动画将覆盖第一个动画。因此,动画“不是同时开始”,只有第二个(有延迟)正在运行。您无法判断它们是同时运行的,因为动画是相同的,也许您应该切换动画以进一步测试您的错误:
在这种情况下,每隔一个动画都会不同。
如果您想在一个视图上运行两个动画,您应该考虑使用动画集。
http://developer.android.com/guide/topics/graphics/view -animation.html
另一方面,没有另一种方式来显示动画:
PS
Java 命名约定 建议以小写字母开头方法名称。
Thinking about it. If you set two animations on a view the second one will just override the first one. Therefore the animations 'aren't starting at the same time' only the second (with delay) is running. You cannot tell that they are running at the same time as the animation is the same, perhaps you should toggle the animation to test your bug further:
In this scenario every second animation will be different.
If you want to run two animations on one view you should look at using an animation set.
http://developer.android.com/guide/topics/graphics/view-animation.html
On a side not there is another way to show an animation:
P.S.
Java naming conventions recommend starting your method names with a lowercase letter.