有两种效果,下边进行详细介绍:
效果一:以自身x轴中轴为中心,左右以20°晃动
/**
* 晃动动画
* <p>
* 那么CycleInterpolator是干嘛用的??
* Api demo里有它的用法,是个摇头效果!
*
* @param counts 1秒钟晃动多少下
* @return Animation
*/
public static Animation shakeAnimation(int counts) {
Animation rotateAnimation = new RotateAnimation(0, 20, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new CycleInterpolator(counts));
rotateAnimation.setRepeatCount(-1);
rotateAnimation.setDuration(3000);
return rotateAnimation;
}
效果二:以自身x轴中轴为中心,左右平移
/**
* 晃动动画
* <p>
* 那么CycleInterpolator是干嘛用的??
* Api demo里有它的用法,是个摇头效果!
*
* @param counts 1秒钟晃动多少下
* @return Animation
*/
public static Animation shakeAnimation(int counts) {
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setRepeatCount(100000);
translateAnimation.setDuration(1000);
return translateAnimation;
}
调用方式:
imageView.setAnimation(shakeAnimation(6));
参考文章:https://blog.csdn.net/pmqiujun/article/details/48627943
网友评论