如何使 ImageButton 随机放置在线性布局中? (Android)

How do I make an ImageButton to randomly be positioned within a linear layout? (Android)

我有一个带有一定填充量的线性布局。我希望将图像按钮随机放置在该线性布局内的任何位置,而不会超出布局。这是我的 xml 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="45dp"
    android:text="Text 1"
    android:id="@+id/text1" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="30dp"
    android:text="Text 2"
    android:id="@+id/text2" />
<LinearLayout
    android:id="@+id/lay1"
    android:layout_width="match_parent"
    android:layout_height="450dp"
    android:layout_margin="20dp">

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:src="@drawable/ib"
    android:id="@+id/button1"/>

</LinearLayout>
</LinearLayout>

我希望将 ImageButton button1 随机放置在布局 lay1 中。

我尝试获取布局的宽度和高度并将它们输入随机函数,然后我使用该函数为图像按钮提供左边距和上边距,但每当我这样做时我的应用程序总是崩溃。这是 Java 代码:

ImageButton b = (ImageButton) findViewById(R.id.button1);
LinearLayout l = (LinearLayout) findViewById(R.id.lay1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) b.getLayoutParams();
int width = l.getWidth();
int height= l.getHeight();
params.leftMargin = new Random().nextInt(width);
params.topMargin = new Random().nextInt(height);
b.setLayoutParams(params);

上述方法无效,我的应用程序在打开此 activity 时总是崩溃。有人可以帮我吗?

你的方法看起来不错,但你也必须考虑图像大小。
所以我会放一些类似的东西:

params.leftMargin = new Random().nextInt(width - b.getWidth());
params.topMargin = new Random().nextInt(height - b.getHeight());

现在您必须更具体地说明您遇到的崩溃。

编辑:SnyersK 也是对的,如果您在元素获得其尺寸之前浏览那段代码,那么您将需要使用 ViewTreeObserver。

我为您提供了部分解决方案。 以下代码将一个按钮放置在随机位置 BUT 可以将其移出屏幕。您应该可以通过使用随机生成器来防止这种情况发生。

public class MainActivity extends ActionBarActivity {

int width, height;
LinearLayout linearLayout;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    linearLayout = (LinearLayout) findViewById(R.id.ll);
    linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Log.v(getClass().getSimpleName(), "onGlobalLayout");
            width = linearLayout.getWidth();
            height = linearLayout.getHeight();

            placeButton();

            if (Build.VERSION.SDK_INT >= 16) {
                linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        }
    });

    button = (Button) findViewById(R.id.button);
}

private void placeButton() {
    Log.v(getClass().getSimpleName(), "width: " + width + " Height: " + height);
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();

    Random rand = new Random();

    params.leftMargin = rand.nextInt(width);
    params.topMargin = rand.nextInt(height);

    Log.v(getClass().getSimpleName(), "left: " + params.leftMargin + " top: " + params.topMargin);
    button.setLayoutParams(params);
    button.invalidate();
}

}

您只需等待绘制线性布局即可。然后你得到它的宽度和高度,然后把按钮放在一个随机的地方。

备注 当您旋转屏幕时,这将移动按钮。您可以通过仅添加 OnGlobalLayoutListener if savedInstanceState == null

来防止这种情况

只需用此替换您的代码

ImageButton b = (ImageButton) findViewById(R.id.button1);
LinearLayout l = (LinearLayout) findViewById(R.id.lay1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)l.getLayoutParams();
int width = l.getWidth();
int height= l.getHeight();
params.leftMargin = new Random().nextInt(width);
params.topMargin = new Random().nextInt(height);
l.setLayoutParams(params);