有兩種效果,下邊進行詳細介紹:
效果一:以自身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;
}
調(diào)用方式:
imageView.setAnimation(shakeAnimation(6));
參考文章:https://blog.csdn.net/pmqiujun/article/details/48627943