李华明Himi 原创,转载务必在明显处注明:
不少童鞋说个人代码运行后,点击home或者back后会程序异常,若是你也这样遇到过,那么你确定没有仔细读完Himi的博文,第十九篇Himi专门写了关于这些错误的缘由和解决方法,这里我在博客都补充说明下,省的童鞋们总疑惑这一块;请点击下面联系进入阅读:css
【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!html
各位童鞋请大家注意:surfaceview中确实有 onDraw这个方法,可是surfaceview不会本身去调用!!!java
而我代码中的ondraw 也好 draw 也好,都是我本身定义的一个方法。。。放在线程中不断调用的,必定要注意!!android
其实上一篇分析surfaceview的文章就是一个简单的游戏框架了,固然这里再强调一下,简单的游戏框架,因此不要高手们不要乱喷~canvas
这个Demo是给群里一童鞋写的一个对图片操做以及按键处理,游戏简单框架的一个demo,这里放出给你们分享~框架
package com.himi; 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; import android.view.SurfaceHolder.Callback; public class MySurfaceView extends SurfaceView implements Callback, Runnable { private Thread th = new Thread(this); private SurfaceHolder sfh; private int SH, SW; private Canvas canvas; private Paint p; private Paint p2; private Resources res; private Bitmap bmp; 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); res = this.getResources(); bmp = BitmapFactory.decodeResource(res, R.drawable.enemy1); sfh = this.getHolder(); sfh.addCallback(this); p = new Paint(); p.setColor(Color.YELLOW); p2 = new Paint(); p2.setColor(Color.RED); p.setAntiAlias(true); setFocusable(true); //备注1 } public void surfaceCreated(SurfaceHolder holder) { SH = this.getHeight(); SW = this.getWidth(); th.start(); } public void draw() { canvas = sfh.lockCanvas(); canvas.drawRect(0, 0, SW, SH, p); //备注2 canvas.save(); //备注3 canvas.drawText("Himi", bmp_x-2, 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, p); } else if (animation_init == animation_down) { canvas.drawBitmap(bmp, bmp_x - animation_down[frame_count] * (bmp.getWidth() / 13), bmp_y, p); } else if (animation_init == animation_left) { canvas.drawBitmap(bmp, bmp_x - animation_left[frame_count] * (bmp.getWidth() / 13), bmp_y, p); } else if (animation_init == animation_right) { canvas.drawBitmap(bmp, bmp_x - animation_right[frame_count] * (bmp.getWidth() / 13), bmp_y, p); } canvas.restore(); //备注3 sfh.unlockCanvasAndPost(canvas); } public void 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 (DOWN || UP || LEFT || RIGHT) { if (frame_count < 2) { frame_count++; } else { frame_count = 0; } } if (DOWN == false && UP == false && LEFT == false && RIGHT == false) { frame_count = 0; } } @Override public boolean onKeyDown(int key, KeyEvent event) { if (key == KeyEvent.KEYCODE_DPAD_UP) { if (UP == false) { animation_init = animation_up; } UP = true; } else if (key == KeyEvent.KEYCODE_DPAD_DOWN) { if (DOWN == false) { animation_init = animation_down; } DOWN = true; } else if (key == KeyEvent.KEYCODE_DPAD_LEFT) { if (LEFT == false) { animation_init = animation_left; } LEFT = true; } else if (key == KeyEvent.KEYCODE_DPAD_RIGHT) { if (RIGHT == false) { animation_init = animation_right; } RIGHT = true; } return super.onKeyDown(key, event); } /* (non-Javadoc) * @see android.view.View#onKeyUp(int, android.view.KeyEvent) */ @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (DOWN) { DOWN = false; } else if (UP) { UP = false; } else if (LEFT) { LEFT = false; } else if (RIGHT) { RIGHT = false; } return super.onKeyUp(keyCode, event); } @Override public void run() { // TODO Auto-generated method stub while (true) { draw(); cycle(); try { Thread.sleep(100); } catch (Exception ex) { } } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } }
备注1ide
此方法是用来响应按键!若是是本身定义一个继承自View的类,从新实现onKeyDown方法后,只有当该View得到焦点时才会调用onKeyDown方法,Actvity中的onKeyDown方法是当全部控件均没有处理该按键事件时,才会调用.post
备注2this
这里也是对屏幕进行刷屏操做,其实这也只是一种,以前文章里我也用到drawRGB的方法一样实现,固然也能够用fillRect等来刷屏。spa
那么这里我想说下,在继承view中,由于onDraw方法是系统自动调用的,不像在surfaceview这里这样去在run里面本身去不断调用,在view中咱们能够抵用 invalidate()/postInvalidate() 这两种方法实现让系统调用onDraw方法,这里也是和surfaceview中的不一样之一!
备注3
这里canvas.save();和canvas.restore();是两个相互匹配出现的,做用是用来保存画布的状态和取出保存的状态的。这里稍微解释一下,
当咱们对画布进行旋转,缩放,平移等操做的时候其实咱们是想对特定的元素进行操做,好比图片,一个矩形等,可是当你用canvas的方法来进行这些操做的时候,实际上是对整个画布进行了操做,那么以后在画布上的元素都会受到影响,因此咱们在操做以前调用canvas.save()来保存画布当前的状态,当操做以后取出以前保存过的状态,这样就不会对其余的元素进行影响
对于 canvas.save();和canvas.restore(); 还有很多童鞋不懂,OK、我再补充点:
代码段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); }
(推荐你们订阅本博客,由于咱的更新速度但是很快的~娃哈哈)
源码下载地址: http://www.himigame.com/android-game/298.html