Created
April 7, 2015 05:25
-
-
Save leelei/1f2c4006afa8e4a17715 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package net.coding.lucas.screenheight; | |
import android.content.Context; | |
import android.content.res.Configuration; | |
import android.content.res.Resources; | |
import android.graphics.Point; | |
import android.os.Build; | |
import android.util.DisplayMetrics; | |
import android.util.TypedValue; | |
import android.view.Display; | |
import android.view.WindowManager; | |
/** | |
* Created by leefei on 15-4-7. | |
*/ | |
public class ScreenUtils { | |
private static Context mContext; | |
private static int sContentPortraitHeight = -1; | |
private static int sContentLandHeight = -1; | |
private static int sContentPortraitWdith = -1; | |
private static int sContentLandWidth = -1; | |
private static int sActionBarPortraitHeight = -1; | |
private static int sActionBarLandHeight = -1; | |
private static int sScreenPortraitWidth = -1; | |
private static int sScreenPortraitHeight = -1; | |
private static int sScreenLandWidth = -1; | |
private static int sScreenLandHeight = -1; | |
private ScreenUtils() { | |
} | |
public static void init(Context context){ | |
mContext = context.getApplicationContext(); | |
} | |
public static int dpToPx(int dp) { | |
return (int) (dp * mContext.getResources().getDisplayMetrics().density); | |
} | |
public static int pxToDp(int px) { | |
return (int) (px / mContext.getResources().getDisplayMetrics().density); | |
} | |
private static Context getContext() { | |
return mContext; | |
} | |
/** | |
* @return [true] if the device has a small screen, [false] otherwise. | |
*/ | |
public static boolean hasSmallScreen() { | |
return getScreenType() == Configuration.SCREENLAYOUT_SIZE_SMALL; | |
} | |
/** | |
* @return [true] if the device has a normal screen, [false] otherwise. | |
*/ | |
public static boolean hasNormalScreen() { | |
return getScreenType() == Configuration.SCREENLAYOUT_SIZE_NORMAL; | |
} | |
/** | |
* @return [true] if the device has a large screen, [false] otherwise. | |
*/ | |
public static boolean hasLargeScreen() { | |
return getScreenType() == Configuration.SCREENLAYOUT_SIZE_LARGE; | |
} | |
/** | |
* @return [true] if the device has an extra large screen, [false] otherwise. | |
*/ | |
public static boolean hasXLargeScreen() { | |
return getScreenType() == Configuration.SCREENLAYOUT_SIZE_XLARGE; | |
} | |
public static boolean isTablet(Context context) { | |
return (context.getResources().getConfiguration() | |
.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) | |
>= Configuration.SCREENLAYOUT_SIZE_LARGE; | |
} | |
//public static boolean isTablet(Resources res) { | |
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { | |
// return mContext.getResources().getConfiguration().smallestScreenWidthDp | |
// >= 600; | |
// } else { // for devices without smallestScreenWidthDp reference calculate if device screen is over 600 | |
// return (res.getDisplayMetrics().widthPixels / res.getDisplayMetrics().density) >= 600; | |
// } | |
//} | |
public static int getDrawerWidth() { | |
Resources res = mContext.getResources(); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { | |
if (res.getConfiguration().smallestScreenWidthDp >= 600 | |
|| res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { | |
// device is a tablet | |
return (int) (320 * res.getDisplayMetrics().density); | |
} else { | |
return (int) (res.getDisplayMetrics().widthPixels - (56 | |
* res.getDisplayMetrics().density)); | |
} | |
} else { // for devices without smallestScreenWidthDp reference calculate | |
// if device screen is over 600 dp | |
if ((res.getDisplayMetrics().widthPixels / res.getDisplayMetrics().density) >= 600 | |
|| res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { | |
return (int) (320 * res.getDisplayMetrics().density); | |
} else { | |
return (int) (res.getDisplayMetrics().widthPixels - (56 | |
* res.getDisplayMetrics().density)); | |
} | |
} | |
} | |
public static int getStatusBarHeight() { | |
int resourceId = mContext .getResources().getIdentifier("status_bar_height", "dimen", | |
"android"); | |
if (resourceId > 0) { | |
return mContext.getResources().getDimensionPixelSize(resourceId); | |
} | |
return 0; | |
} | |
public static int getActionBarHeight() { | |
if (isLandscape()) { | |
if (sActionBarLandHeight == -1) { | |
sActionBarLandHeight = resolveActionBarHeight(); | |
} | |
return sActionBarLandHeight; | |
} else { | |
if (sActionBarPortraitHeight == -1) { | |
sActionBarPortraitHeight = resolveActionBarHeight(); | |
} | |
return sActionBarPortraitHeight; | |
} | |
} | |
public static int getMaterialActionBarHeight() { | |
return mContext.getResources().getDimensionPixelSize( | |
R.dimen.abc_action_bar_default_height_material); | |
} | |
public static int getNavigationBarHeight() { | |
Resources resources = getContext().getResources(); | |
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); | |
if (resourceId > 0) { | |
return resources.getDimensionPixelSize(resourceId); | |
} | |
return 0; | |
} | |
public static int getScreenWidth() { | |
if (isLandscape()) { | |
if (sScreenLandWidth == -1) { | |
resolveScreenSize(); | |
} | |
return sScreenLandWidth; | |
} else { | |
if (sScreenPortraitWidth == -1) { | |
resolveScreenSize(); | |
} | |
return sScreenPortraitWidth; | |
} | |
} | |
/** | |
* include StatusBarHeight | |
* @return | |
*/ | |
public static int getScreenHeight() { | |
if (isLandscape()) { | |
if (sScreenLandHeight == -1) { | |
resolveScreenSize(); | |
} | |
return sScreenLandHeight; | |
} else { | |
if (sScreenPortraitHeight == -1) { | |
resolveScreenSize(); | |
} | |
return sScreenPortraitHeight; | |
} | |
} | |
/** | |
* exclude NavigationBarHeight and StatusBarHeight | |
* @return | |
*/ | |
public static int getContentHeight(){ | |
if(isLandscape()){ | |
if(sContentLandHeight==-1){ | |
Point point = resolveContentSize(); | |
sContentLandHeight = point.y; | |
sContentLandWidth = point.x; | |
} | |
return sContentLandHeight; | |
}else{ | |
//portrait | |
if(sContentPortraitHeight==-1){ | |
Point point = resolveContentSize(); | |
sContentPortraitHeight = point.y; | |
sContentPortraitWdith = point.x; | |
} | |
return sContentPortraitHeight; | |
} | |
} | |
/** | |
* exclude NavigationBarHeight | |
* @return | |
*/ | |
public static int getContentWidth(){ | |
if(isLandscape()){ | |
if(sContentLandWidth==-1){ | |
Point point = resolveContentSize(); | |
sContentLandHeight = point.y; | |
sContentLandWidth = point.x; | |
} | |
return sContentLandWidth; | |
}else{ | |
//portrait | |
if(sContentPortraitWdith==-1){ | |
Point point = resolveContentSize(); | |
sContentPortraitHeight = point.y; | |
sContentPortraitWdith = point.x; | |
} | |
return sContentPortraitWdith; | |
} | |
} | |
public static boolean isLandscape() { | |
return mContext.getResources().getConfiguration().orientation | |
== Configuration.ORIENTATION_LANDSCAPE; | |
} | |
public static int getScreenType() { | |
return mContext.getResources().getConfiguration().screenLayout | |
& Configuration.SCREENLAYOUT_SIZE_MASK; | |
} | |
///** | |
// * In Lollipop include NavigationBar | |
// * @param view | |
// */ | |
//public static void getRootView(final View view){ | |
// view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { | |
// @Override public boolean onPreDraw() { | |
// view.getRootView().getHeight(); | |
// return true; | |
// } | |
// }); | |
//} | |
private static Point resolveContentSize() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { | |
Configuration configuration = mContext.getResources().getConfiguration(); | |
return new Point(dpToPx(configuration.screenWidthDp), dpToPx(configuration.screenHeightDp)); | |
} else { | |
// APIs prior to v13 gave the screen dimensions in pixels. | |
// We convert them to DIPs before returning them. | |
WindowManager windowManager = | |
(WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); | |
DisplayMetrics displayMetrics = new DisplayMetrics(); | |
windowManager.getDefaultDisplay().getMetrics(displayMetrics); | |
return new Point(displayMetrics.widthPixels, displayMetrics.heightPixels); | |
} | |
} | |
private static int resolveActionBarHeight() { | |
TypedValue typedValue = new TypedValue(); | |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) { | |
mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, typedValue, true); | |
} else { | |
mContext.getTheme().resolveAttribute(R.attr.actionBarSize, typedValue, true); | |
} | |
return dpToPx(typedValue.data); | |
} | |
private static void resolveScreenSize() { | |
WindowManager w = | |
(WindowManager) mContext | |
.getSystemService(Context.WINDOW_SERVICE); | |
Display d = w.getDefaultDisplay(); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { | |
Point size = new Point(); | |
d.getSize(size); | |
if (isLandscape()) { | |
sScreenLandWidth = size.x; | |
sScreenLandHeight = size.y; | |
} else { | |
sScreenPortraitWidth = size.x; | |
sScreenPortraitHeight = size.y; | |
} | |
} else { | |
if (isLandscape()) { | |
sScreenLandWidth = d.getWidth(); | |
sScreenLandHeight = d.getHeight(); | |
} else { | |
sScreenPortraitWidth = d.getWidth(); | |
sScreenPortraitHeight = d.getHeight(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment