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
  • 相关阅读:
    SpringMVC 集成 Swagger【问题】 No qualifying bean of type RequestMappingHandlerMapping found for dependency
    【leetcode】medianofTwoSortedArrays
    记一次apache+php调优
    java 文件定位
    Java知识探究一:关于IO类库
    180app待选内容
    SQlserver 2000 根据spid 查询执行的SQL
    (转)Flashbuilder4.5中文版破解方法
    代码整洁之道
    手机分辨率基础知识(DPI,DIP计算)
  • 原文地址:https://www.cnblogs.com/KiloNet/p/2004334.html
Copyright © 2011-2022 走看看