`
onewayonelife
  • 浏览: 252393 次
  • 性别: Icon_minigender_1
  • 来自: 太原
社区版块
存档分类
最新评论

游戏角色在屏幕行走

 
阅读更多

MySurfaceView

package org.wp.activity;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * canvas.save() 和  canvas.restore()是两个相互匹配出现的,
 * 作用是用来保存画布的状态和取出保存的状态的。
 * 
 * 这里稍微解释一下,
 * 当我们对画布进行旋转,缩放,平移等操作的时候其实我们是想对特定的元素进行操作,
 * 比如图片,一个矩形等,但是当你用canvas的方法来进行这些操作的时候,
 * 其实是对整个画布进行了操作,那么之后在画布上的元素都会受到影响,
 * 所以我们在操作之前调用canvas.save()来保存画布当前的状态,
 * 当操作之后取出之前保存过的状态,这样就不会对其他的元素进行影响
 * 
 * 代码段1:
 * public void draw() { 
 * 		Canvas canvas = sfh.lockCanvas(); 
 * 		canvas.drawColor(Color.BLACK); 
 * 		canvas.drawBitmap(bmp1, 0,0,paint); 
 * 		canvas.save(); 
 * 	 	canvas.scale(1.5f, 1.5f); 
 * 		canvas.restore(); 
 * 		canvas.drawBitmap(bmp2, 0,0,paint); 
 *  	sfh.unlockCanvasAndPost(canvas);
 * }
 * 
 * 代码段2:
 * 
 * public void draw() { 
 * 	 	Canvas canvas = sfh.lockCanvas();
 * 		canvas.drawColor(Color.BLACK); 
 * 		canvas.drawBitmap(bmp1, 0,0,paint); 
 * 		canvas.scale(1.5f, 1.5f); 
 * 		canvas.drawBitmap(bmp2, 0,0,paint);
 * 		sfh.unlockCanvasAndPost(canvas); 
 * }
 * 
 * 上面这两个代码片段中我们都假设有两张图片 bmp1和bmp2,并且都画在画布上!
 * 那么代码段1和代码段2的不同:
 * 
 * 代码段1中我们进行画布缩放的之前保存了画布状态,
 * 做了缩放操作之后又取出之前保存的状态,
 * 这样做是为了保证bmp2正常画出来不受到缩放的影响!
 * 
 * 代码段2里,画了bmp1后就执行了缩放操作,并且没有保存状态!
 * 紧接着画了bmp2,那么bmp2也会一样受到缩放的影响!!
 * 
 * 所以我们如果单独处理一张图片的时候,而且不想影响其他部分的绘制,那么应该如下来做:
 * 
 * public void draw() { 
 * 		Canvas canvas = sfh.lockCanvas();
 * 		canvas.drawColor(Color.BLACK); 
 * 		canvas.drawBitmap(bmp1, 0,0,paint); 
 * 		canvas.save(); 
 * 		canvas.scale(1.5f, 1.5f); 
 * 		canvas.drawBitmap(bmp2, 0,0,paint); 
 * 		canvas.restore(); 
 * 		sfh.unlockCanvasAndPost(canvas); 
 * }
 * 
 * 
 * @author wp
 * 
 */
public class MySurfaceView extends SurfaceView implements
		SurfaceHolder.Callback, Runnable {
	private static final String TAG = "MySurfaceView";

	private SurfaceHolder sfh;
	private Resources res;
	private Bitmap bmp;
	private Paint p1;
	private Paint p2;
	private int SH, SW;
	private Canvas canvas;

	private int bmp_x = 100, bmp_y = 100; // 初始坐标位置
	private boolean UP, DOWN, LEFT, RIGHT; // 按键类型标识
	private int animation_up[] = { 3, 4, 5 }; // 向上图片位置
	private int animation_down[] = { 0, 1, 2 }; // 向下图片位置
	private int animation_left[] = { 6, 7, 8 }; // 向左图片位置
	private int animation_right[] = { 9, 10, 11 }; // 向右图片位置
	private int animation_init[] = animation_down; // 默认人物图片向下
	private int frame_count; // 人物对应数组位置

	public MySurfaceView(Context context) {
		super(context);
		this.setKeepScreenOn(true); // 保持屏幕常亮
		/*
		 * 此方法是用来响应按键!如果是自己定义一个继承自View的类,
		 * 重新实现onKeyDown方法后,只有当该View获得焦点时才会调用onKeyDown方法,
		 * Actvity中的onKeyDown方法是当所有控件均没有处理该按键事件时,才会调用.
		 */
		this.setFocusable(true);
		sfh = this.getHolder();
		sfh.addCallback(this);
		res = this.getResources();
		bmp = BitmapFactory.decodeResource(res, R.drawable.enemy1);
		p1 = new Paint();
		p1.setColor(Color.YELLOW);
		p1.setAntiAlias(true);
		p2 = new Paint();
		p2.setColor(Color.RED);
		p2.setAntiAlias(true);
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Log.i(TAG, "surfaceCreated()");
		SW = this.getWidth();
		SH = this.getHeight();
		Log.i(TAG, "SW:" + SW + " SH:" + SH);
		new Thread(this).start();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		Log.i(TAG, "surfaceChanged()");
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.i(TAG, "surfaceDestroyed()");
	}

	@Override
	public void run() {
		while (true) {
			draw();
			cycle();
			try {
				Thread.sleep(100);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void draw() {
		Log.i(TAG, "draw()");
		canvas = sfh.lockCanvas();
		/*
		 * 这里也是对屏幕进行刷屏操作,其实这也只是一种, 
		 * 如果用到drawRGB的方法同样实现,当然也可以用fillRect等来刷屏。
		 * 在继承view中,因为onDraw方法是系统自动调用的, 
		 * 不像在surfaceview这里这样去在run里面自己去不断调用,
		 * 在view中我们可以使用invalidate()/postInvalidate()
		 * 这两种方法实现让系统调用onDraw方法,这里也是和surfaceview中的不同之一!
		 */
		canvas.drawRect(0, 0, SW, SH, p1);
		canvas.save();
		canvas.drawText("wp", bmp_x - 1, bmp_y - 10, p2); // 图片上方人物名称
		canvas.clipRect(bmp_x, bmp_y, bmp_x + bmp.getWidth() / 13, bmp_y + bmp.getHeight()); // 裁剪图片人物
		if (animation_init == animation_up) {
			canvas.drawBitmap(bmp, bmp_x - animation_up[frame_count] * (bmp.getWidth() / 13), bmp_y, p1);
		} else if (animation_init == animation_down) {
			canvas.drawBitmap(bmp, bmp_x - animation_down[frame_count] * (bmp.getWidth() / 13), bmp_y, p1);
		} else if (animation_init == animation_left) {
			canvas.drawBitmap(bmp, bmp_x - animation_left[frame_count] * (bmp.getWidth() / 13), bmp_y, p1);
		} else if (animation_init == animation_right) {
			canvas.drawBitmap(bmp, bmp_x - animation_right[frame_count] * (bmp.getWidth() / 13), bmp_y, p1);
		}
		canvas.restore();
		sfh.unlockCanvasAndPost(canvas);
	}

	private void cycle() {
		Log.i(TAG, "cycle()");
		if (DOWN) {
			bmp_y += 5;
		} else if (UP) {
			bmp_y -= 5;
		} else if (LEFT) {
			bmp_x -= 5;
		} else if (RIGHT) {
			bmp_x += 5;
		}
		// 按键的行为发生,循环图片位置
		if (UP || DOWN || LEFT || RIGHT) {
			if (frame_count < 2) {
				frame_count++;
			} else {
				frame_count = 0;
			}
		}
		// 用户松开按键,恢复图片位置
		if (UP == false && DOWN == false && LEFT == false && RIGHT == false) {
			frame_count = 0;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
			if (UP == false) {
				animation_init = animation_up;
			}
			UP = true;
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
			if (DOWN == false) {
				animation_init = animation_down;
			}
			DOWN = true;
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
			if (LEFT == false) {
				animation_init = animation_left;
			}
			LEFT = true;
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
			if (RIGHT == false) {
				animation_init = animation_right;
			}
			RIGHT = true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (UP) {
			UP = false;
		} else if (DOWN) {
			DOWN = false;
		} else if (LEFT) {
			LEFT = false;
		} else if (RIGHT) {
			RIGHT = false;
		}
		return super.onKeyUp(keyCode, event);
	}
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics