zoukankan      html  css  js  c++  java
  • android 正确获取屏幕像素大小

    屏幕的像素是根据DisplayMetrics类来获取的

    具体的计算方法是  width = widthPixels * density;   height = heightPixels * density  (ps: widthPixels  和  heightPixels  ,density   都是从DisplayMetrics中获取的)。

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int widthPixels= dm.widthPixels;
    int heightPixels= dm.heightPixels;
    float density = dm.density;
    int screenWidth = widthPixels * density ;
    int screenHeight = heightPixels * density ;

    在320*480 的phone 上的 到的 widthPixels 值是320, heightPixels  值是480, density 的值是1.0。

    在480*800 的phone 上的到的idthPixels 值是320, heightPixels  值是533, density 的值是1.5。

    注意: 此处DisplayMetrics 不要使用context.getApplicationContext().getResources().getDisplayMetrics();

    打印信息:

    800*480:

    density=1.5
    screen(w,h)=799,480
    pixels(w,h)=533,320

    480*320:

    density=1.5
    screen(w,h)=480,319
    pixels(w,h)=320,213
  • 相关阅读:
    棋盘格渲染
    openvino踩坑之Data type is unsupported
    [video super resolution] ESPCN论文笔记
    tensorflow fp16训练
    openvino安装踩坑记
    python numpy中astype使用不当导致图像出现artifact
    Python~字典
    Django~待解决的问题
    正则表达式应用
    GitLab使用
  • 原文地址:https://www.cnblogs.com/KiloNet/p/2004334.html
Copyright © 2011-2022 走看看