在android中使用animationSet()制作动画 [英] Animation with animationSet() in android

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

问题描述

好的,问题来了我的活动中有一个 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...., 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,所有添加的动画都会同时运行,而不是按顺序运行;因此您需要为第一个动画之后的每个动画设置StartOffset(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天全站免登陆