栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

屏幕分辨率

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

屏幕分辨率

查看屏幕分辨率

activity_main.xml




    

    

    

    

    
        


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();

    // 显示当前手机的屏幕参数信息(px)
    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的文本内容
        tv_screen_px.setText(info);
    }

    // 显示当前手机的屏幕参数信息(dp)
    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的文本内容
        tv_screen_dp.setText(info);
    }

    // 获得屏幕的宽度
    public static int getScreenWidth(Context ctx) {
        // 从系统服务中获取窗口管理器
        WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        // 从默认显示器中获取显示参数保存到dm对象中
        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();
        // 从默认显示器中获取显示参数保存到dm对象中
        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();
        // 从默认显示器中获取显示参数保存到dm对象中
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.density; // 返回屏幕的像素密度数值
    }

    // 根据手机的分辨率从 px(像素) 的单位 转成为 dp
    public static int px2dip(Context context, float pxValue) {
        // 获取当前手机的像素密度
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f); // 四舍五入取整
    }
    
    
}

结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/572357.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号