/**
* 左右摇摆动画类
*
*/
public class CustomRotateAnimextends Animation {
/**
* 控件宽
*/
private int mWidth;
/**
* 控件高
*/
private int mHeight;
/**
* 实例
*/
private static CustomRotateAnimrotateAnim;
/**
* 获取动画实例
*
* @return 实例
*/
public static CustomRotateAnim getCustomRotateAnim() {
if (null ==rotateAnim) {
rotateAnim =new CustomRotateAnim();
}
return rotateAnim;
}
/**
* @param width 控件的宽
* @param height 控件的高
* @param parentWidth
* @param parentHeight
*/
@Override
public void initialize(int width,int height,int parentWidth,int parentHeight) {
this.mWidth = width;
this.mHeight = height;
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// 左右摇摆
// 获取矩阵
Matrix matrix = t.getMatrix();
// 设置旋转
/**
* 参数一:旋转度数
* 参数二:X旋转度数
* 参数三:Y旋转度数
*
*/
matrix.setRotate((float) (Math.sin(interpolatedTime * Math.PI *2) *40),mWidth /3,mHeight /2);
super.applyTransformation(interpolatedTime, t);
}
}
//我在activity的onResume()方法里加载动画
@Override
protected void onResume() {
super.onResume();
mThread =new Thread(new Runnable() {
@Override
public void run() {
try {
for (; ; ) {
Thread.sleep(4000);
runOnUiThread(new Runnable() {
@Override
public void run() {
showAnimation();
}
});
}
}catch (InterruptedException e) {
e.printStackTrace();
}
}
});
mThread.start();
}
// 项目中的效果
private void showAnimation() {
// 获取自定义动画实例
CustomRotateAnim rotateAnim = CustomRotateAnim.getCustomRotateAnim();
// 一次动画执行1秒
rotateAnim.setDuration(450);
// 设置为循环播放
rotateAnim.setRepeatCount(Animation.RESTART);
// 设置为匀速
rotateAnim.setInterpolator(new LinearInterpolator());
// 开始播放动画
img_money.startAnimation(rotateAnim);
}