Android游戏编程_人物移动Demo

1,SurfaceView的使用

2,实现view内部相应键盘的方法OnKeyDown,OnKeyUp事件

3,Canvas中drawClip方法

4,保存画布,恢复画布的运用 (Cavan.save,canvas.restore)

package mystyle.com;

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

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable{
    private SurfaceHolder holder;
    private Paint p,p1;
    private Bitmap bmp;
    private int bmpx = 100,bmpy = 100;
    private int tileW;
    private int tileH;
    private Thread td = new Thread(this);
    private int SH,SW;
    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 currentAnimation[] = animation_down;
    private int frameIndex = 0;
    private Canvas canvas;
	public MySurfaceView(Context context) {
		super(context);
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.enemy1);
		tileW = bmp.getWidth()/13;
		tileH = bmp.getHeight();
		holder = getHolder();
		holder.addCallback(this);
		p = new Paint();
		p.setColor(Color.YELLOW);
		p1 = new Paint();
		p1.setColor(Color.RED);
		p.setAntiAlias(true); //设置抗锯齿
		this.setFocusable(true);//此view获得到焦点才可以处理keyEvent事件
	}
    private void doDraw(){
    	canvas = holder.lockCanvas();
    	canvas.drawRect(0,0,SW,SH, p); //相当于清屏
    	canvas.save();
    	canvas.drawText("style", bmpx-10, bmpy-5, p1);
    	canvas.clipRect(bmpx,bmpy,bmpx+tileW,bmpy+tileH); //裁剪出一个矩形区域 其他地方是不显示的
    	if(currentAnimation == animation_up){
    	  canvas.drawBitmap(bmp, bmpx - animation_up[frameIndex]*tileW, bmpy, p);
    	}else if(currentAnimation == animation_down){
    		canvas.drawBitmap(bmp, bmpx - animation_down[frameIndex]*tileW, bmpy, p);
    	}else if(currentAnimation == animation_left){
    		canvas.drawBitmap(bmp, bmpx - animation_left[frameIndex]*tileW, bmpy, p);
    	}else if(currentAnimation == animation_right){
    		canvas.drawBitmap(bmp, bmpx-animation_right[frameIndex]*tileW,bmpy, p);
    	}
    	canvas.restore();//恢复画布
    	holder.unlockCanvasAndPost(canvas);
    }
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}
    private void doFrame(){
    	if(UP && bmpy > 5){
    		bmpy -= 5;
    	}else if(DOWN && bmpy<SH-5-tileH){
    		bmpy += 5;
    	}else if(LEFT && bmpx>5){
    		bmpx -= 5;
    	}else if(RIGHT && bmpx<SW-5-tileW){
    		bmpx += 5;
    	}
    	if(UP || DOWN || LEFT || RIGHT){
    		if(frameIndex < 2){
    			frameIndex ++;
    		}else{
    			frameIndex = 0;
    		}
    	}
    	if(!UP && !DOWN && !LEFT && !RIGHT){
    		frameIndex = 0;
    	}
    }
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_DPAD_UP){
			if(UP == false){
				currentAnimation = animation_up;
			}
			UP = true;
		}else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
			if(DOWN == false){
				currentAnimation = animation_down;
			}
			DOWN = true;
		}else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
			if(LEFT == false){
				currentAnimation = animation_left;
			}
			LEFT = true;
		}else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
			if(RIGHT == false){
				currentAnimation = animation_right;
			}
			RIGHT = true;
		}
		// TODO Auto-generated method stub
		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;
		}
		// TODO Auto-generated method stub
		return super.onKeyUp(keyCode, event);
	}
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		SH = this.getHeight();
		SW = this.getWidth();
		System.out.println("sh, sw : "+ SH+" , "+SW);
		td.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			doDraw();
			doFrame();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

 注:canvas.save()和canvas.restore()方法往往是匹配使用的,当我们需要对画布中的某一个元素绘制时,使用canvas是对整个画布进行绘制,这样就不能达到对某个特定元素进行绘制的要求,我们可以先用canvas.sava对画布进行保存,然后再对特定的元素进行平移、缩放、旋转等操作,最后再用canvas.restore恢复画布,就不会影响我们绘制特定元素了。

转载于:https://www.cnblogs.com/randy-1989/archive/2012/02/26/2368816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值