查看屏幕分辨率
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前屏幕分辨率信息如下"
android:layout_gravity="center_horizontal"
android:textColor="@color/black"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="以px值表示:"
android:textColor="@color/black"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"/>
<TextView
android:id="@+id/tv_screen_px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="以dp值表示:"
android:textColor="@color/black"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"/>
<TextView
android:id="@+id/tv_screen_dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
MainActivity.java
package com.example.testdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv_screen_px;
private TextView tv_screen_dp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_screen_px = findViewById(R.id.tv_screen_px);
showScreenInfo_px();
tv_screen_dp = findViewById(R.id.tv_screen_dp);
showScreenInfo_dp();
private void showScreenInfo_px() {
int width = getScreenWidth(this);
int height = getScreenHeight(this);
float density = getScreenDensity(this);
String info = String.format("宽度:%dpx 高度:%dpx \n像素密度:%f", width, height, density);
tv_screen_px.setText(info);
}
private void showScreenInfo_dp() {
int width = px2dip(this,getScreenWidth(this));
int height = px2dip(this,getScreenHeight(this));
float density = getScreenDensity(this);
String info = String.format("宽度:%dp 高度:%dp \n像素密度:%f", width, height, density);
tv_screen_dp.setText(info);
}
public static int getScreenWidth(Context ctx) {
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
public static int getScreenHeight(Context ctx) {
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
return dm.heightPixels;
}
public static float getScreenDensity(Context ctx) {
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
return dm.density;
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
结果:
|