在 Android 中获取屏幕宽度和高度

Getting Screen width and height in Android

我正在使用此代码获取我的屏幕宽度和高度,并将我的按钮发送到屏幕上的随机位置,但有时我的按钮完全不在屏幕上。我不相信我的代码得到的分辨率是真正的分辨率。

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>

这里有 2 张图片显示了我的问题:

  1. 按钮稍微出现在屏幕上:
  2. 按钮完全离开屏幕:

您可以做的是: 你已经随机生成了 x 和 y, x + button.getWidth() 到它并检查它是否小于 screen width 如果是你的 x 很好地绑定如果没有重新生成你的随机 x 和 y,类似如果 x 在绑定中为 y 做它并检查 y + button.getHeight() 小于 screen height 就可以了。