Created
May 10, 2016 09:46
-
-
Save thebestpol/988b0ccd7753ceb4471f03130e1eee36 to your computer and use it in GitHub Desktop.
Class with util methods about screen size and density
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Class with util methods about screen size and density | |
*/ | |
public class DensityUtils { | |
/** | |
* Converts the given device independent pixels (DIP) value into the corresponding pixels | |
* value for the current screen. | |
* | |
* @param context Context instance | |
* @param dip The DIP value to convert | |
* | |
* @return The pixels value for the current screen of the given DIP value. | |
*/ | |
public static int convertDIPToPixels(Context context, int dip) { | |
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); | |
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, displayMetrics); | |
} | |
/** | |
* Converts the given device independent pixels (DIP) value into the corresponding pixels | |
* value for the current screen. | |
* | |
* @param context Context instance | |
* @param dip The DIP value to convert | |
* | |
* @return The pixels value for the current screen of the given DIP value. | |
*/ | |
public static int convertDIPToPixels(Context context, float dip) { | |
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); | |
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, displayMetrics); | |
} | |
/** | |
* Converts the given pixels value into the corresponding device independent pixels (DIP) | |
* value for the current screen. | |
* | |
* @param context Context instance | |
* @param pixels The pixels value to convert | |
* | |
* @return The DIP value for the current screen of the given pixels value. | |
*/ | |
public static float convertPixelsToDIP(Context context, int pixels) { | |
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); | |
return pixels / (displayMetrics.densityDpi / 160f); | |
} | |
/** | |
* Returns the current screen dimensions in device independent pixels (DIP) as a {@link android.graphics.Point} | |
* object where | |
* {@link android.graphics.Point#x} is the screen width and {@link android.graphics.Point#y} is the screen height. | |
* | |
* @param context Context instance | |
* | |
* @return The current screen dimensions in DIP. | |
*/ | |
public static Point getScreenDimensionsInDIP(Context context) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { | |
Configuration configuration = context.getResources().getConfiguration(); | |
return new Point(configuration.screenWidthDp, configuration.screenHeightDp); | |
} else { | |
// APIs prior to v13 gave the screen dimensions in pixels. | |
// We convert them to DIPs before returning them. | |
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | |
DisplayMetrics displayMetrics = new DisplayMetrics(); | |
windowManager.getDefaultDisplay().getMetrics(displayMetrics); | |
int screenWidthInDIP = (int) convertPixelsToDIP(context, displayMetrics.widthPixels); | |
int screenHeightInDIP = (int) convertPixelsToDIP(context, displayMetrics.heightPixels); | |
return new Point(screenWidthInDIP, screenHeightInDIP); | |
} | |
} | |
/** | |
* @param context Context to get screen width and device specific display metrics | |
* | |
* @return an int value to represent the px value | |
*/ | |
public static int getScreenWidth(Context context) { | |
DisplayMetrics metrics = new DisplayMetrics(); | |
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); | |
Point size = new Point(); | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) { | |
size.set(display.getWidth(), display.getHeight()); | |
} else { | |
display.getSize(size); | |
} | |
return size.x; | |
} | |
/** | |
* @param context Context to get screen width and device specific display metrics | |
* | |
* @return an int value to represent the px value | |
*/ | |
public static int getScreenHeight(Context context) { | |
DisplayMetrics metrics = new DisplayMetrics(); | |
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); | |
Point size = new Point(); | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) { | |
size.set(display.getWidth(), display.getHeight()); | |
} else { | |
display.getSize(size); | |
} | |
return size.y; | |
} | |
/** | |
* @param context Context instance | |
* | |
* @return [true] if the device is in landscape orientation, [false] otherwise. | |
*/ | |
public static boolean isInLandscapeOrientation(Context context) { | |
Configuration configuration = context.getResources().getConfiguration(); | |
return configuration.orientation == Configuration.ORIENTATION_LANDSCAPE; | |
} | |
/** | |
* @param context Context instance | |
* | |
* @return [true] if the device has a small screen, [false] otherwise. | |
*/ | |
public static boolean hasSmallScreen(Context context) { | |
return getScreenSize(context) == Configuration.SCREENLAYOUT_SIZE_SMALL; | |
} | |
/** | |
* @param context Context instance | |
* | |
* @return [true] if the device has a normal screen, [false] otherwise. | |
*/ | |
public static boolean hasNormalScreen(Context context) { | |
return getScreenSize(context) == Configuration.SCREENLAYOUT_SIZE_NORMAL; | |
} | |
/** | |
* @param context Context instance | |
* | |
* @return [true] if the device has a large screen, [false] otherwise. | |
*/ | |
public static boolean hasLargeScreen(Context context) { | |
return getScreenSize(context) == Configuration.SCREENLAYOUT_SIZE_LARGE; | |
} | |
/** | |
* @param context Context instance | |
* | |
* @return [true] if the device has an extra large screen, [false] otherwise. | |
*/ | |
public static boolean hasXLargeScreen(Context context) { | |
return getScreenSize(context) == Configuration.SCREENLAYOUT_SIZE_XLARGE; | |
} | |
/** | |
* The size of the screen, one of 4 possible values: | |
* <p> | |
* <ul> | |
* <li>http://developer.android.com/reference/android/content/res/Configuration.html#SCREENLAYOUT_SIZE_SMALL</li> | |
* <li>http://developer.android.com/reference/android/content/res/Configuration.html#SCREENLAYOUT_SIZE_NORMAL</li> | |
* <li>http://developer.android.com/reference/android/content/res/Configuration.html#SCREENLAYOUT_SIZE_LARGE</li> | |
* <li>http://developer.android.com/reference/android/content/res/Configuration.html#SCREENLAYOUT_SIZE_XLARGE</li> | |
* </ul> | |
* <p> | |
* See http://developer.android.com/reference/android/content/res/Configuration.html#screenLayout for more details. | |
* | |
* @param context Context instance | |
* | |
* @return The size of the screen | |
*/ | |
public static int getScreenSize(Context context) { | |
Configuration configuration = context.getResources().getConfiguration(); | |
return configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment