而我代碼中的ondraw 也好 draw 也好,都是我自己定義的一個方法。。。放在執行緒中不斷調用的,一定要注意!!
其實上一篇分析surfaceview的文章就是一個簡單的遊戲框架了,當然這裡再強調一下,簡單的遊戲框架,所以不要 高手們不要亂噴~
這個Demo是給一童鞋寫的一個對圖片操作以及按鍵處理,遊戲簡單框架的一個demo,這裡放出給大家分享~
備註1
- 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
- }
- }
此方法是用來回應按鍵!如果是自己定義一個繼承自View的類,重新實現onKeyDown方法後,只有當該View獲得焦點時才會調用onKeyDown方法,Actvity中的onKeyDown方法是當所有控制項均沒有處理該按鍵事件時,才會調用.
備註2
這裡也是對螢幕進行刷屏操作,其實這也只是一種,之前文章裡我也用到drawRGB的方法同樣實現,當然也可以用fillRect等來刷屏。
那麼這裡我想說下,在繼承view中,因為onDraw方法是系統自動調用的,不像在surfaceview這裡這樣去在run裡面自己去不斷調用,在view中我們可以抵用 invalidate()/postInvalidate() 這兩種方法實現讓系統調用onDraw方法,這裡也是和surfaceview中的不同之一!
備註3
這裡canvas.save();和canvas.restore();是兩個相互匹配出現的,作用是用來保存畫布的狀態和取出保存的狀態的。這裡稍微解釋一下,當我們對畫布進行旋轉,縮放,平移等操作的時候其實我們是想對特定的元素進行操作,比如圖片,一個矩形等,但是當你用canvas的方法來進行這些操作的時候,其實是對整個畫布進行了操作,那麼之後在畫布上的元素都會受到影響,所以我們在操作之前調用canvas.save()來保存畫布當前的狀態,當操作之後取出之前保存過的狀態,這樣就不會對其他的元素進行影響。
對於 canvas.save();和canvas.restore(); 還有不少童鞋不懂,OK、我再補充點:
程式碼片段1:
上面這兩個代碼片段中我們都假設有兩張圖片 bmp1和bmp2,並且都畫在畫布上!程式碼片段2:
- 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);
- }
- 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);
- }
那麼程式碼片段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);
- }
沒有留言:
張貼留言