久违的写一篇博客
看了几篇关于动画的博客,尝试写一个让控件围绕Y轴进行旋转的动画(其实就是在别人的基础上修改的)
Activity和布局文件就不写了
public class TestAnimation extends Animation {
private int mCenterWidth, mCenterHeight;
private Camera mCamera = new Camera();
private float mRotateY = 0.0f;
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
//设置动画长度
setDuration(5000);
//动画结束后保持原样
setFillAfter(true);
mCenterWidth = width /2;
mCenterHeight = height /2;
}
public void setRotateY(float rotateY){
mRotateY=rotateY;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
//获取矩阵
final Matrix matrix=t.getMatrix();
mCamera.save();
//使用camera设置旋转角度
mCamera.rotateY(mRotateY * interpolatedTime);
//将旋转做用于矩阵
mCamera.getMatrix(matrix);
mCamera.restore();
// 在旋转之后开始位移动画
matrix.postTranslate(mCenterWidth, mCenterHeight);
// 在旋转之前开始位移动画
matrix.preTranslate(-mCenterWidth, -mCenterHeight);
}
}
附上我看的那篇博客地址http://www.cnblogs.com/wondertwo/p/5295976.html
4/28更新_____________________
昨天发现使用属性动画更加简单。。。
ObjectAnimator animator = ObjectAnimator.ofFloat(rlText, "rotationY", 0f, 180f).setDuration(2000);
animator.start();