精灵类:
package com.app.fengactivity;
import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
/**
* @author 作者 E-mail:
* @version 创建时间:2016-6-12 下午3:09:37
* 类说明
*/
public class Sprite {
//图片有四行
private static final int BMP_ROWS = 4;
//图片有三列
private static final int BMP_COLUMNS = 3;
private int x = 0;
private int y = 0;
private int xSpeed = 5;
private int ySpeed = 5;
private GameViewSurface gameView;
private Bitmap bmp;
//当前片断
private int currentFrame = 0;
private int width;
private int height;
Random r;
public Sprite(GameViewSurface gameView, Bitmap bmp) {
this.gameView = gameView;
this.bmp = bmp;
this.width = bmp.getWidth() / BMP_COLUMNS;
this.height = bmp.getHeight() / BMP_ROWS;
//坐标随机参考代码
r = new Random();
x = r.nextInt(gameView.getWidth() - width);
y = r.nextInt(gameView.getHeight() - height);
}
private void update() {
if (x > gameView.getWidth() - width - xSpeed) {
xSpeed = -2;
}
if (x + xSpeed < 0) {
xSpeed = 2;
}
x = x + xSpeed;
if (y > gameView.getHeight() - height - ySpeed) {
ySpeed = -2;
}
if (y + ySpeed < 0) {
ySpeed = 2;
}
y = y + ySpeed;
currentFrame = ++currentFrame % BMP_COLUMNS;
}
private int getAnimationRow(){
double dirDouble = (Math.atan2(xSpeed, ySpeed)) / (Math.PI / 2) + 2;
int direction = (int)Math.round(dirDouble) % BMP_ROWS;
return direction;
}
public void onDraw(Canvas canvas) {
update();
//处理,人物的行走
int srcX = currentFrame * width;
//对于人物的动作先不处理
// int srcY = 1 * height;
//人物动作参考代码
int srcY = getAnimationRow()* height;
//1,画出我们要矩形
Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
//2,画到的具体目的
Rect dst = new Rect(x, y, x + width, y + height);
//3,画出..
canvas.drawBitmap(bmp, src, dst, null);
}
public boolean isCollison(float x2, float y2) {
//判断要消失的精灵位置
return x2 > x && x2 < x +width && y2 > y && y2 < y + height ;
}
}
线程类:
import android.annotation.SuppressLint;
import android.graphics.Canvas;
/**
* @author 作者 E-mail:
* @version 创建时间:2016-6-12 下午3:09:04
* 类说明
*/
public class GameThread extends Thread {
private GameViewSurface view;
private boolean running = false;
public GameThread(GameViewSurface view) {
this.view = view;
}
public void setRunning(boolean run) {
running = run;
}
@SuppressLint("WrongCall")
@Override
public void run() {
while (running) {
Canvas c = null;
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
}
}
}
自定义surfaceview:
package com.app.fengactivity;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
/**
* @author 作者 E-mail:
* @version 创建时间:2016-6-12 下午3:08:04
* 类说明
*/
@SuppressLint("WrongCall")
public class GameViewSurface extends SurfaceView {
//1,声明我们的Bitmap对象
private Bitmap bitmap;
//2,声明一个holder对象
private SurfaceHolder holder;
//3,声明用于线程循环的对象
private GameThread gameThread;
//4,声明我们的精灵,下一章节用的
private List<Sprite> sprites = new ArrayList<Sprite>();
//5,初始我们的变量
public GameViewSurface(Context context) {
super(context);
// TODO Auto-generated constructor stub
gameThread = new GameThread(this);
holder = getHolder();
//用于调用运行的线程
holder.addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//1退出时终止我们的run方法
boolean retry = true;
gameThread.setRunning(false);
//2等待线程的终止
while (retry) {
try {
gameThread.join();
retry = false;
} catch (InterruptedException e) {
Log.d("sur", "gua le ");
}
}
}
//添加我们的绘图线程
@Override
public void surfaceCreated(SurfaceHolder holder) {
//1,创建精灵
createSprites();
//2,开始我们的线程
gameThread.setRunning(true);
gameThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
});
}
public GameViewSurface(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
gameThread = new GameThread(this);
holder = getHolder();
//用于调用运行的线程
holder.addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//1退出时终止我们的run方法
boolean retry = true;
gameThread.setRunning(false);
//2等待线程的终止
while (retry) {
try {
gameThread.join();
retry = false;
} catch (InterruptedException e) {
Log.d("sur", "gua le ");
}
}
}
//添加我们的绘图线程
@Override
public void surfaceCreated(SurfaceHolder holder) {
//1,创建精灵
createSprites();
//2,开始我们的线程
gameThread.setRunning(true);
gameThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
});
}
//创建精灵方法
private void createSprites() {
sprites.add(createSprite(R.drawable.start3));
}
private Sprite createSprite(int resouce) {
bitmap = BitmapFactory.decodeResource(getResources(), resouce);
return new Sprite(this, bitmap);
}
//绘图方法
protected void onDraw(Canvas canvas){
//把背景画成黑色
canvas.drawColor(Color.BLACK);
//画出所有精灵
for (Sprite sprite : sprites) {
sprite.onDraw(canvas);
}
}
long lastClick;
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
// 当时间间隔太少不执行以下方法
//点了精灵消失的方法
//1,控制最后点击的时间
if(System.currentTimeMillis() - lastClick > 2000){
lastClick = System.currentTimeMillis();
synchronized (getHolder()) {
float tX = event.getX();
float tY = event.getY();
for(int i = sprites.size() -1; i >=0;i--){
Sprite sprite = sprites.get(i);
if(sprite.isCollison(tX,tY)){
//精灵消失
sprites.remove(i);
break;
}
}
}
}
return true;
}
}
布局文件:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.app.fengactivity.GameViewSurface
android:id="@+id/gameview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>