检测7英寸和10英寸平板电脑编程平板、电脑

2023-09-12 00:22:46 作者:抄抄抄,学生的妙招

有没有一种方法以编程方式找到该应用程序安装在设备是7寸的平板电脑或10英寸的平板电脑?

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?

推荐答案

您可以使用 DisplayMetrics ,以获取有关屏幕,你的应用程序正在运行一大堆信息上。

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

首先,我们创建一个 DisplayMetrics 规格对象:

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;

这将返回宽度的绝对值和像素高度,所以1280×720的银河SIII,银河的Nexus等。

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

这是通常不是对自己有帮助的,当我们的工作在Android设备上的,我们平时preFER密度独立的像素工作,畅游。

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.

您获得密度使用度量屏幕再次,在一个比例系数为设备的形式,这是基于 Android的设计资源为 MDPI 华电国际等。

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

从此得到的结果将帮助您确定您正在使用与Android配置范例,它给你的每一个屏幕尺寸相对DP:

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:一个典型的手机屏幕(240×320 LDPI,小320x480 MDPI,480x800的华电国际,等等)。    480dp:一个中间人的平板状的条纹(480×800 MDPI)。    600dp:7平板电脑(600x1024 MDPI)。    720dp:10平板电脑(720x1280 MDPI,800x1280 MDPI等)    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).

使用上述信息,我们知道,如果最小宽度的装置的大于600dp,该装置是一个7片剂,如果它是大于720dp,该装置是一个10片剂。

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.

我们可以使用数学类的分函数,传递 heightDp 和 widthDp 返回 smallestWidth

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
}

不过,这并不总是给你一个确切的比赛,尤其是与晦涩的平板电脑工作时,可能是misre presenting它们的密度为华电国际时,它不是,或者说可能只有800×480像素,但还是在7屏幕。

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.