我先准备一张图片了,如上所示:
public class ExplodeView extends View {
Bitmap bitmap = null;
Rect src = null;
Rect dst = null;
private int x1 = 50;
private int y1 = 0;
private int x2 = 75;
private int y2 = 25;
public ExplodeView(Context context) {
super(context);
bitmap = BitmapFactory.decodeResource(this.getResources(),
R.drawable.explode2);
src = new Rect(x1, y1, x2, y2);
dst = new Rect(0, 0, 25, 25);
}
@Override
protected void onDraw(final Canvas canvas) {
canvas.drawBitmap(bitmap, src, dst, null);
postInvalidate();//重新绘制画面
if (t == null) {
t = new Thread() {
@Override
public void run() {
while (x1 > 0 && x2 > 25) {
try {
Thread.sleep(150);
//我的图片是75*25的图片,平均分为三部分,没三分之一就是一个爆炸时的状态
canvas.drawBitmap(bitmap, src, dst, null);
x1 -= 25;
x2 -= 25;
src = new Rect(x1, y1, x2, y2);
postInvalidate();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
}
}
Thread t = null;
}