设置按钮的位置,随机点在屏幕上? - Android电子 [英] Setting a button's position to random points on screen? - Android

查看:123
本文介绍了设置按钮的位置,随机点在屏幕上? - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,我已经得到了Java和XML code,基本上我想点击一个固定的按钮,并在屏幕上第二跳键在任意点

Below, I've got both the Java and XML code, basically I'm trying to click a fixed button, and make the 2nd button jump around the screen at random points

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

XML Explanation: So, I've got a relative layout with two buttons... One of them is fixed at the bottom of the screen... and I want to set the other one's position randomly.

屏幕打开,点击固定键...和其他按钮的位置应该随意改变。

Screen opens, Click the fixed button... and the other button's position should change randomly.

说我再次点击固定键和阿恩,另一个按钮应该围绕随机跳。

Say I click the fixed button again and agin, the other button should jump around randomly.

<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功能。

JAVA Explanation And here I've got the OnClick function for the fixed button.

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

►b = The button that's supposed to jump around

首先,我们得到的屏幕尺寸,然后使用的LayoutParams,利用随机函数设定的按钮的位置。

First of all, we get the screen dimensions, and then using LayoutParams, set the buttons position using the random function.

   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()+新的随机()nextInt(metrics.widthPixels - b.getWidth());

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

最小值= b.getWidth()。

Minimum value = b.getWidth().

所以,从理论上讲,按钮应该永远甚至部分出现了小屏幕。

So, IN THEORY, the button should never ever even partially appear a little out the screen.

在为nextInt的参数,我用[屏幕宽度 - 巴顿宽度] ...所以,从理论上讲,它应该永远不会过时的屏幕从另一个侧面也...

In the parameters for nextInt, I use [Screenwidth - Button Width] ... So, IN THEORY, it should never go out of the screen from the other side too...

问题
然而,它的作用。大约有一半的时间,按钮甚至没有出现在屏幕上......这个问题有可能成为随机函数(我是这么认为的)......我只是希望它出现在屏幕上随机点。

Problem Yet it does. Approximately half the time, the button does not even appear on the screen... The problem has to be in the Random Function (I think so) ... I just want it to appear on random spots on the screen.

我觉得这个问题很简单的逻辑,因为我有我需要的所有功能。

I reckon the problem is simply logical, because I have all the functions I need..

这不起作用
►Setting按钮和相对Layour追加保证金。
从随机函数►Removing所有按钮的尺寸参数。

This didn't work ►Setting a Margin on the button and the Relative Layour. ►Removing all button dimension parameter from the random function.

也就是说,使用:

新的随机()。nextInt(metrics.widthPixels)

new Random().nextInt(metrics.widthPixels)

所以,我怎么拿错?

推荐答案

由于您添加 b.getWidth()来的随机值,你必须改变的随机值到 metrics.widthPixels - 2 * b.getWidth(),因为otherwize偏移可能是 metrics.widthPixels - b.getWidth()+ b。的getWidth 这可以解释为什么它不在边界的权利,并且有时底部。因此,它应该是这样的:

Since you add b.getWidth() to the random value, you have to change the random value to metrics.widthPixels - 2*b.getWidth(), because otherwize the offset could be metrics.widthPixels - b.getWidth() + b.getWidth which explains why it is out of border to the right and to the bottom sometimes. So it should look like this:

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

请注意:在这个版本中,按钮将永远不会触碰左边缘或屏幕的顶部边缘。

NOTE: In this version, the button will never touch the left edge or top edge of the screen.

如果您想按钮也(可能)会接触上和/或左边框,不宽/高增加保证金:

If you want to the button also to (possibly) touch the upper and/or left border, don't add width/height to margin:

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


编辑:结果
我改变了边距的计算使按键woun't处于相同的位置固定的按钮。



I changed the calculation of the top margin so the button woun't be at the same position as the fixed button.

这篇关于设置按钮的位置,随机点在屏幕上? - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆