-3
我已經嘗試了下面的代碼來找出屏幕尺寸,但沒有一個適合我。界面對於相同屏幕尺寸和相同尺寸的不同設備而言也不成比例。我沒有實際的設備,我正在使用由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?
Calculating android screen size?
Detect 7 inch and 10 inch tablet programmatically
我已經做了一些改變,以我的問題。你可以看看它。 – user2387107 2014-10-08 07:21:03