【问题标题】:Setting a button's position to random points on screen? - Android将按钮的位置设置为屏幕上的随机点? - 安卓
【发布时间】:2013-10-02 11:26:47
【问题描述】:

下面,我有 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] ...因此,理论上,它也不应该从另一侧离开屏幕...

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

我认为这个问题很简单,因为我拥有我需要的所有功能..

这不起作用 ►在按钮和相对层上设置边距。 ►从随机函数中移除所有按钮尺寸参数。

也就是说,使用:

new Random().nextInt(metrics.widthPixels)

那么,我做错了什么?

【问题讨论】:

    标签: android button random position


    【解决方案1】:

    由于您将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);
    

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

    【讨论】:

    • 不! ...仍然不起作用..(虽然,它确实出现在屏幕内大约 80% 的时间...)
    • 如果您从 XML 文件中的 RelativeLayout 中删除填充怎么办?
    • 没关系!我让它开始工作了......我在 Random() 里面减去了多少......以及我在外面添加了多少...... :-)......是的,事实证明,填充导致它在碰撞时不可见:-)
    【解决方案2】:

    解决办法如下:

    public class OpenArea extends Activity {
    
        private RelativeLayout loOPenArea;
        private ImageView imageView;
        private DisplayMetrics metrics;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_open);
    
            metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            loOPenArea = (RelativeLayout) findViewById(R.id.lo_open_area);
        }
    
        public void addView(View v) {
            imageView = new ImageView(this);
            imageView.setImageResource(R.mipmap.ic_launcher);
            loOPenArea.addView(imageView);
    
            int leftMargin = new Random().nextInt(metrics.widthPixels - imageView.getWidth());;
            int topMargin = new Random().nextInt(metrics.heightPixels - 2*imageView.getHeight());;
    
            setMargins(imageView, leftMargin, topMargin, 0, 0);
        }
    
        private void setMargins(View view, int left, int top, int right, int bottom) {
            if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
                ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
                p.setMargins(left, top, right, bottom);
                view.requestLayout();
            }
        }
    }
    

    XML:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">
    
        <RelativeLayout
            android:id="@+id/lo_open_area"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="50dp">
    
        </RelativeLayout>
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="-50dp"
            android:onClick="addView"
            android:text="+" />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多