[英]Android RotateAnimation not working as expected
我想以這種方式為imageView設置動畫:(角度符號定義為三角法)
使用下面的代碼,我得到:
所以,根本不是我想要的!
有誰看到如何解決這個問題?
謝謝 !
這是我的代碼:
float angle = 45f;
RotateAnimation rotateAnimation1 = new RotateAnimation(0, -angle,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation1.setStartOffset(0);
rotateAnimation1.setDuration(2000);
rotateAnimation1.setInterpolator(new LinearInterpolator());
RotateAnimation rotateAnimation2 = new RotateAnimation(-angle, angle,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// animationSet.addAnimation(rotateAnimation);
rotateAnimation2.setStartOffset(0);
rotateAnimation2.setDuration(4000);
rotateAnimation2.setInterpolator(new LinearInterpolator());
RotateAnimation rotateAnimation3 = new RotateAnimation(angle, -angle,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation3.setStartOffset(4000);
rotateAnimation3.setDuration(4000);
rotateAnimation3.setInterpolator(new LinearInterpolator());
final AnimationSet animSet1 = new AnimationSet(true);
animSet1.setFillEnabled(true);
animSet1.addAnimation(rotateAnimation1);
final AnimationSet animSet2 = new AnimationSet(true);
// animSet2.setFillEnabled(true);
animSet2.addAnimation(rotateAnimation2);
animSet2.addAnimation(rotateAnimation3);
animSet1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
view.startAnimation(animSet2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animSet2.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
view.startAnimation(animSet2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
view.startAnimation(animSet1);
視圖返回零的原因是視圖的rotation屬性不會更改。 您應該在第一個動畫結束后將旋轉設置為視圖,或者使用ObjectAnimator.ofFloat(imageview ,"rotation", 0f, 360f);
默認設置視圖的旋轉。
1.從0到-45的平滑旋轉:反轉角度符號和setFillAfter(true)
使視圖保持結束動畫位置
RotateAnimation rotateAnimation1 = new RotateAnimation(0, 45f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation1.setFillAfter(true); //This keeps view at animation ended position
rotateAnimation1.setStartOffset(0);
rotateAnimation1.setDuration(2000);
rotateAnimation1.setInterpolator(new LinearInterpolator());
2.從-45到+45的平滑旋轉:將動畫起始角度設置為0,因為視圖已經旋轉。
// Since view already is at -45 position due to rotateAnimation1.setFillAfter(true), now start point is 0 again
RotateAnimation rotateAnimation2 = new RotateAnimation(0, -90,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation2.setFillAfter(true);
rotateAnimation2.setStartOffset(0);
rotateAnimation2.setDuration(4000);
rotateAnimation2.setInterpolator(new LinearInterpolator());
其余步驟遵循上述邏輯是直截了當的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.