• Android获取屏幕的大小与密度的代码


    Android项目开发中很多时候需要获取手机屏幕的宽高以及屏幕密度来进行动态布局,这里总结了三种获取屏幕大小和屏幕密度的方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    // 获取屏幕密度(方法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""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""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""screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
  • 相关阅读:
    bedtools神器 | gtf转bed | bed文件运算
    常识的力量
    Introduction to dnorm, pnorm, qnorm, and rnorm for new biostatisticians
    最大似然估计实例 | Fitting a Model by Maximum Likelihood (MLE)
    (转)从最大似然估计开始,你需要打下的机器学习基石
    highly variable gene | 高变异基因的选择 | feature selection | 特征选择
    用R的igraph包来画蛋白质互作网络图 | PPI | protein protein interaction network | Cytoscape
    从fasta中提取或者过滤掉多个序列
    GenomicConsensus (quiver, arrow)使用方法 | 序列 consensus
    blast | diamond 输出结果选择和解析 | 比对
  • 原文地址:https://www.cnblogs.com/likeju/p/4789360.html
Copyright © 2020-2023  润新知