好的,问题来了 我的 Activity 中有一个 ImageView,这是它在 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 并具有弹跳效果。
我已经用我的代码实现了这个:
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)
然而,图像并没有像它应该的那样移动,而是停在 100 处然后弹跳......
嗯....,你们知道哪里出了问题或者我应该怎么做才能做到这一点吗?
最佳答案
如果我没记错的话,您正在拍摄一系列动画。
有趣的是,一旦启动了一个AnimationSet,所有添加的动画都会同时运行,而不是按顺序运行;因此,您需要为第一个动画之后的每个动画设置 setStartOffset(long offSet)。
也许这样的事情会奏效......
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 - 在 android 中使用 animationSet() 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6355495/