适用于Android屏幕尺寸的代码不起作用
问题描述:
我已经尝试了下面的代码来找出屏幕尺寸,但没有一个适合我。界面对于相同屏幕尺寸和相同尺寸的不同设备而言也不成比例。我没有实际的设备,我正在使用由AVD提供的屏幕尺寸/尺寸的设备。代码适用于尺寸,但不能提供正确的屏幕尺寸。它为4英寸屏幕计算的屏幕尺寸是3.9英寸(圆形图形),而3.7英寸屏幕的屏幕尺寸也是3.9英寸。 Nexus S(4“,480x800:hdpi)的界面代码不适用于4”WVGA(480x800:hdpi),其中两者的屏幕尺寸和尺寸均相同。适用于Android屏幕尺寸的代码不起作用
能否请你帮我解决这个问题。
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int widthInch=dm.widthPixels;
int heightInch=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)widthInch/(double)dens;
double hi=(double)heightInch/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);
screenInches = (double)Math.round(screenInches * 10)/10;
int widthPix = (int) Math.ceil(dm.widthPixels * (dm.densityDpi/160.0));
Toast.makeText(getApplicationContext(), width+"*"+height+" - "+widthPix+
" - " +screenInches,
Toast.LENGTH_LONG).show();
//================================================================
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
//get density per inch for example: 120 , 160 , 240
float mXDpi = metrics.xdpi; // 160 The exact physical pixels per inch of the screen in the X dimension.
float mYDpi = metrics.ydpi;
//density
int nDensity = metrics.densityDpi; // 160 screen density expressed as dots-per-inch
float mMetersToPixelsX = mXDpi/0.0254f; // 1 inch == 0.0254 metre
float mMetersToPixelsY = mYDpi/0.0254f;
//Resolution
//The total number of physical pixels on a screen.
int wPix = metrics.widthPixels; // 320 The absolute width of the display in pixels.
int hPix = metrics.heightPixels; // 480 The absolute height of the display in pixels.
int nWidthDisplay = (wPix < hPix)? wPix : hPix;
float nWidthScreenInInch = wPix/mXDpi; //320/160 == 2.0 in inch.
float nHeightScreenInInch = hPix/mYDpi; //480/160 == 3.0 in inch.
double screenInches = Math.sqrt(nHeightScreenInInch*nHeightScreenInInch +
nWidthScreenInInch * nWidthScreenInInch);
screenInches = (double)Math.round(screenInches * 10)/10;
Toast.makeText(getApplicationContext(),"Screen size: "+screenInches,
Toast.LENGTH_LONG).show();
//================================================================
DisplayMetrics metrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float hgt=metrics.heightPixels/metrics.xdpi;
float wdth=metrics.widthPixels/metrics.ydpi;
double screenInches = FloatMath.sqrt(hgt*hgt+wdth*wdth);
screenInches = (double)Math.round(screenInches * 10)/10;
Toast.makeText(getApplicationContext(),width+"*"+height+" - "+screenInches,
Toast.LENGTH_LONG).show();
//===============================================================
How to get android device screen size?
答
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
final int height = metrics.heightPixels;
int width = metrics.widthPixels;
我已经做了一些改变,以我的问题。你可以看看它。 – user2387107 2014-10-08 07:21:03