通过差异化code Android设备[8英寸对10英寸平板电脑] [英] Differentiate android devices via code [8inch vs 10 inch tablet]

查看:141
本文介绍了通过差异化code Android设备[8英寸对10英寸平板电脑]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码我申请两个特定设备

I am coding my application for two specific devices

戴尔Venue 8 8寸

三星Galaxy Note 10.1 10寸

在我的codeI想写code不同的每个设备的一个小模块,但我不能够区分两个

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

我已经用

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

但它返回相同的两个 1.3312500715255737

but it returns the same for both 1.3312500715255737

使用 getDisplayMetrics()

我试图让该决议,但它返回相同的两个

i tried to get the resolution but it returned the same for both

1280×720

让我怎么使用我的code这两种设备之间的区别?

so what do i use to differentiate between both these devices in my code?

推荐答案

您可以使用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 displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;

这将返回宽度的绝对值和像素高度,所以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.

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

使用上述信息,我们知道,如果最小宽度的装置的大于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.

我们可以计算出的最小宽度使用Math类的分功能,传入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 = 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
}

这篇关于通过差异化code Android设备[8英寸对10英寸平板电脑]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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