0

如何判断Android设备是手机还是pad,我找不到Android API的一些方法。现在我根据设备尺寸来判断,if(size > 6) -->pad else ---> phone,可以吗有另一种解决方案

4

3 回答 3

5

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

你需要问问自己,为什么?
- 有 7 英寸 + 具有电话功能的设备。
- 有 5 英寸 - 没有电话功能的设备。
- 传感器因设备而异,无论大小。
- 有可能属于任一类别的平板手机。

所以,如果我对“电话”的定义是“它可以打电话吗?” 然后 ...

TelephonyManager manager = 
        (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE)
{ // it has no phone 
}
于 2013-07-01T02:42:54.413 回答
0

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

/**
 * 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;
}
于 2013-07-01T02:36:11.770 回答
0

我找到了缩放位图的最佳方法,例如在游戏意义上,大致确定你希望你的图像占据屏幕的百分比,例如,如果我有一个播放器并且我有一个 256x256 分辨率的图像并且我处于纵向模式,我希望图像占据屏幕宽度的大约 33%,我按屏幕宽度的百分比而不是硬编码值缩放图像,然后无论你在什么屏幕上,一切都会调整大小。代码例如:

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;

}
于 2019-08-26T00:58:59.667 回答