2017-02-27 34 views
1

可移动和点击的按钮,我是新来的Xamarin的Android和目前工作的一个浮动的操作按钮,我实现View.IOnTouchListener和普通按钮点击事件(faButton.Click + = floatButtonPressed;)执行我的行动。但对于的情况MotionEventActions.Move,它不工作,因为我想。左右移动可以正常工作,但是对于顶部和底部,只要我开始移动,它会向下移动一点点。此外,当我将按钮移动到屏幕边框时,它将能够超出屏幕。因此,我试图检测屏幕大小,并限制它,但它仍然不够好,有没有其他可用的解决方案或设置?如何创建Android的

public bool OnTouch(View v, MotionEvent e) 
    { 
     switch (e.Action) 
     { 
      case MotionEventActions.Down: 
       oldXvalue = e.GetX(); 
       oldYvalue = e.GetY(); 

       if (oldXvalue == e.GetX() && oldYvalue == e.GetY()) 
       { 
        return false; 
       } 

       break; 

      case MotionEventActions.Up: 

       if (oldXvalue == e.GetX() && oldYvalue == e.GetY()) 
       { 
        return false; 
       }     
       break; 

      case MotionEventActions.Move: 
       var xleft = (int)(e.RawX - oldXvalue); 
       var xright = xleft + v.Width; 
       var ytop = (int)(e.RawY - oldYvalue); 
       var ybtm = (ytop + v.Height); 
       if (xleft + v.Width >= intWidth) 
       { 
        break; 
       } 
       if (xleft <= 0) 
       { 
        break; 
       } 

       if (ytop + v.Height >= intHeight) 
       { 
        break; 
       } 
       if (ytop <= 0) 
       { 
        break; 
       } 
       v.Layout(xleft, ytop, xright, ybtm); 
       break; 
     } 
     return true; 
    } 
+0

可以的DOI删除和添加视图(按钮)触摸 –

+0

你是什么意思删除,并添加观点?你能给我一些提示吗? @JinalPatel –

+0

你想要一个可移动的按钮,你可以使用一个按钮,它将添加在你触摸并从上一个位置移开的地方 –

回答

0

您可以先获取屏幕高度和屏幕宽度。当按钮超过屏幕时,您需要重置按钮位置。

试试下面的代码:

public class MainActivity : Activity, IOnTouchListener 
    { 
     Button dragAbleBt; 
     int screenWidth = 0; 
     int screenHeight = 0; 
     int lastX = 0, lastY = 0; 
     public bool OnTouch(View v, MotionEvent e) 
     {    
      MotionEventActions ea = e.Action; 
      switch (ea) { 
       case MotionEventActions.Down: 
        lastX = (int)e.RawX; 
        lastY = (int)e.RawY;     
        break; 
       case MotionEventActions.Move: 
        int dx = (int)e.RawX - lastX; 
        int dy = (int)e.RawY - lastY; 
        int left = v.Left + dx; 
        int right = v.Right + dx; 
        int top = v.Top + dy; 
        int bottom = v.Bottom + dy; 
        if (left < 0) 
        { 
         left = 0; 
         right = left + v.Width; 
        } 
        if (right > screenWidth) 
        { 
         right = screenWidth; 
         left = right - v.Width; 
        } 
        if (top < 0) 
        { 
         top = 0; 
         bottom = top + v.Height; 
        } 
        if (bottom > screenHeight) 
        { 
         bottom = screenHeight; 
         top = bottom - v.Height; 
        } 
        v.Layout(left, top, right, bottom); 
        lastX = (int) e.RawX; 
        lastY = (int) e.RawY; 
        v.PostInvalidate(); 
        break; 
       case MotionEventActions.Up:     
        break;     
      } 
      return false; 
     } 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      SetContentView (Resource.Layout.Main); 
      //DisplayMetrics dm = Resources.DisplayMetrics; 
      //screenWidth = dm.WidthPixels; 
      //screenHeight = dm.HeightPixels; 
      dragAbleBt = FindViewById<Button>(Resource.Id.button1); 
      dragAbleBt.SetOnTouchListener(this); 
     } 

     public override void OnWindowFocusChanged(bool hasFocus) 
     { 
      base.OnWindowFocusChanged(hasFocus); 
      if (hasFocus) 
      { 
       Rect outRect = new Rect(); 
       this.Window.FindViewById(Window.IdAndroidContent).GetDrawingRect(outRect); 
       screenWidth = outRect.Width(); 
       screenHeight = outRect.Height(); 
      } 
     } 
    } 

我试图检测屏幕尺寸和限制,但它仍然不够好,还有没有其他可用的解决方案或设置?

你可能得到了整个屏幕宽度和高度,按钮将超出屏幕高度,在这种情况下,你需要通过this.Window.FindViewById(Window.IdAndroidContent).GetDrawingRect(outRect)

屏幕截图获取视图绘图区域:

注:我正在使用速度较慢的仿真器,如果您使用的是真实设备,速度会更快。

enter image description here

+0

此代码完美工作。它解决了我的两个问题。非常感谢你的兄弟! @MikeMa –