Android中带有animationSet()的动画 [英] Animation with animationSet() in android

查看:193
本文介绍了Android中带有animationSet()的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是问题
i在我的活动中有一个ImageView,这是它在main.xml中的外观:

OK here's the problem i have an ImageView in my activity, here's what it looks in main.xml:

<ImageView  
android:id="@+id/ic"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/icon"
android:layout_gravity="center_horizontal"/>

我希望此图像先移动-200(左),然后移动到100(右),然后再返回到0并具有弹跳效果。

I want this image to move -200(left) and then to 100(right) and then back to 0 with bouncing effect.

我已使用我的代码实现了此操作:

I've implement this with my code:

as = new AnimationSet(true);
as.setFillEnabled(true);
as.setInterpolator(new BounceInterpolator());

TranslateAnimation ta = new TranslateAnimation(-300, 100, 0, 0); 
ta.setDuration(2000);
as.addAnimation(ta);

AnimationSet sa = new AnimationSet(true);
sa.setFillEnabled(true);
sa.setInterpolator(new DecelerateInterpolator());

TranslateAnimation ta2 = new TranslateAnimation(100, 0, 0, 0); 
ta2.setDuration(2000);
sa.addAnimation(ta2);

as.addAnimation(sa);

您可以在代码中看到我想要的X过渡(-300,100)然后(100,0 )

you can see at the code the X transition that i want (-300,100) then (100, 0)

但是,图像并没有像应有的那样移动,而是停在100处然后弹跳...

however, the image doesn't move like it should, instead it just stop at 100 and then bouncing...

hmmm ....,你们知道发生了什么错误或应该怎么做吗?

hmmm...., do you guys know what is wrong or what should i do to accomplish this?

推荐答案

如果我没记错的话,您是在拍摄一系列动画。

If I'm not mistaking, you're shooting for a sequence of animations.

有趣的是,一旦启动AnimationSet,所有添加的动画将同时运行,并且不按顺序因此,您需要为第一个动画之后的每个动画设置setStartOffset(long offSet)。

Interestingly, once you start an AnimationSet, all the animations added are ran simultaneously and not sequentially; therefore you need to setStartOffset(long offSet) for each animation that follows the first animation.

也许类似的方法会起作用...

Maybe something like this will work...

as = new AnimationSet(true);
as.setFillEnabled(true);
as.setInterpolator(new BounceInterpolator());

TranslateAnimation ta = new TranslateAnimation(-300, 100, 0, 0); 
ta.setDuration(2000);
as.addAnimation(ta);

TranslateAnimation ta2 = new TranslateAnimation(100, 0, 0, 0); 
ta2.setDuration(2000);
ta2.setStartOffset(2000); // allowing 2000 milliseconds for ta to finish
as.addAnimation(ta2);

这篇关于Android中带有animationSet()的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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