Android 不支持Path或者Shape的animation,只能用Frame来模拟。定义好每一帧所需要画的内容即可。
但是有一点Drawable不支持动画启动和结束的callback事件。其实也挺简单,我们重载Drawable的onDraw方法,在那里触发callback事件。
代码片段:
public class CallbackableShapeDrawable extends ShapeDrawable{
public Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public Path path;
public boolean isLastFrame = false;
public boolean isFirstFrame = false;
public AnimationCallBack callBack;
protected void onDraw(Shape s, Canvas c, Paint p) {
if(isFirstFrame && callBack != null){
callBack.animationStarted(this);
}
c.drawPath(path, paint);
if(isLastFrame && callBack != null){
callBack.animationEnded(this);
}
}
public abstract static class AnimationCallBack {
public abstract void animationStarted(CallbackableShapeDrawable drawable);
public abstract void animationEnded(CallbackableShapeDrawable drawable);
}
}