Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách lấy chiều cao và chiều rộng màn hình thiết bị Android

Giới thiệu

Hướng dẫn này trình bày cách lấy chiều cao, chiều rộng màn hình (tính bằng pixel) và kích thước đường chéo màn hình (tính bằng inch) trên thiết bị Android thông qua DisplayMetrics.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project, điền thông tin cần thiết và tạo dự án mới.

Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_marginTop="30dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tvScreenHeight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20sp"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvScreenWidth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvScreenInches"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="16sp"
        android:textStyle="bold" />
</LinearLayout>

Bước 3: Viết code xử lý (MainActivity.java)

package com.example.screensize;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvHeight, tvWidth, tvInches;

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

        tvHeight = findViewById(R.id.tvScreenHeight);
        tvWidth = findViewById(R.id.tvScreenWidth);
        tvInches = findViewById(R.id.tvScreenInches);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        int screenHeight = displayMetrics.heightPixels;
        int screenWidth = displayMetrics.widthPixels;

        // Tính kích thước màn hình theo inch (đường chéo)
        double heightInches = screenHeight / displayMetrics.ydpi;
        double widthInches = screenWidth / displayMetrics.xdpi;
        double screenInches = Math.sqrt(heightInches * heightInches + widthInches * widthInches);
        screenInches = Math.round(screenInches * 10.0) / 10.0; // Làm tròn 1 chữ số thập phân

        tvHeight.setText("Chiều cao màn hình: " + screenHeight + " px");
        tvWidth.setText("Chiều rộng màn hình: " + screenWidth + " px");
        tvInches.setText("Kích thước màn hình: " + screenInches + " inch");
    }
}

Bước 4: Cấu hình AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.screensize">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ScreenSize">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Chạy ứng dụng

Kết nối thiết bị Android thật với máy tính, chọn thiết bị làm target và chạy ứng dụng. Màn hình sẽ hiển thị chiều cao, chiều rộng và kích thước đường chéo của màn hình thiết bị.

Lưu ý quan trọng

  • getDefaultDisplay().getMetrics() đã bị deprecated từ API 30. Với Android 11+, nên sử dụng WindowMetricsCalculator hoặc WindowManager.getCurrentWindowMetrics().
  • Trên thiết bị có cutout/notch, chiều cao báo về bao gồm cả vùng notch.
  • Độ phân giải DPI (xdpi, ydpi) có thể không chính xác trên một số thiết bị.