我有相同的分辨率两款Android设备如何获取android设备的屏幕尺寸?
Device1的 - >分辨率为480x800屏幕对角线尺寸 - >4.7英寸
设备2 - >分辨率为480x800屏幕对角线尺寸 - >4.0英寸
如何找到设备对角线屏幕尺寸?
Detect 7 inch and 10 inch tablet programmatically
我已经使用了上面的链接,但它提供了两种设备屏幕对角线尺寸 - > 5.8
我有相同的分辨率两款Android设备如何获取android设备的屏幕尺寸?
Device1的 - >分辨率为480x800屏幕对角线尺寸 - >4.7英寸
设备2 - >分辨率为480x800屏幕对角线尺寸 - >4.0英寸
如何找到设备对角线屏幕尺寸?
Detect 7 inch and 10 inch tablet programmatically
我已经使用了上面的链接,但它提供了两种设备屏幕对角线尺寸 - > 5.8
试试这个代码英寸获取屏幕尺寸
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);
真正有效的唯一答案! –
谢谢您的赞赏 –
@卡拉斯请接受答案。 –
试试这个:
public static Boolean isTablet(Context context) {
if ((context.getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE) {
return true;
}
return false;
}
这将无法正常工作?
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);
DisplayMetrics displayMetrics = context.getResources() .getDisplayMetrics(); String screenWidthInPix = displayMetrics.widthPixels; String screenheightInPix = displayMetrics.heightPixels;
你能至少解释你的代码? –
这是一个不好的方法。这些资源会返回不准确的dpi和分辨率。 – grebulon
毕达哥拉斯定理找到Android手机/平板屏幕的对角线尺寸,相同的原理可以应用到iPhone或黑莓屏幕。
尝试的另一种方式如下:
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
不要忘记在scaledDensity乘以屏幕尺寸如果你正在做我做的事情,并根据屏幕大小更改东西的大小。例如:
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);
注意第二行!这里有更多的信息缩放密度的东西:What does DisplayMetrics.scaledDensity actually return in Android?
那么你的问题是什么? – GrIsHu
我想找到编程方式对角线屏幕尺寸,以找到哪个设备是 – Kailas
您已经尝试的代码是正确的代码,因为你正在获得对角屏幕尺寸。 – GrIsHu