以编程方式检测 7 英寸和 10 英寸平板电脑 [英] Detect 7 inch and 10 inch tablet programmatically

查看:18
本文介绍了以编程方式检测 7 英寸和 10 英寸平板电脑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法以编程方式确定安装该应用的设备是 7 英寸平板电脑还是 10 英寸平板电脑?

解决方案

您可以使用 DisplayMetrics 获取有关运行应用的屏幕的大量信息.

首先,我们创建一个 DisplayMetrics 指标对象:

DisplayMetrics metrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metrics);

由此,我们可以得到显示尺寸所需的信息:

int widthPixels = metrics.widthPixels;int heightPixels = metrics.heightPixels;

这将返回宽度和高度的绝对值(以像素为单位),因此 Galaxy SIII、Galaxy Nexus 等为 1280x720.

这本身通常没有帮助,因为当我们在 Android 设备上工作时,我们通常更喜欢在与密度无关的像素下工作.

您再次使用 metrics 以设备的比例因子的形式获得屏幕的 密度,该比例因子基于 ,它为您提供每种屏幕尺寸的相对 dp:

<块引用>
  • 320dp:典型的手机屏幕(240x320 ldpi、320x480 mdpi、480x800 hdpi 等).
  • 480dp:像 Streak 一样的 tweener 平板电脑 (480x800 mdpi).
  • 600dp:7 英寸平板电脑 (600x1024 mdpi).
  • 720dp:10 英寸平板电脑(720x1280 mdpi、800x1280 mdpi 等).

根据以上信息,我们知道如果设备的最小宽度大于600dp,则设备为7"平板电脑,如果大于720dp,则设备为10"平板电脑.

我们可以使用Math类的min函数计算出最小宽度,传入heightDpwidthDp 返回smallestWidth.

float minimumWidth = Math.min(widthDp, heightDp);如果(最小宽度 > 720){//设备为10"平板电脑}否则如果(smallestWidth > 600){//设备为7寸平板}

然而,这并不总能给你一个完全匹配的结果,尤其是在使用一些晦涩的平板电脑时,这些平板电脑可能会误将其密度表示为 hdpi,或者可能只有 800 x 480 像素但仍然在7" 屏幕.

除了这些方法之外,如果您需要知道设备的确切尺寸(以英寸为单位),您也可以使用 metrics 方法计算出每英寸有多少像素屏幕.

float widthDpi = metrics.xdpi;浮动高度Dpi = metrics.ydpi;

您可以使用每英寸设备中有多少像素和总像素数的知识来计算设备有多少英寸.

float widthInches = widthPixels/widthDpi;float heightInches = heightPixels/heightDpi;

这将以英寸为单位返回设备的高度和宽度.这对于确定它是什么类型的设备并不总是那么有帮助,因为设备的广告尺寸是对角线,我们只有高度和宽度.

然而,我们也知道给定三角形的高度和宽度,我们可以使用勾股定理来计算斜边的长度(在这种情况下,屏幕对角线的大小).

//a² + b² = c²//以英寸为单位的对角线的大小等于以英寸为单位的高度的平方加上以英寸为单位的宽度的平方根.double diagonalInches = Math.sqrt((宽度英寸 * 宽度英寸)+ (heightInches * heightInches));

由此,我们可以确定设备是否为平板电脑:

if (diagonalInches >= 10) {//设备为10"平板电脑}否则如果(对角线英寸 >= 7){//设备为7寸平板}

这就是您计算您使用的设备类型的方式.

Is there a way to programmatically find whether the device the app is installed on is a 7 inch tablet or a 10 inch tablet?

解决方案

You can use the DisplayMetrics to get a whole bunch of information about the screen that your app is running on.

First, we create a DisplayMetrics metrics object:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

From this, we can get the information required to size the display:

int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;

This will return the absolute value of the width and the height in pixels, so 1280x720 for the Galaxy SIII, the Galaxy Nexus etc.

This isn't usually helpful on its own, as when we're working on Android devices, we usually prefer to work in density independent pixels, dip.

You get the density of the screen using metrics again, in the form of a scale factor for the device, which is based on the Android Design Resources for mdpi, hdpi etc.

float scaleFactor = metrics.density;

From this result, we can calculate the amount of density independent pixels there are for a certain height or width.

float widthDp = widthPixels / scaleFactor
float heightDp = heightPixels / scaleFactor

The result you get from this will help you decide what type of screen you are working with in conjunction with the Android Configuration examples, which give you the relative dp for each screen size:

  • 320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
  • 480dp: a tweener tablet like the Streak (480x800 mdpi).
  • 600dp: a 7" tablet (600x1024 mdpi).
  • 720dp: a 10" tablet (720x1280 mdpi, 800x1280 mdpi, etc).

Using the above information, we know that if the smallest-width of the device is greater than 600dp, the device is a 7" tablet, if it's greater than 720dp, the device is a 10" tablet.

We can work out the smallest width using the min function of Math class, passing in the heightDp and the widthDp to return the smallestWidth.

float smallestWidth = Math.min(widthDp, heightDp);

if (smallestWidth > 720) {
    //Device is a 10" tablet
} 
else if (smallestWidth > 600) {
    //Device is a 7" tablet
}

However, this doesn't always give you an exact match, especially when working with obscure tablets that might be misrepresenting their density as hdpi when it isn't, or that might only be 800 x 480 pixels yet still be on a 7" screen.

Further to these methods, if you ever need to know the exact dimensions of a device in inches, you can work that out too, using the metrics method for how many pixels there are per inch of the screen.

float widthDpi = metrics.xdpi;
float heightDpi = metrics.ydpi;

You can use the knowledge of how many pixels are in each inch of device and the amount of pixels in total to work out how many inches the device is.

float widthInches = widthPixels / widthDpi;
float heightInches = heightPixels / heightDpi;

This will return the height and width of the device in inches. This again isn't always that helpful for determining what type of device it is, as the advertised size of a device is the diagonal, all we have is the height and the width.

However, we also know that given the height of a triangle and the width, we can use the Pythagorean theorem to work out the length of the hypotenuse (In this case, the size of the screen diagonal).

//a² + b² = c²

//The size of the diagonal in inches is equal to the square root of the height in inches squared plus the width in inches squared.
double diagonalInches = Math.sqrt(
    (widthInches * widthInches) 
    + (heightInches * heightInches));

From this, we can work out whether the device is a tablet or not:

if (diagonalInches >= 10) {
    //Device is a 10" tablet
} 
else if (diagonalInches >= 7) {
    //Device is a 7" tablet
}

And that's how you calculate what kind of device you're working with.

这篇关于以编程方式检测 7 英寸和 10 英寸平板电脑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆