• 获取屏幕密度合集


    // 获取屏幕密度(方法1)
    int screenWidth  = getWindowManager().getDefaultDisplay().getWidth();        // 屏幕宽(像素,如:480px)
    int screenHeight = getWindowManager().getDefaultDisplay().getHeight();        // 屏幕高(像素,如:800p)

    Log.e(TAG + "  getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);


    // 获取屏幕密度(方法2)
    DisplayMetrics dm = new DisplayMetrics();
    dm = getResources().getDisplayMetrics();

    float density  = dm.density;        // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
    int densityDPI = dm.densityDpi;        // 屏幕密度(每寸像素:120/160/240/320)
    float xdpi = dm.xdpi;           
    float ydpi = dm.ydpi;

    Log.e(TAG + "  DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
    Log.e(TAG + "  DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);

    screenWidth  = dm.widthPixels;        // 屏幕宽(像素,如:480px)
    screenHeight = dm.heightPixels;        // 屏幕高(像素,如:800px)

    Log.e(TAG + "  DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);

    // 获取屏幕密度(方法3)
    dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    density  = dm.density;        // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
    densityDPI = dm.densityDpi;        // 屏幕密度(每寸像素:120/160/240/320)
    xdpi = dm.xdpi;           
    ydpi = dm.ydpi;

    Log.e(TAG + "  DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
    Log.e(TAG + "  DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);

    int screenWidthDip = dm.widthPixels;        // 屏幕宽(dip,如:320dip)
    int screenHeightDip = dm.heightPixels;        // 屏幕宽(dip,如:533dip)

    Log.e(TAG + "  DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip);

    screenWidth  = (int)(dm.widthPixels * density + 0.5f);        // 屏幕宽(px,如:480px)
    screenHeight = (int)(dm.heightPixels * density + 0.5f);        // 屏幕高(px,如:800px)

    Log.e(TAG + "  DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);

  • 相关阅读:
    HDU 1434 幸福列车(优先队列)
    HDU 4287 Intelligent IME(字典树)
    HDU 1671 Phone List(字典树)
    HDU 1711 Number Sequence(KMP匹配数字串)
    HDU 1251 统计难题(字典树计算前缀数量)
    HDU 2087 剪花布条(KMP基础应用)
    HRBUST 1909 理工门外的树(双数组实现线段树功能)
    HDU 1166 敌兵布阵(线段树)
    HDU 1754 I Hate It(线段树基础应用)
    HDU 1260 Tickets(基础dp)
  • 原文地址:https://www.cnblogs.com/VellBibi/p/3339716.html
Copyright © 2020-2023  润新知