Android动画实现(一)
自定义View实现动画
原理:使用View的postDelayed方法实现延迟重绘操作。在重绘操作中每次绘制不同的图形就能实现动画。
代码如下:
public class CircleAnim extends View {
private Paint mPaint;
private static final int sColor = 0x33668843;
private int mRadius;
private int mMaxRadius;
private int mY;
public CircleAnim(Context context) {
super(context);
initView();
}
public CircleAnim(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView();
}
public CircleAnim(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
public void initView(){
Log.e("zxx", "initView");
mPaint = new Paint();
mPaint.setColor(sColor);
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
mMaxRadius = wm.getDefaultDisplay().getWidth();
mY = wm.getDefaultDisplay().getHeight();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.e("zxx","onDraw");
//绘制不同大小圆形
canvas.drawCircle(mMaxRadius, mY / 2, mRadius, mPaint);
if (mRadius < mMaxRadius * 1.3){
mRadius = mRadius + 100;
postDelayed(new Runnable() {
@Override
public void run() {
//重绘
invalidate();
}
}, 100);
}else{
mRadius = 0;
postDelayed(new Runnable() {
@Override
public void run() {
invalidate();
},100);
}
}
}
- 动画效果如下