DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); float height = displayMetrics.heightPixels; float width = displayMetrics.widthPixels; float maxX = width; float maxY = height; float minX = width/10; float minY = height/15; Random rand = new Random(); float finalX = rand.nextFloat() * abs(maxX - minX); float finalY = rand.nextFloat() * abs(maxY - minY); _height.setText("Current Y: " + String.valueOf(finalY)); _width.setText("Current X: " + String.valueOf(finalX)); max_height.setText("Max height: " + String.valueOf(height)); max_width.setText("Max width: " + String.valueOf(width)); btnClick.setX(finalX); btnClick.setY(finalY); btnClick.setVisibility(View.VISIBLE);
这是XML
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/cLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background" app:layout_constraintRight_toRightOf="@+id/width" tools:context="com.jamblatech.clicker.MainActivity" tools:layout_editor_absoluteY="81dp"> <Button android:id="@+id/buttonEnd" android:layout_width="363dp" android:layout_height="572dp" android:background="@android:color/transparent" android:onClick="" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" /> <TextView android:id="@+id/textViewCount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/zero" android:textColor="@color/colorAccent" android:textSize="36sp" android:visibility="invisible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.03" /> <TextView android:id="@+id/timerView" android:layout_width="185dp" android:layout_height="41dp" android:text="@string/zero" android:textAlignment="center" android:textSize="24sp" android:visibility="invisible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.502" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.937" /> <Button android:id="@+id/buttonClick" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="200dp" android:background="@drawable/clickbutton" android:text="@string/Click" android:textSize="24sp" android:visibility="invisible" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/textViewCount" /> <Button android:id="@+id/buttonReplay" android:layout_width="221dp" android:layout_height="103dp" android:text="@string/Replay" android:visibility="invisible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.503" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/buttonBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Back" android:visibility="invisible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.674" /> <TextView android:id="@+id/highScore" android:layout_width="261dp" android:layout_height="107dp" android:textAlignment="center" android:textColor="@android:color/holo_purple" android:textSize="30sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.149" /> <TextView android:id="@+id/height" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Test" android:visibility="invisible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.029" /> <TextView android:id="@+id/width" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Test" android:visibility="invisible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <TextView android:id="@+id/maxheight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Test" android:visibility="invisible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.06" /> <TextView android:id="@+id/maxwidth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Test" android:visibility="invisible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.09" /> <TextView android:id="@+id/testTExt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Test" android:visibility="invisible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <TextView android:id="@+id/startTimerText" android:layout_width="134dp" android:layout_height="95dp" android:text="@string/Test_number" android:textAlignment="center" android:textColor="@android:color/holo_green_dark" android:textSize="60sp" android:visibility="visible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.54" /> </android.support.constraint.ConstraintLayout>
您可以做的是: x + button.getWidth() 并检查其是否小于 screen width 如果是的话,你的x在界内,如果不是的话,调整你的随机x和y,类似的,如果x在界内,做它的y和检查y+按钮。getHeight()小于 screen height 这样就可以了。
x + button.getWidth()
screen width
screen height