主要有三小點需要注意:
- 1.旋轉動畫設定好圓心
- 2.延遲一秒後再執行動畫
- 3.使用
執行動畫,不然第二次會無效view.startAnimation(animation);
private ImageView imageView;
private Handler handler;
private RotateAnimation animation;
/**
* 給 view 設定動畫
*/
public void showAnimal(ImageView imageView, Handler handler) {
this.imageView = imageView;
this.handler = handler;
handler.postDelayed(hideRunnable, 300);
}
/**
* 停止view動畫
*/
public void stopAnimal() {
if (imageView != null) {
imageView.clearAnimation();
if (animation != null) {
animation.cancel();
}
if (handler != null) {
handler.removeCallbacksAndMessages(null);
}
}
}
private final Runnable hideRunnable = new Runnable() {
@Override
public void run() {
if (animation == null) {
animation = new RotateAnimation(0f, -10f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);
animation.setDuration(200);//設定動畫持續時間
animation.setInterpolator(new LinearInterpolator());//不停頓
animation.setRepeatMode(ValueAnimator.REVERSE);//重新從頭執行
//animation.setRepeatCount(ValueAnimator.INFINITE);//設定重複次數
animation.setRepeatCount(1);//設定重複次數
animation.setAnimationListener(new AnimationListenerAdapter() {
@Override
public void onAnimationEnd(Animation animation) {
if (handler != null) {
handler.removeCallbacks(hideRunnable);
handler.postDelayed(hideRunnable, 1000);
}
}
});
if (imageView != null) {
imageView.setAnimation(animation);
}
}
if (imageView != null) {
imageView.startAnimation(animation);
}
}
};
複制