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

时间:2017-07-09 22:32:14

标签: android random screen-resolution screen-size android-screen-support

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

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. 按钮略微在屏幕上:
    Button slightly on-screen
  2. 按钮完全脱离屏幕: Button completely off-screen

1 个答案:

答案 0 :(得分:0)

你能做的是: 你已经随机生成了x和y,  x + button.getWidth()它并检查它是否小于screen width如果是,如果没有重新更新你的随机x和y,你的x是否合适,类似的如果x在绑定中为y做,并检查y + button.getHeight()小于它将screen height