android surface 游戏,xamarin.android SurfaceView 实现 游戏 触摸摇杆

1 public classRemoteSurfaceView : SurfaceView, ISurfaceHolderCallback, IRunnable2 {3 //private float scale = this.Resources().getDisplayMetrics().density;

4 privateThread th;5 privateISurfaceHolder sfh;6 privateCanvas canvas;7 privatePaint paint;8 private boolflag;9

10 private int bigCircleX = 0;11 private int bigCircleY = 0;12 private int bigCircleR = 0;13 //摇杆的X,Y坐标以及摇杆的半径

14 private float smallCircleX = 0;15 private float smallCircleY = 0;16 private float smallCircleR = 0;17

18

19 privateBitmap bitmap;20 privateRemoteViewBg remoteViewBg;21 public RemoteSurfaceView(Context context) : base(context)22 {23 sfh = this.Holder;24 sfh.AddCallback(this);25 paint = newPaint();26 paint.AntiAlias = true;27 Focusable = true;28 FocusableInTouchMode = true;29 SetZOrderOnTop(true);30 Holder.SetFormat(Format.Transparent);31 }32 public voidSetLayoutParams() { }33 /***34 * 得到两点之间的弧度35 */

36 public float getRad(float px1, float py1, float px2, floatpy2)37 {38 float x = px2 -px1;39

40 float y = py1 -py2;41 //斜边的长

42 float z = (float)Java.Lang.Math.Sqrt(Java.Lang.Math.Pow(x, 2) + Java.Lang.Math.Pow(y, 2));43 float cosAngle = x /z;44 float rad = (float)Java.Lang.Math.Acos(cosAngle);45

46 if (py2

53 public void GetXY(float x, float y, float R, doublerad)54 {55

56 smallCircleX = (float)(R * Java.Lang.Math.Cos(rad)) +x;57

58 smallCircleY = (float)(R * Java.Lang.Math.Sin(rad)) +y;59 }60

61 public voiddraw()62 {63 try

64 {65 canvas =sfh.LockCanvas();66 canvas.DrawColor(Color.White);//Resources.GetColor(Resources.GetColor("colorAccent").));

67 Rect src = new Rect(0, 0, bitmap.Width, bitmap.Height);68 Rect dst = new Rect(bigCircleX - bigCircleR, bigCircleY - bigCircleR, bigCircleX + bigCircleR, bigCircleY +bigCircleR);69 //绘制图片

70 remoteViewBg.draw(canvas, paint, src, dst);71 paint.Color =Color.AliceBlue;72 //绘制摇杆

73 canvas.DrawCircle(smallCircleX, smallCircleY, smallCircleR, paint);74 }75 catch(Java.Lang.Exception e)76 {77 //TODO: handle exception

78 }79 finally

80 {81 try

82 {83 if (canvas != null)84 sfh.UnlockCanvasAndPost(canvas);85 }86 catch(Java.Lang.Exception e2)87 {88 e2.PrintStackTrace();89 }90 }91 }92

93 public voidRun()94 {95 while(flag)96 {97 draw();98 try

99 {100 //Thread.Sleep(50);

101 }102 catch(Java.Lang.Exception ex)103 {104 ex.PrintStackTrace();105 }106 }107 }108

109 public voidSurfaceCreated(ISurfaceHolder holder)110 {111 int width =Width;112 int height =Height;113 bigCircleX = width / 2;114 bigCircleY = height / 2;115 bigCircleR = width / 4;116 smallCircleX = width / 2;117 smallCircleY = height / 2;118 smallCircleR = width / 8;119 bitmap =BitmapFactory.DecodeResource(Resources, Resource.Mipmap.fangxiang);120 remoteViewBg = newRemoteViewBg(bitmap);121 th = new Thread(this);122 flag = true;123 th.Start();124 }125 public override boolOnTouchEvent(MotionEvent e)126 {127 if (e.Action == MotionEventActions.Down || e.Action ==MotionEventActions.Move)128 {129 //范围外触摸

130 if (Java.Lang.Math.Sqrt(Java.Lang.Math.Pow((bigCircleX - (int)e.GetX()), 2) +

131 Java.Lang.Math.Pow((bigCircleY - (int)e.GetY()), 2)) >=bigCircleR)132 {133

134 double tempRad =getRad(bigCircleX, bigCircleY, e.GetX(), e.GetY());135

136 GetXY(bigCircleX, bigCircleY, bigCircleR, tempRad);137 }138 else

139 {//范围内触摸

140 smallCircleX = (int)e.GetX();141 smallCircleY = (int)e.GetY();142 }143 }144 else if (e.Action ==MotionEventActions.Up)145 {146 smallCircleX =bigCircleX;147 smallCircleY =bigCircleY;148

149 }150 return true;151 }152 public voidSurfaceDestroyed(ISurfaceHolder holder)153 {154 flag = false;155 }156

157 public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, intheight)158 {159

160 }161 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值