android自定义动画步骤
继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransformation方法进行相应的轨阵变化即可!
参数讲解
initialize(int width, int height, int parentWidth, int parentHeight) width和height代表指定播放动画的View控件宽高,parentWidth和parentHeight代表该View控件所在的父控件宽高。
applyTransformation(float interpolatedTime, Transformation t) 其中的interpolatedTime参数都会改变一次,值从0到1递增,当interpolatedTime的值为1时则动画结束。 Transformatio类是一个变换的矩阵,通过改变该矩阵就可以实现各种复杂的效果。
applyTransformation()方法是动画具体的实现方法,在系统绘制动画时会反复调用这个方法,每调用一次
实例代码
在**自定义折叠布局**中就是用了自定义动画。
下面我们看一个实现左右旋转摇摆的例子
/**
* 左右摇摆动画
*/
public class RotateAnim extends Animation {
/** 控件宽 */
private int mWidth;
/** 控件高 */
private int mHeight;
/** 实例 */
private static RotateAnim rotateAnim;
/**
* 获取动画实例
* @return 实例
*/
public static RotateAnim getRotateAnim() {
if (null == rotateAnim) {
rotateAnim = new RotateAnim();
}
return rotateAnim;
}
//获取视图的宽高
@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) {
// 左右摇摆
t.getMatrix().setRotate((float)(Math.sin(interpolatedTime*Math.PI*2)*50), mWidth/2, mHeight/2);
super.applyTransformation(interpolatedTime, t);
}
}
外部调用
RotateAnim rotateAnim = new RotateAnim.getRotateAnim();
rotateAnim .setDuration(3000);
startAnimation(rotateAnim );