android - 将按钮的位置设置为屏幕上的随机点? - 安卓

标签 android button random position

下面,我有 Java 和 XML 代码,基本上我试图点击一个固定按钮,然后让第二个按钮在屏幕上随机跳转

XML 说明: 所以,我有一个带有两个按钮的相对布局...其中一个固定在屏幕底部...我想随机设置另一个的位置。

屏幕打开,点击固定按钮...另一个按钮的位置应该随机变化。

假设我再次点击固定按钮,另一个按钮应该随机跳动。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Rrand" >

    <Button
        android:id="@+id/rbb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

    <Button
        android:id="@+id/bss"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:onClick="aa"
        android:text="Button" />

</RelativeLayout>

JAVA解释 在这里,我得到了固定按钮的 OnClick 函数。

►b = 应该跳来跳去的按钮

首先,我们获取屏幕尺寸,然后使用 LayoutParams,使用随机函数设置按钮位置。

   public void Clicky(View v) {
            b = (Button) findViewById(R.id.rbb1);

    ///Getting the Screen dimensions for API 1+

            LayoutParams params = (LayoutParams) b.getLayoutParams();
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);

    ///Setting the buttons paramaters, again for API 1+

            params.leftMargin = b.getWidth()
                    + new Random().nextInt(metrics.widthPixels - b.getWidth());
            params.topMargin = b.getHeight()
                    + new Random().nextInt(metrics.heightPixels - b.getHeight());
            b.setLayoutParams(params);

        }

X 坐标的随机函数:

b.getWidth()+ new Random().nextInt(metrics.widthPixels - b.getWidth());

最小值 = b.getWidth()。

因此,从理论上讲,该按钮不应该甚至部分地出现在屏幕之外。

在 nextInt 的参数中,我使用了 [Screenwidth - Button Width] ...

问题 但确实如此。大约一半的时间,按钮甚至没有出现在屏幕上......问题必须出在随机函数中(我认为是这样)......我只是希望它出现在屏幕上的随机点上。

我认为这个问题很合乎逻辑,因为我拥有我需要的所有功能..

这没用 ►在按钮和相对布局上设置边距。 ►从随机函数中删除所有按钮尺寸参数。

也就是使用:

新的 Random().nextInt(metrics.widthPixels)

那么,我做错了什么?

最佳答案

由于您将b.getWidth()添加到随机值中,因此您必须将随机值更改为metrics.widthPixels - 2*b.getWidth(),因为否则偏移量可能是 metrics.widthPixels - b.getWidth() + b.getWidth 这解释了为什么它有时会超出右侧和底部的边界。所以它应该是这样的:

params.leftMargin = b.getWidth()
                + new Random().nextInt(metrics.widthPixels - 2*b.getWidth());
params.topMargin = b.getHeight()
                + new Random().nextInt(metrics.heightPixels - 3*b.getHeight());
b.setLayoutParams(params);

注意:在此版本中,按钮永远不会触及屏幕的左边缘或上边缘。


如果您希望按钮也(可能)触摸上边框和/或左边框,请不要将宽度/高度添加到边距:

params.leftMargin = new Random().nextInt(metrics.widthPixels - b.getWidth());
params.topMargin = new Random().nextInt(metrics.heightPixels - 2*b.getHeight());
b.setLayoutParams(params);

编辑:
我更改了上边距的计算,因此按钮不会与固定按钮位于同一位置。

关于android - 将按钮的位置设置为屏幕上的随机点? - 安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19135827/

相关文章:

python:生成用于身份验证的 16 字节 token

javascript - Math.random() 和 .replace() 跨浏览器

android - 在 Android 上监听多个传感器并使用传感器值更新多个 TextView

android - 对多按键有线耳机的 react

android - 如何将宽度设置为 "fill parent"的 android 按钮中的图标和文本居中

java - 如何动态改变按钮的位置

android - 为什么在android中强制关闭后 Activity 会继续重新加载?

android - 如何在 Android Gallery3D (cooliris) 中显示特定文件夹?

html - onclick CSS 按钮效果

C++ 如何生成 10,000 个唯一的随机整数以存储在 BST 中?