How to get android device screen size?

2019-01-04 09:36发布

I have two android device with same resolution

Device1 -> resolution 480x800 diagonal screen size -> 4.7 inches

Device2 -> resolution 480x800 diagonal screen size -> 4.0 inches

How to find device diagonal screen size?

Detect 7 inch and 10 inch tablet programmatically

I have used the above link but it gives both device diagonal screen size -> 5.8

6条回答
2楼-- · 2019-01-04 09:48

Pythagoras theorem to find the diagonal size of Android phone/tablet screen, same principal can be applied to iPhone or Blackberry screen.

Try as below the other way:

  DisplayMetrics met = new DisplayMetrics();                
  this.getWindowManager().getDefaultDisplay().getMetrics(met);// get display metrics object
  String strSize = 
  new DecimalFormat("##.##").format(Math.sqrt(((met.widthPixels / met.xdpi) *
  (met.widthPixels / met.xdpi)) +
  ((met.heightPixels / met.ydpi) * (met.heightPixels / met.ydpi))));
  // using Dots per inches with width and height
查看更多
看我几分像从前
3楼-- · 2019-01-04 09:50

Try this:

 public static Boolean isTablet(Context context) {

    if ((context.getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE) {

      return true;
  }
    return false;
}
查看更多
对你真心纯属浪费
4楼-- · 2019-01-04 09:54

Don't forget to multiply the screen size by the scaledDensity if you are doing what I did and change the size of stuff based on the screen size. e.g.:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
double screenInches = Math.sqrt(x + y) * dm.scaledDensity;
Log.d("debug", "Screen inches : " + screenInches); 

Note the second last line! Here's more info the scaledDensity stuff: What does DisplayMetrics.scaledDensity actually return in Android?

查看更多
冷血范
5楼-- · 2019-01-04 09:55

This won't work?

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
double screenInches = Math.sqrt(x + y);
Log.d("debug", "Screen inches : " + screenInches); 
查看更多
我只想做你的唯一
6楼-- · 2019-01-04 10:09

try this code to get screen size in inch

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
double wi=(double)width/(double)dm.xdpi;
double hi=(double)height/(double)dm.ydpi;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);
查看更多
何必那么认真
7楼-- · 2019-01-04 10:13
DisplayMetrics displayMetrics = context.getResources()
                    .getDisplayMetrics();

String screenWidthInPix = displayMetrics.widthPixels;

String screenheightInPix = displayMetrics.heightPixels;
查看更多
登录 后发表回答