简体   繁体   English

通过代码[8英寸与10英寸平板电脑]区分Android设备

[英]Differentiate android devices via code [8inch vs 10 inch tablet]

I am coding my application for two specific devices 我正在为两个特定设备编写应用程序

Dell Venue 8 8inch 戴尔Venue 8 8英寸

Samsung Galaxy Note 10.1 10inch 三星Galaxy Note 10.1 10英寸

In my code i want to write code differently for each of these devices for a small module but i am not able to differentiate between the two 在我的代码中,我想为一个小模块为每个设备编写不同的代码,但我无法区分这两个

I have used 我用过

double density = getResources().getDisplayMetrics().density;

but it returns the same for both 1.3312500715255737 但它同样返回1.3312500715255737

using getDisplayMetrics() 使用getDisplayMetrics()

i tried to get the resolution but it returned the same for both 我试图获得分辨率,但两者都返回相同

1280x720 1280×720

so what do i use to differentiate between both these devices in my code? 那我在代码中用什么来区分这两个设备呢?

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指标对象:

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;

This will return the absolute value of the width and the height in pixels, so 1280x720 for the Galaxy SIII, the Galaxy Nexus etc. 这将返回宽度和高度的绝对值(以像素为单位),因此Galaxy SIII,Galaxy Nexus等为1280x720。

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设备上工作时,我们通常更喜欢使用与密度无关的像素,dip。

float scaleFactor = displaymetrics .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

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. 使用上述信息,我们知道如果设备的最小宽度大于600dp,设备是7“平板电脑,如果它大于720dp,设备是10”平板电脑。

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. 我们可以使用Math类的min函数计算出最小的宽度,传入heightDp和widthDp以返回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. 但是,这并不总能给你一个完全匹配,特别是当使用模糊的平板电脑时可能会将其密度误认为hdpi,或者可能只有800 x 480像素但仍然在7英寸屏幕上。

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 = displaymetrics .xdpi;
float heightDpi = displaymetrics .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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使10英寸平板电脑代码与Android中的7英寸平板电脑兼容 - How make 10 inch tablet code compatible to 7 inch tablet in android 8英寸平板电脑的布局和可绘制文件夹 - Layout and Drawable folder for 8inch Tablet 仅发布7和10英寸平板电脑的Android应用程序 - Publish Android application for 7 and 10 inch tablet only 以编程方式检测 7 英寸和 10 英寸平板电脑 - Detect 7 inch and 10 inch tablet programmatically 支持9.6英寸和10.1英寸android平板电脑 - Supporting 9.6 inch and 10.1 inch android tablet Android:手电筒可以在Galaxy 10英寸平板电脑上正常工作,但不能在Galaxy 7英寸平板电脑上正常工作 - Android: Flashlight working fine in Galaxy 10 inch Tablet but not in Galaxy 7 inch Tablet 如何在7英寸台式机和10英寸平板电脑上运行Android应用 - How to run an Android app on both 7-inch table and 10-inch tablet Android:数字浮动键盘会阻止10英寸平板电脑上的EditText - Android: The numeric floating keyboard blocks EditText on 10 inch tablet 如何在Android中仅以人像模式设计10英寸平板电脑? - How to design in 10 inch tablet only in portrait mode in Android? 查看分页器无法在Real Device Tablet Android 10英寸中正常工作 - View Pager not work properly in Real Device Tablet Android 10 inch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM