android Region碰撞
16lz
2021-01-25
- Region是一个类,这个类比较常用的方法就是用于判断一个点是否在矩形区域内,其方法是使是Regions类中的contains(int x, int y)函数创建实例:RegionCollsion
项目运行效果:
MySurfaceView.java
1.2.package com.rectCollsion;3.4.import android.content.Context;5.import android.graphics.Canvas;6.import android.graphics.Color;7.import android.graphics.Paint;8.import android.graphics.Paint.Style;9.import android.graphics.Rect;10.import android.view.KeyEvent;11.import android.view.MotionEvent;12.import android.view.SurfaceHolder;13.import android.view.SurfaceHolder.Callback;14.import android.view.SurfaceView;15.16.public class MySurfaceView extends SurfaceView implements Callback,Runnable{17. //用于控制SurfaceView18. private SurfaceHolder sfh;19. //声明一个画笔20. private Paint paint;21. //声明一个线程22. private Thread th;23. //线程消亡的标志位24. private boolean flag;25. //声明一个画布26. private Canvas canvas;27. //声明屏幕的宽高28. private int screenW,screenH;29. //定义两个矩形的宽高坐标30. private int rectX1 = 10,rectY1 = 10,rectW1 = 40,rectH1 = 40;31. private int rectX2 = 100,rectY2 = 110,rectW2 = 40,rectH2 = 40;32. //便于观察是否发生碰撞设置一个标识位33. private boolean isCollsion;34. //定义第一个矩形的矩形碰撞数组35. private Rect clipRect1 = new Rect(0,0,15,15);36. private Rect clipRect2 = new Rect(rectW1 - 15, rectH1 - 15,rectW1,rectH1);37. private Rect[] arrayRect1 = new Rect[]{clipRect1,clipRect2};38. //定义第二个矩形的矩形碰撞数组39. private Rect clipRect3 = new Rect(0,0,15,15);40. private Rect clipRect4 = new Rect(rectW2 - 15,rectH2 - 15,rectW2,rectH2);41. private Rect[] arrayRect2 = new Rect[]{clipRect3,clipRect4};42. 43. /**44. * SurfaceView初始化函数45. */46. public MySurfaceView(Context context){47. super(context);48. //实例SurfaceHolder49. sfh = this.getHolder();50. //为SurfaceView添加状态监听51. sfh.addCallback(this);52. //实例一个画笔53. paint = new Paint();54. //设置画笔颜色为白色55. paint.setColor(Color.WHITE);56. //设置焦点57. setFocusable(true);58. }59. @Override60. public void surfaceCreated(SurfaceHolder holder) {61. // TODO Auto-generated method stub62. screenW = this.getWidth();63. screenH = this.getHeight();64. flag = true;65. //实例线程66. th = new Thread(this);67. //启动线程68. th.start();69. }70. 71. 72. /**73. * 游戏绘图74. */75. public void myDraw(){76. try {77. canvas = sfh.lockCanvas();78. if(canvas != null){79. //刷屏80. canvas.drawColor(Color.BLACK);81. //判断是否发生了碰撞82. if(isCollsion){//发生碰撞83. paint.setColor(Color.RED);84. paint.setTextSize(20);85. canvas.drawText("Collision!", 0, 30, paint);86. 87. } else{//没发生碰撞88. paint.setColor(Color.WHITE);89. }90. //绘制两个矩形91. canvas.drawRect(rectX1, rectY1, rectX1 + rectW1, rectY1 + rectH1, paint);92. canvas.drawRect(rectX2, rectY2, rectX2 + rectW2, rectY2 + rectH2, paint);93. //----绘制碰撞区为非填充,并设置画笔为白色94. paint.setStyle(Style.STROKE);95. paint.setColor(Color.WHITE);96. //---绘制第一个矩形所有矩形碰撞区域97. for(int i=0; i < arrayRect1.length; i++){98. canvas.drawRect(arrayRect1[i].left + this.rectX1, arrayRect1[i].top + this.rectY1, 99. arrayRect1[i].right + this.rectX1, arrayRect1[i].bottom + this.rectY1, paint);100. }101. //---绘制第二个矩形所有矩形碰撞区域102. for(int i=0;i < arrayRect2.length; i++){103. canvas.drawRect(arrayRect2[i].left + this.rectX2, arrayRect2[i].top + this.rectY2,104. arrayRect2[i].right + this.rectX2,arrayRect2[i].bottom + this.rectY2, paint);105. }106. 107. }108. }catch(Exception e){109. 110. }finally{111. if(canvas != null)112. sfh.unlockCanvasAndPost(canvas);113. }114. }115. /**116. * 触屏事件监听117. */118. @Override119. public boolean onTouchEvent(MotionEvent event) {120. // TODO Auto-generated method stub121. //让矩形1随着触屏位置移动(触屏点设为此矩形的中心点)122. rectX1 = (int) event.getX() - rectW1/2;123. rectY1 = (int) event.getY() - rectH1/2;124. //当矩形之间发生碰撞125. if(isCollsionWithRect(arrayRect1, arrayRect2)){126. isCollsion = true;//设置标志位为真127. //当矩形之间没有发生碰撞128. }else{129. isCollsion = false; //设置标志位为假130. }131. return true;132. }133. /**134. * 矩形碰撞的函数135. * @param left 表示矩形左上角坐标的X坐标136. * @param top 表示矩形左上角坐标Y坐标137. * @param right 表示矩形右下角坐标X坐标138. * @param buttom 表示矩形右下角坐标Y坐标139. */140. public boolean isCollsionWithRect(Rect[] rectArray, Rect[] rect2Array){141. Rect rect = null;142. Rect rect2 = null;143. for(int i = 0; i < rectArray.length; i++){144. //依次取出第一个矩形数组的每个矩形实例145. rect = rectArray[i];146. //获取到第一个矩形数组中每个矩形元素的属性值147. int x1 = rect.left + this.rectX1;148. int y1 = rect.top + this.rectY1;149. int w1 = rect.right - rect.left;150. int h1 = rect.bottom - rect.top;151. for(int j = 0; j < rect2Array.length; j++){152. //依次取出第二个矩形数组的每个矩形实例153. rect2 = rect2Array[i];154. //获取到第二个矩形数组中每个矩形的属性值155. int x2 = rect2.left + this.rectX1;156. int y2 = rect2.top + this.rectY2;157. int w2 = rect2.right - rect2.left;158. int h2 = rect2.bottom - rect2.top;159. //进行循环遍历两个矩形碰撞数组所有元素之间的位置关系160. if(x1 >= x2 && x1 > x2 + w2){161. }else if(x1 <= x2 && x1 + w1 <= x2) {162. }else if(y1 >=y2 && y1 >= y2 + h2){163. }else if(y1 <=y2 && y1 + h1 <= y2){164. }else {165. //只要有一个碰撞矩形数组与另一个碰撞矩形数组发生碰撞则认为碰撞166. return true;167. }168. }169. }170. return false;171. }172.173. 174. /**175. * 按键事件监听176. */177. @Override178. public boolean onKeyDown(int keyCode, KeyEvent event) {179. // TODO Auto-generated method stub180. return super.onKeyDown(keyCode, event);181. }182. /**183. * 游戏逻辑184. */185. private void logic(){186. 187. }188. @Override189. public void run() {190. // TODO Auto-generated method stub191. while(flag){192. long start = System.currentTimeMillis();193. myDraw();194. logic();195. long end = System.currentTimeMillis();196. try {197. if (end - start < 50) {198. Thread.sleep(50 - (end - start));199. }200. } catch(InterruptedException e){201. e.printStackTrace();202. }203. }204. 205. }206. /**207. * Surfaceview视图状态发生改变,响应此函数208. */209. @Override210. public void surfaceChanged(SurfaceHolder holder, int format, int width,211. int height) {212. // TODO Auto-generated method stub213. 214. }215. /**216. * Surfaceview视图消亡,响应此函数217. */218. @Override219. public void surfaceDestroyed(SurfaceHolder holder) {220. // TODO Auto-generated method stub221. 222. }223.}
更多相关文章
- Android(安卓)ScrollView反弹效果的实现
- Android中解析JSON形式的数据
- Android中ViewFlipper详解
- Android(安卓)ListView的OnItemClickListener()参数详解
- 转载:Android(安卓)常用代码集合
- android状态栏右上角增加图标的方法
- Android(安卓)MPChart—柱状图
- android java json与实体互相转换工具
- android开发大作业开发记录(关于制作圆角按钮)