游戏中的爆炸实现

本文介绍如何使用Java实现一张75*25图片的爆炸动画效果,并通过线程控制图片显示状态,每150毫秒更新一次画面,直至图片完全消失。

我先准备一张图片了,如上所示:

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值