/**
* 操作游戏中的人物
*
* @time 上午11:27:35
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
@SuppressWarnings("unused")
public class PlaySurfaceView extends SurfaceView implements Callback, Runnable {
private int screenX, screenY;
private SurfaceHolder sfh;
private Thread thread;
private Canvas canvas;
private boolean flag;
private Paint paint;
private Bitmap person;// 人物图片
private final int DIR_LEFT = 0;
private final int DIR_RIGHT = 1;
// 人物的方向(默认是向右)
private int dir = DIR_RIGHT;
private int person_x, person_y;// 人物的位置坐标
private boolean isUp, isDown, isLeft, isRight;// 处理按键卡现象
private int currentFream;// 当前帧
public PlaySurfaceView(Context context) {
super(context);
sfh = this.getHolder();
sfh.addCallback(this);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
this.setFocusable(true);
// 获得人物图片
person = BitmapFactory.decodeResource(this.getResources(), R.drawable.robot);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenX = this.getWidth();
screenY = this.getHeight();
flag = true;
thread = new Thread(this);
thread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}
@SuppressWarnings("static-access")
@Override
public void run() {
while (flag) {
try {
long start = System.currentTimeMillis();
// 绘图
draw();
// 游戏逻辑
logic();
long end = System.currentTimeMillis();
if (end - start < 50) {
thread.sleep(50 - (end - start));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 绘制图形
*/
private void draw() {
try {
canvas = sfh.lockCanvas();
if (null != canvas) {
canvas.drawColor(Color.WHITE);
// 绘制帧动画
drawFream(currentFream, canvas, paint);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != canvas) {
sfh.unlockCanvasAndPost(canvas);
}
}
}
/**
* 绘制帧动画
*
* @param paint
* 画笔
* @param canvas
* 画布
* @param currentFream
* 当前帧
*/
private void drawFream(int currentFream, Canvas canvas, Paint paint) {
// 取得上帧图片的高和宽
int freamW = person.getWidth() / 6;
int freamH = person.getHeight() / 2;
// 位图每列的帧数
int FreamRowNum = person.getWidth() / freamW;
// 得到帧相对于位图的坐标
// x
int x = currentFream % FreamRowNum * freamW;
// y
int y = currentFream / FreamRowNum * freamH;
canvas.save();
// 绘制与人物相同大小的可视区域 (默认是向右移动)
canvas.clipRect(person_x, person_y, person_x + freamW, person_y + freamH);
// 如果是向左移动
if (dir == DIR_LEFT) {
// 镜向反转
canvas.scale(-1, 1, person_x + freamW, person_y + freamH);
}
// 绘图
canvas.drawBitmap(person, person_x - x, person_y - y, paint);
canvas.restore();
}
/**
* 游戏逻辑
*/
private void logic() {
// 移动动画
if (isUp) {
person_y -= 5;
}
if (isDown) {
person_y += 5;
}
if (isLeft) {
person_x -= 5;
}
if (isRight) {
person_x += 5;
}
// 跑动动画
currentFream++;
if (currentFream >= 12) {
currentFream = 0;
}
}
/********************** 按键事件临听 *************************/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
// 向上
case KeyEvent.KEYCODE_DPAD_UP:
isUp = true;
break;
// 向下
case KeyEvent.KEYCODE_DPAD_DOWN:
isDown = true;
break;
// 向左
case KeyEvent.KEYCODE_DPAD_LEFT:
isLeft = true;
dir = DIR_LEFT;
break;
// 向右
case KeyEvent.KEYCODE_DPAD_RIGHT:
isRight = true;
dir = DIR_RIGHT;
break;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
// 向上
case KeyEvent.KEYCODE_DPAD_UP:
isUp = false;
break;
// 向下
case KeyEvent.KEYCODE_DPAD_DOWN:
isDown = false;
break;
// 向左
case KeyEvent.KEYCODE_DPAD_LEFT:
isLeft = false;
break;
// 向右
case KeyEvent.KEYCODE_DPAD_RIGHT:
isRight = false;
break;
}
return super.onKeyUp(keyCode, event);
}
/*********************************************************/
}
android游戏开发学习笔之八 (学习书籍 Android游戏编程之从零开始)
最新推荐文章于 2020-08-21 09:48:20 发布