Reputation: 9
How to judge whether the Android device is phone or pad,I can not find some method of Android API.Now I judge it based on device dimensions, if(size > 6) -->pad else ---> phone,does it have another solution
Upvotes: 0
Views: 1488
Reputation: 21
I've found the best way to scale bitmaps and such in a gaming sense is to figure out roughly what percentage of the screen u want ur images to take up for example if i have a player and i have an image of 256x256 resolution and i was in portrait mode and i wanted the image to take up roughly 33% of the screens width, i scale the image by that percentage of the screens width rather than a hard coded value, then everything resizes no matter what screen you are on. code for example:
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;
}
Upvotes: 0
Reputation: 2352
I know this is not what you want to hear, but you don't distinguish between phone or tablet.
You need to ask yourself, why?
- There are 7inch + devices with phone functionality.
- There are 5inch - devices without phone functionality.
- Sensors vary between devices, large and small.
- There are phablets which may fall into either category.
So, if my definition of a "phone" is, "can it make phone calls?" then ...
TelephonyManager manager =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE)
{ // it has no phone
}
Upvotes: 5
Reputation: 13535
Here is a function that you can check if the device is a tablet.
/**
* 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;
}
Upvotes: 0