[英]No Rotation in image using rotate Animation
在Android中使用旋轉動畫,但是單擊按鈕后圖像中沒有旋轉。
public class MainActivity extends AppCompatActivity {
ImageView spinImage;
Button buton;
Random r;
int degree =0 , degreeold= 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buton = (Button) findViewById(R.id.spinbutton);
spinImage= (ImageView) findViewById(R.id.spinimage);
r= new Random();
buton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
degreeold = degree % 360;
degree = r.nextInt(3600)+720;
degree= 4400;
RotateAnimation rotate = new RotateAnimation( degreeold, degree,
RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF , 0.5f);
rotate.setDuration(3600);
rotate.setFillAfter(true);
rotate.setInterpolator( new DecelerateInterpolator());
rotate.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
spinImage.setAnimation(rotate);
}
});
}
我無法找到為什么圖像沒有旋轉的錯誤。 運行時沒有錯誤,應用程序打開沒有任何延遲,但是單擊該按鈕時沒有動畫。
您尚未開始動畫。 嘗試使用spinImage.startAnimation(rotate);
如果要延遲啟動動畫或與其他動畫一起啟動,setAnimation旨在為您提供更精細的控制。
從文檔中:
設置要為此視圖播放的下一個動畫。 如果要立即播放動畫,請改用startAnimation(android.view.animation.Animation)。 此方法提供了對開始時間和無效時間的細粒度控制,但是您必須確保1)設置了開始時間的動畫,以及2)當視圖的父對象(控制子動畫的視圖)無效時,動畫應該開始了。
替換spinImage.setAnimation(rotate);
與spinImage.startAnimation(rotate);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.