主要有三小点需要注意:
- 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);
}
}
};