如何在android中创建一个可移动和可点击的按钮

时间:2017-02-27 09:18:05

标签: c# android xamarin

我是Xamarin Android的新手,目前正在开发一个浮动操作按钮,我实现了 View.IOnTouchListener 和正常的按钮点击事件(faButton.Click + = floatButtonPressed;)来执行我的操作。但对于案例MotionEventActions.Move ,它无法正常工作。向左和向右移动它可以正常工作,但是无论何时开始移动,它都会向上和向下移动一点点。此外,当我将按钮移动到屏幕边框时,它将能够超出屏幕。因此,我尝试检测屏幕大小并限制它但仍然不够好,还有其他可用的解决方案或设置吗?

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;
    }

1 个答案:

答案 0 :(得分:0)

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

请尝试以下代码:

     <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>Engine</mainClass>
                            </transformer>
                        </transformers>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
  

我尝试检测屏幕大小并对其进行限制,但仍然不够好,是否有其他可用的解决方案或设置?

您可能获得整个屏幕的宽度和高度,按钮将超过屏幕高度,在这种情况下,您需要通过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(); } } } 获取视图绘制区域

屏幕截图:

注意:我使用的是慢速模拟器,如果您使用的是真实设备,它会更快。

enter image description here