how to get total screen size on an android device programmatically

后端 未结 8 1673
旧巷少年郎
旧巷少年郎 2021-01-02 11:42

if i use the code shown here to get the total screen size it is always short on height to the total screen size as shown in the documented specs for the device. for example

8条回答
  •  没有蜡笔的小新
    2021-01-02 11:54

    int density;
        private DisplayMetrics metrics;
        private int widthPixels;
        private float scaleFactor;
        private float widthDp;
    metrics = new DisplayMetrics();
                    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
                    density= getResources().getDisplayMetrics().densityDpi;
    
                    widthPixels = metrics.widthPixels;
    
                    scaleFactor = metrics.density;
    
                    widthDp = widthPixels / scaleFactor;
    System.out.println("widthDp== "+widthDp );
    if(density==DisplayMetrics.DENSITY_HIGH)
                            System.out.println("Density is high");
    
                        if(density==DisplayMetrics.DENSITY_XXHIGH)
                            System.out.println("Density is xxhigh");
    
                        if(density==DisplayMetrics.DENSITY_XXXHIGH)
                            System.out.println("Density is xxxhigh");
    
                        if(density==DisplayMetrics.DENSITY_TV)
                            System.out.println("Density is Tv");
    

提交回复
热议问题