当前位置: 首页 > article >正文

android计算屏幕尺寸dpi

说明:
我计划用一个Android程序,打印出平板屏幕的尺寸,大小,dpi等参数信息
效果图:



分辨率: 1280x752
 DPI: 213
 物理尺寸(英寸): 对角线 9.4

step1:

package com.example.myapplication;

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

public class TestActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        printScreenInfo();

    }

    private void printScreenInfo() {
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        // 分辨率
        int widthPixels = metrics.widthPixels;
        int heightPixels = metrics.heightPixels;

        // DPI(密度分级)
        int densityDpi = metrics.densityDpi;

        // 屏幕物理尺寸计算(英寸)
        float widthInches = (float) widthPixels / metrics.xdpi;
        float heightInches = (float) heightPixels / metrics.ydpi;
        float diagonalInches = (float) Math.sqrt(
                Math.pow(widthInches, 2) + Math.pow(heightInches, 2)
        );

        Log.e("ScreenInfo", "分辨率: " + widthPixels + "x" + heightPixels);
        Log.e("ScreenInfo", "DPI: " + densityDpi);
        Log.e("ScreenInfo", "物理尺寸(英寸): 对角线 " + String.format("%.1f", diagonalInches));
    }
}

http://www.kler.cn/a/566125.html

相关文章:

  • 使用Spring Data Redis操作Redis
  • MySQL,Oracle,MariaDB的区别
  • Spring源码分析の循环依赖
  • MATLAB应用介绍
  • Vue项目性能优化、提取公共库(Common Chunks)
  • ONLYOFFICE + Ollama,本地AI模型的高效集成方案
  • Uniapp使用大疆SDK打包离线原生插件
  • log4j2中<logger>中没有指定appender的输出
  • DevOps原理和实现面试题及参考答案
  • 深度学习笔记线性代数方面,记录一些每日学习到的知识
  • 【前端基础】Day 5 CSS浮动
  • 行为型模式 - 状态模式 (State Pattern)
  • springBean介绍
  • TCP,http,WebSocket
  • errorstack ORA-01466 flashback query expdp scn 都有这个问题
  • 2-3文件的属性信息
  • 中英文多商家电商系统对东南亚国家电商的助力作用
  • 实现实时数据仓库开源项目
  • 图数据库Neo4j面试内容整理-Cypher 查询优化
  • Redis---缓存穿透,雪崩,击穿