如何检测7英寸Android平板电脑在code [英] How to detect 7" Android tablet in code

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

问题描述

我想检测我的code 7平板电脑(即的Kindle Fire和放大器; Nook Color的),但只检查为最小尺寸1024x600的并不好,因为那时Galaxy Nexus的将通过这个测试,以及。任何人有与检测这种信息的经验?

谢谢, 伊戈尔

编辑: 我发现,检测的Kindle Fire和放大器的一种方式; Nook Color的设备具有以下code:

 活动活动=(活动)activityContext;
。activity.getWindowManager()getDefaultDisplay()getMetrics(度量)。
INT宽度= activity.getWindowManager()getDefaultDisplay()的getWidth()。
INT高= activity.getWindowManager()getDefaultDisplay()的getHeight()。
如果((宽== 1024安培;&安培;高度== 600)||(宽== 600安培;&安培;高度== 1024)){
        //检测7平板电脑:即的Kindle Fire和放大器;角落颜色
        isTablet = TRUE;
    }
 

解决方案

要计算设备的高度和宽度(以英寸为单位),可以使用下面的code。

  DisplayMetrics指标=新DisplayMetrics();
。getWindowManager()getDefaultDisplay()getMetrics(度量)。

浮widthInInches = metrics.widthPixels / metrics.xdpi;
浮heightInInches = metrics.heightPixels / metrics.ydpi;
 

然后,大小可以计算

 双sizeInInches =的Math.sqrt(Math.pow(widthInInches,2)+ Math.pow(heightInInches,2));
//0.5缓冲7设备
布尔is7inchTablet = sizeInInches> = 6.5安培;&安培; sizeInInches< = 7.5;
 

根据Commonsware建议以上,潜在快,但不太明显的执行情况。

 双sizeInInchesSquared =(widthInInches * widthInInches)+(heightInInches * heightInInches);
//0.5缓冲7设备(6.5 ^ 2 = 42.25)(7.5 ^ 2 = 56.25)
布尔is7inchTablet = sizeInInchesSquared> = 42.25和放大器;&安培; sizeInInchesSquared< = 56.25;
 

I am trying to detect 7" tablets in my code (i.e. Kindle Fire & Nook Color). However, simply testing for minimum dimensions 1024x600 is not good, because then the Galaxy Nexus would pass this test as well. Anybody has experience with detecting this kind of information?

Thanks, Igor

EDIT: I have found one way to detect Kindle Fire & Nook Color devices with the following code:

Activity activity = (Activity) activityContext;
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
if ((width == 1024 && height == 600) || (width == 600 && height == 1024)) {    
        //Detects 7" tablets: i.e. Kindle Fire & Nook Color
        isTablet = true;
    }

解决方案

To calculate the height and width of the device (in inches) you can use the following code.

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

float widthInInches = metrics.widthPixels / metrics.xdpi;
float heightInInches = metrics.heightPixels / metrics.ydpi;

Then the size can be calculated

double sizeInInches = Math.sqrt(Math.pow(widthInInches, 2) + Math.pow(heightInInches, 2));
//0.5" buffer for 7" devices
boolean is7inchTablet = sizeInInches >= 6.5 && sizeInInches <= 7.5; 

As per Commonsware suggestion above, a potentially faster, but less obvious implementation.

double sizeInInchesSquared = (widthInInches * widthInInches) + (heightInInches * heightInInches);
//0.5" buffer for 7" devices (6.5^2 = 42.25) (7.5^2 = 56.25)
boolean is7inchTablet = sizeInInchesSquared >= 42.25 && sizeInInchesSquared <= 56.25; 

这篇关于如何检测7英寸Android平板电脑在code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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