Android自定义View实现游戏摇杆键盘的方法示例

Android自定义View实现游戏摇杆键盘的方法示例,第1张

概述前言本文主要给大家介绍的是关于Android自定义View实现游戏摇杆键盘的相关内容,为什么会有这篇文章呢?因为在之前的一个项目, *** 作方向的方式为上下左右,左上需要同时按住左键和右键的方式进行 *** 作。

前言

本文主要给大家介绍的是关于AndroID自定义view实现游戏摇杆键盘的相关内容,为什么会有这篇文章呢?因为在之前的一个项目, *** 作方向的方式为上下左右,左上需要同时按住左键和右键的方式进行 *** 作。

如下图:

近来需要升级项目, *** 作方式改为类似王者荣耀的摇杆 *** 作。

如下图:


好了,下面话不多说了,跟着小编来一起看看是如何实现的吧。

绘制背景

实现遥感按钮,需要绘制背景,绘制中心的遥感按钮。绘制遥感背景,需要创建一个RemoteVIEwBg类,存储背景图,减少重复创建bitmap。

RemoteVIEwBg类代码如下:

public class RemoteVIEwBg {private Bitmap bitmapBg;public RemoteVIEwBg(Bitmap bitmap) { bitmapBg = bitmap;}//背景的绘图函数public voID draw(Canvas canvas,Paint paint,Rect src0,Rect dst0 ) { canvas.drawBitmap(bitmapBg,src0,dst0,paint);}}

点击触摸事件

重写系统的触摸时间,判断触摸点在背景范围内还是背景范围外

 @OverrIDepublic boolean ontouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() ==  MotionEvent.ACTION_MOVE) { //   // 在范围外触摸  if (Math.sqrt(Math.pow((bigCircleX - (int) event.getX()),2) + Math.pow((bigCircleY - (int) event.getY()),2)) >= bigCircleR) {   double tempRad = geTrad(bigCircleX,bigCircleY,event.getX(),event.getY());   getXY(bigCircleX,bigCircleR,tempRad);  } else {//范围内触摸   smallCircleX = (int) event.getX();   smallCircleY = (int) event.getY();  } } else if (event.getAction() == MotionEvent.ACTION_UP) {  smallCircleX = bigCircleX;  smallCircleY = bigCircleY; } return true;}

弧度计算

通过 event.getX() ,event.getY()获得当前的触摸点,与圆点进行计算,获取弧度

/*** * 得到两点之间的弧度 */public float geTrad(float px1,float py1,float px2,float py2) { float x = px2 - px1; float y = py1 - py2; //斜边的长 float z = (float) Math.sqrt(Math.pow(x,2) + Math.pow(y,2)); float cosAngle = x / z; float rad = (float) Math.acos(cosAngle); if (py2 < py1) {  rad = -rad; } return rad;}

图形绘制

通过 canvas.drawCircle()canvas.drawBitmap()分别进行遥感按钮和遥感背景的绘制,注意对遥感背景的保存,如果在绘制的时候每次BitmapFactory.decodeResource()会增加耗时,因此只需在surfaceCreated()中进行bitmap的生成即可。

public voID draw() { try {  canvas = sfh.lockCanvas();  canvas.drawcolor(getResources().getcolor(R.color.ghostwhite)); // 指定图片绘制区域(左上角的四分之一)  Rect src = new Rect(0,bitmap.getWIDth(),bitmap.getHeight());  // 指定图片在屏幕上显示的区域  Rect dst = new Rect(bigCircleX - bigCircleR,bigCircleY - bigCircleR,bigCircleX + bigCircleR,bigCircleY + bigCircleR);  // 绘制图片  remoteVIEwBg.draw(canvas,paint,src,dst);  paint.setcolor(0x70ff0000);  //绘制摇杆  canvas.drawCircle(smallCircleX,smallCircleY,smallCircleR,paint); } catch (Exception e) {  // Todo: handle exception } finally {  try {   if (canvas != null)    sfh.unlockCanvasAndPost(canvas);  } catch (Exception e2) {   e2.printstacktrace();  } }}

使用

在activity中动态添加

 relativeLayout relativeLayout = (relativeLayout) findVIEwByID(R.ID.dance_relative_layout); remoteSurfaceVIEw = new RemoteSurfaceVIEw(this); params = new relativeLayout.LayoutParams(relativeLayout.LayoutParams.MATCH_PARENT,relativeLayout.LayoutParams.MATCH_PARENT); remoteSurfaceVIEw.setLayoutParams(params); relativeLayout.addVIEw(remoteSurfaceVIEw);

全部代码

public class RemoteSurfaceVIEw extends SurfaceVIEw implements Callback,Runnable {private float scale = this.getResources().getdisplayMetrics().density;private Thread th;private SurfaceHolder sfh;private Canvas canvas;private Paint paint;private boolean flag;private int bigCircleX = 0;private int bigCircleY =0;private int bigCircleR = 0;//摇杆的X,Y坐标以及摇杆的半径private float smallCircleX = 0;private float smallCircleY = 0;private float smallCircleR = 0;private Bitmap bitmap;private RemoteVIEwBg remoteVIEwBg;public RemoteSurfaceVIEw(Context context) { super(context); sfh = this.getHolder(); sfh.addCallback(this); paint = new Paint(); paint.setAntiAlias(true); setFocusable(true); setFocusableIntouchMode(true); setZOrderOntop(true); getHolder().setFormat(PixelFormat.transparent);}public voID surfaceCreated(SurfaceHolder holder) { int wIDth = getWIDth(); int height = getHeight(); bigCircleX = wIDth / 2; bigCircleY = height / 2; bigCircleR = wIDth / 4; smallCircleX = wIDth / 2; smallCircleY = height / 2; smallCircleR = wIDth / 8; bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.fangxiang); remoteVIEwBg = new RemoteVIEwBg(bitmap); th = new Thread(this); flag = true; th.start();}/*** * 得到两点之间的弧度 */public float geTrad(float px1,2)); float cosAngle = x / z; float rad = (float) Math.acos(cosAngle); if (py2 < py1) {  rad = -rad; } return rad;}@OverrIDepublic boolean ontouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {  // 在范围外触摸  if (Math.sqrt(Math.pow((bigCircleX - (int) event.getX()),tempRad);  } else {//范围内触摸   smallCircleX = (int) event.getX();   smallCircleY = (int) event.getY();  } } else if (event.getAction() == MotionEvent.ACTION_UP) {  smallCircleX = bigCircleX;  smallCircleY = bigCircleY; } return true;}public voID getXY(float x,float y,float R,double rad) { //获取圆周运动的X坐标 smallCircleX = (float) (R * Math.cos(rad)) + x; //获取圆周运动的Y坐标 smallCircleY = (float) (R * Math.sin(rad)) + y;}public voID draw() { try {  canvas = sfh.lockCanvas();  canvas.drawcolor(getResources().getcolor(R.color.ghostwhite));  // 指定图片绘制区域(左上角的四分之一)  Rect src = new Rect(0,paint); } catch (Exception e) {  // Todo: handle exception } finally {  try {   if (canvas != null)    sfh.unlockCanvasAndPost(canvas);  } catch (Exception e2) {   e2.printstacktrace();  } }}public voID run() { while (flag) {  draw();  try {   Thread.sleep(50);  } catch (Exception ex) {   ex.printstacktrace();  } }}public voID surfaceChanged(SurfaceHolder holder,int format,int wIDth,int height) {}public voID surfaceDestroyed(SurfaceHolder holder) { flag = false;} }

总结

以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程小技巧的支持。

总结

以上是内存溢出为你收集整理的Android自定义View实现游戏摇杆键盘的方法示例全部内容,希望文章能够帮你解决Android自定义View实现游戏摇杆键盘的方法示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1145580.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存