1
我想在Android項目中製作簡單的動畫。我在我的活動得到了一個形象:在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()'將起始偏移添加到其他動畫中。 – TR4Android
我已經嘗試了偏移量:0,4000,8000和12000,但最終效果非常好,並且出乎意料。我擔心我需要爲每個動畫設置AnimationListener並覆蓋onAnimationEnd方法,然後開始下一個動畫。這個序列可以,但是更復雜的動畫是什麼? – maniek099