Android 让游戏人物在屏幕上显示

移动开发 Android
主要讲就是在surfaceview中有 onDraw这个方法,但这个方法surfaceview不会自己去调用,而我们的代码中ondraw也好draw也好,都是我们自己去定义的一个方法,也就是说我慢把它放在线程中不断的去调用,下面的代码主要是一个对图片操作和按键的处理,那么我们就来看看在代码中是怎么样来实现的吧!

Java代码:

  1. package eoe.demo;   
  2. import android.content.Context;   
  3. import android.content.res.Resources;   
  4. import android.graphics.Bitmap;   
  5. import android.graphics.BitmapFactory;   
  6. import android.graphics.Canvas;   
  7. import android.graphics.Color;   
  8. import android.graphics.Paint;   
  9. import android.util.Log;   
  10. import android.view.KeyEvent;   
  11. import android.view.SurfaceHolder;   
  12. import android.view.SurfaceView;   
  13. import android.view.SurfaceHolder.Callback;   
  14. public class MySurfaceView extends SurfaceView implements Callback, Runnable {   
  15. private Thread th = new Thread(this);   
  16. private SurfaceHolder sfh;   
  17. private int SH, SW;   
  18. private Canvas canvas;   
  19. private Paint p;   
  20. private Paint p2;   
  21. private Resources res;   
  22. private Bitmap bmp;   
  23. private int bmp_x = 100bmp_y = 100;   
  24. private boolean UP, DOWN, LEFT, RIGHT;   
  25. private int animation_up[] = { 3, 4, 5 };   
  26. private int animation_down[] = { 0, 1, 2 };   
  27. private int animation_left[] = { 6, 7, 8 };   
  28. private int animation_right[] = { 9, 10, 11 };   
  29. private int animation_init[] = animation_down;   
  30. private int frame_count;   
  31. public MySurfaceView(Context context) {   
  32. super(context);   
  33. this.setKeepScreenOn(true);   
  34. res = this.getResources();   
  35. bmp = BitmapFactory.decodeResource(res, R.drawable.enemy1);   
  36. sfh = this.getHolder();   
  37. sfh.addCallback(this);   
  38. p = new Paint();   
  39. p.setColor(Color.YELLOW);   
  40. p2 = new Paint();   
  41. p2.setColor(Color.RED);   
  42. p.setAntiAlias(true);   
  43. setFocusable(true); //备注1   
  44. }   
  45. public void surfaceCreated(SurfaceHolder holder) {   
  46. SH = this.getHeight();   
  47. SW = this.getWidth();   
  48. th.start();   
  49. }   
  50. public void draw() {   
  51. canvas = sfh.lockCanvas();   
  52. canvas.drawRect(0, 0, SW, SH, p); //备注2   
  53. canvas.save(); //备注3   
  54. canvas.drawText("Himi", bmp_x-2, bmp_y-10, p2);   
  55. canvas.clipRect(bmp_x, bmp_y, bmp_x + bmp.getWidth() / 13, bmp_y+bmp.getHeight());   
  56. if (animation_init == animation_up) {   
  57. canvas.drawBitmap(bmp, bmp_x - animation_up[frame_count] * (bmp.getWidth() / 13), bmp_y, p);   
  58. } else if (animation_init == animation_down) {   
  59. canvas.drawBitmap(bmp, bmp_x - animation_down[frame_count] * (bmp.getWidth() / 13), bmp_y, p);   
  60. } else if (animation_init == animation_left) {   
  61. canvas.drawBitmap(bmp, bmp_x - animation_left[frame_count] * (bmp.getWidth() / 13), bmp_y, p);   
  62. } else if (animation_init == animation_right) {   
  63. canvas.drawBitmap(bmp, bmp_x - animation_right[frame_count] * (bmp.getWidth() / 13), bmp_y, p);   
  64. }   
  65. canvas.restore(); //备注3   
  66. sfh.unlockCanvasAndPost(canvas);   
  67. }   
  68. public void cycle() {   
  69.      if (DOWN) {   
  70.      bmp_y += 5;   
  71. } else if (UP) {   
  72.     bmp_y -5;   
  73. } else if (LEFT) {   
  74.     bmp_x -5;   
  75. } else if (RIGHT) {   
  76.     bmp_x += 5;   
  77. }   
  78. if (DOWN || UP || LEFT || RIGHT) {   
  79. if (frame_count < 2) {   
  80. frame_count++;   
  81. } else {   
  82.    frame_count = 0;   
  83. }   
  84. }   
  85. if (DOWN == false && UP == false && LEFT == false && RIGHT == false) {   
  86.     frame_count = 0;   
  87. }   
  88. }   
  89. @Override   
  90. public boolean onKeyDown(int key, KeyEvent event) {   
  91. if (key == KeyEvent.KEYCODE_DPAD_UP) {   
  92. if (UP == false) {   
  93.     animation_init = animation_up;   
  94. }   
  95.   UP = true;   
  96. } else if (key == KeyEvent.KEYCODE_DPAD_DOWN) {   
  97. if (DOWN == false) {   
  98.    animation_init = animation_down;   
  99. }   
  100.    DOWN = true;   
  101. } else if (key == KeyEvent.KEYCODE_DPAD_LEFT) {   
  102. if (LEFT == false) {   
  103.    animation_init = animation_left;   
  104. }   
  105. LEFT = true;   
  106. } else if (key == KeyEvent.KEYCODE_DPAD_RIGHT) {   
  107.  if (RIGHT == false) {   
  108.     animation_init = animation_right;   
  109. }   
  110. RIGHT = true;   
  111. }   
  112. return super.onKeyDown(key, event);   
  113. }   
  114. /* (non-Javadoc)   
  115. * @see android.view.View#onKeyUp(int, android.view.KeyEvent)   
  116. */   
  117. @Override   
  118. public boolean onKeyUp(int keyCode, KeyEvent event) {   
  119. if (DOWN) {   
  120.   DOWN = false;   
  121. } else if (UP) {   
  122.   UP = false;   
  123. } else if (LEFT) {   
  124.   LEFT = false;   
  125. } else if (RIGHT) {   
  126.   RIGHT = false;   
  127. }   
  128. return super.onKeyUp(keyCode, event);   
  129. }   
  130. @Override   
  131. public void run() {   
  132. // TODO Auto-generated method stub   
  133. while (true) {   
  134. draw();   
  135. cycle();   
  136. try {   
  137. Thread.sleep(100);   
  138. } catch (Exception ex) {   
  139. }   
  140. }   
  141. }   
  142. @Override   
  143. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {   
  144. // TODO Auto-generated method stub   
  145. }   
  146. @Override   
  147. public void surfaceDestroyed(SurfaceHolder holder) {   
  148. // TODO Auto-generated method stub   
  149. }   
  150. </STRONG> 

备注1 

此方法是用来响应按键!如果是自己定义一个继承自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()来保存画布当前的状态,当操作之后取出之前保存过的状态,这样就不会对其他的元素进行影响

【编辑推荐】

Android软件游戏汉化教程

八款开源Android游戏引擎

手机游戏行家:制作出iOS精品游戏的关键点

资深游戏设计师解析手机游戏开发的四个层次

责任编辑:zhaolei 来源: 开发者社区
相关推荐

2012-09-10 09:46:19

JavaScriptJSHTML5

2012-05-15 13:10:57

HTML5

2015-07-20 16:30:14

Windows 10Xbox One游戏

2012-09-03 09:21:51

2018-02-09 15:23:35

PC游戏手游Moonlight

2022-01-27 14:26:11

LinuxUbuntu游戏

2012-09-17 09:32:16

JavaScriptJSHTML5

2012-08-22 14:39:11

电视手机显示屏

2022-06-02 14:01:54

屏幕阅读器NVDAWindows

2013-05-21 14:22:29

Android游戏开发捕获屏幕双击事件

2013-05-21 14:15:23

Android游戏开发屏幕分辨率

2011-05-31 14:21:13

2015-10-14 10:02:33

ClojureScri Android

2017-08-15 19:20:51

AndroidHttpServer

2018-11-01 10:20:26

完全修复Windows 10Windows

2009-07-17 13:46:52

.Net Micro

2018-11-06 09:24:55

LinuxLutries命令

2021-01-22 14:03:34

Flutter系统鸿蒙

2011-08-31 17:04:26

2010-04-28 17:54:07

aix系统
点赞
收藏

51CTO技术栈公众号