首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何判断Android设备是手机还是平板电脑?

如何判断Android设备是手机还是平板电脑?
EN

Stack Overflow用户
提问于 2013-07-01 10:33:03
回答 3查看 1.3K关注 0票数 0

如何判断Android设备是手机还是平板,我找不到一些Android API.Now的方法我是根据设备尺寸来判断的,如果(size> 6) -->pad ->手机,有没有别的解决方案?

EN

回答 3

Stack Overflow用户

发布于 2013-07-01 10:42:54

我知道这不是你想听到的,但你不会区分手机和平板电脑。

你需要问问你自己,为什么?

  • 有超过7英寸的设备具有电话功能。
  • 有一些5英寸的设备没有电话功能。
  • 传感器因设备而异,大小不一。
  • 有些平板手机可能属于这两种类型。

那么,如果我对“电话”的定义是,“它能打电话吗?”然后..。

代码语言:javascript
运行
复制
TelephonyManager manager = 
        (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE)
{ // it has no phone 
}
票数 5
EN

Stack Overflow用户

发布于 2013-07-01 10:36:12

这是一个你可以检查设备是否是平板电脑的功能。

代码语言:javascript
运行
复制
/**
 * Checks if the device is a tablet or a phone
 * 
 * @param activityContext
 *            The Activity Context.
 * @return Returns true if the device is a Tablet
 */
public static boolean isTabletDevice(Context activityContext) {
    // Verifies if the Generalized Size of the device is XLARGE to be
    // considered a Tablet
    boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout & 
                        Configuration.SCREENLAYOUT_SIZE_MASK) == 
                        Configuration.SCREENLAYOUT_SIZE_XLARGE);

    // If XLarge, checks if the Generalized Density is at least MDPI
    // (160dpi)
    if (xlarge) {
        DisplayMetrics metrics = new DisplayMetrics();
        Activity activity = (Activity) activityContext;
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,
        // DENSITY_TV=213, DENSITY_XHIGH=320
        if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
                || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {

            // Yes, this is a tablet!
            return true;
        }
    }

    // No, this is not a tablet!
    return false;
}
票数 0
EN

Stack Overflow用户

发布于 2019-08-26 08:59:00

我已经找到了缩放位图的最好方法,在游戏意义上,就是粗略地计算出你想要你的图像占据屏幕的百分比。例如,如果我有一个播放器,我有一张256x256分辨率的图像,我在纵向模式下,我想要图像占据大约33%的屏幕宽度,我按照屏幕宽度的百分比而不是硬编码值来缩放图像,然后无论你在什么屏幕上,所有的东西都会调整大小。代码示例:

代码语言:javascript
运行
复制
private RectF rect;
private Bitmap bitmap;
private int width;

CritterPlayer(Context context, int screenX, int screenY){
    rect = new RectF();

    //percentage of screen
    width = screenX / 3;

//load bitmap
    bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.player);

//scale bitmap
    bitmap = Bitmap.createScaledBitmap(bitmap,
            width,
            width,
            false);
//get center
    x = (screenX - bitmap.getWidth()) / 2;
    y = (screenY - bitmap.getHeight()) / 2;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17396726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档