【发布时间】:2016-08-24 10:25:07
【问题描述】:
我想在 Android 项目中制作简单的动画。我的活动中有一张图片:
<ImageView
android:id="@+id/pointer_png"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:src="@drawable/pointer_400" />
这是我在活动类中的 onClick 方法:
public void onStartButtonClick(View view){
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new LinearInterpolator());
animationSet.setFillAfter(true);
RotateAnimation anim = new RotateAnimation(0.0f, -45.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim.setDuration(4000);
animationSet.addAnimation(anim);
RotateAnimation anim2 = new RotateAnimation(0.0f, 90.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim2.setDuration(4000);
animationSet.addAnimation(anim2);
RotateAnimation anim3 = new RotateAnimation(0.0f, -135.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim3.setDuration(4000);
animationSet.addAnimation(anim3);
RotateAnimation anim4 = new RotateAnimation(0.0f, 180.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim4.setDuration(4000);
animationSet.addAnimation(anim4);
final ImageView pointer = (ImageView) findViewById(R.id.pointer_png);
pointer.startAnimation(animationSet);
}
很遗憾,效果出乎意料。我想按这个顺序旋转图像:
- 在 4 秒内旋转 180 度。
- 在接下来的 4 秒内旋转 -135 度。
- 在接下来的 4 秒内旋转 90 度。
- 在最后 4 秒内旋转 -45 度。
但是用这个代码动画肯定短于 16 秒,它只由一个部分组成 - 从 0 点开始 90 度,它就结束了。 AnimationSet 可能会检查所有动画并计算我序列中的最后一个位置。我尝试设置 AnimationSet(false) 并向每个 RotateAnimation 添加单独的 LinearInterpolator,但它不起作用。
我应该怎么做才能使我的动画更长并且所有旋转都分开(4 步,每步 4 秒)?
【问题讨论】:
-
尝试使用
setStartOffset()为您的其他动画添加起始偏移量。 -
我尝试过使用偏移量:0、4000、8000 和 12000,但最终效果非常意外且出乎意料。恐怕我需要为每个动画设置 AnimationListener 并覆盖 onAnimationEnd 方法,然后开始下一个动画。这个序列没问题,但是更复杂的动画呢?
标签: android animation rotation android-animation rotateanimation