canvas教程

《飞机大战》安卓游戏开发源码(二)(3)

字号+ 作者:H5之家 来源:H5之家 2018-02-09 14:05 我要评论( )

import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff.Mode; import android.view.SurfaceHolde

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.view.SurfaceHolder;

import com.pm_road.fireair.bean.Bullet;

/**
* 爆炸线程
*
* @ClassName: BoomRunnable
* @Description: 爆炸线程
* @author lipeng
* @date 2014-11-5 下午02:43:39
*
*/
public class BoomRunnable implements Runnable {

private Bullet bullet;// 当前的子弹

private Context context;// 上下文

private SurfaceHolder holder;


public BoomRunnable(Context context,SurfaceHolder holder) {
super();
this.context = context ;
this.holder = holder;
}

@Override
public void run() {

Canvas canvas = holder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);

Paint paint = new Paint();
paint.setColor(Color.BLUE);
//碰撞效果
canvas.drawLine(bullet.getX()-10, 2, bullet.getX()-15, 10, paint);
canvas.drawLine(bullet.getX()-15, 10, bullet.getX()-10, 12, paint);
canvas.drawLine(bullet.getX()-10, 12, bullet.getX()-13, 20, paint);
canvas.drawLine(bullet.getX()-13, 20, bullet.getX(), 12, paint);
canvas.drawLine(bullet.getX(), 12, bullet.getX()+13, 20, paint);
canvas.drawLine(bullet.getX()+13, 20, bullet.getX()+10, 12, paint);
canvas.drawLine(bullet.getX()+10, 12, bullet.getX()+15, 10, paint);
canvas.drawLine(bullet.getX()+15, 10, bullet.getX()+10, 2, paint);
holder.unlockCanvasAndPost(canvas);
}

public Bullet getBullet() {
return bullet;
}

public void setBullet(Bullet bullet) {
this.bullet = bullet;
}


}

写完实体类和线程之后,就应该把他和安卓的xml 进行结合。结合代码:

本文章属于原创性文章,珍惜他人劳动成果,转载请注明出处:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评