如何判断Android设备是手机还是平板,我找不到一些Android API.Now的方法我是根据设备尺寸来判断的,如果(size> 6) -->pad ->手机,有没有别的解决方案?
发布于 2013-07-01 10:42:54
我知道这不是你想听到的,但你不会区分手机和平板电脑。
你需要问问你自己,为什么?
那么,如果我对“电话”的定义是,“它能打电话吗?”然后..。
TelephonyManager manager =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE)
{ // it has no phone
}
发布于 2013-07-01 10:36:12
这是一个你可以检查设备是否是平板电脑的功能。
/**
* 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;
}
发布于 2019-08-26 08:59:00
我已经找到了缩放位图的最好方法,在游戏意义上,就是粗略地计算出你想要你的图像占据屏幕的百分比。例如,如果我有一个播放器,我有一张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;
}
https://stackoverflow.com/questions/17396726
复制相似问题