Created
April 14, 2015 02:42
-
-
Save rbtr/da92723f2f1350b6e991 to your computer and use it in GitHub Desktop.
A Screen Size Utility Class for Android
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
/** | |
* This class is just a helper package to get the screen dimensions | |
*/ | |
public class ScreenSizeUtils { | |
// helper classes | |
private Context mContext; | |
private WindowManager mWm; | |
private Display mDisplay; | |
private Configuration mConfig; | |
private DisplayMetrics mDisplayMetrics, mRealDisplayMetrics; | |
// helper attributes | |
private static final int Unknown = -255; | |
private int width_px, height_px; | |
private int width_dp, height_dp; | |
//values | |
private String widthxheight_px; | |
private String widthxheight_dp; | |
public ScreenSizeUtils(Context mContext) { | |
this.mContext = mContext; | |
init(); | |
} | |
private ScreenSizeUtils init() { | |
mWm = ((WindowManager) mContext | |
.getSystemService(Context.WINDOW_SERVICE)); | |
mDisplay = mWm.getDefaultDisplay(); | |
mConfig = mContext.getResources().getConfiguration(); | |
mDisplayMetrics = new DisplayMetrics(); | |
mDisplay.getMetrics(mDisplayMetrics); | |
if (Build.VERSION.SDK_INT >= 17) { | |
mRealDisplayMetrics = new DisplayMetrics(); | |
mDisplay.getRealMetrics(mRealDisplayMetrics); | |
} | |
GET_widthxheight_dp(); | |
return this; | |
} | |
public String GET_widthxheight_px() { | |
if (Build.VERSION.SDK_INT < 13) { | |
width_px = mDisplay.getWidth(); | |
height_px = mDisplay.getHeight(); | |
} else { | |
Point mPoint = new Point(); | |
mDisplay.getSize(mPoint); | |
width_px = mPoint.x; | |
height_px = mPoint.y; | |
} | |
widthxheight_px = width_px + " x " + height_px; | |
return widthxheight_px; | |
} | |
public String GET_widthxheight_dp() { | |
int real_width_px = Unknown; | |
int real_height_px = Unknown; | |
if (Build.VERSION.SDK_INT >= 17) { | |
real_width_px = mRealDisplayMetrics.widthPixels; | |
real_height_px = mRealDisplayMetrics.heightPixels; | |
} else { | |
//it is necessary if you haven't called it before | |
GET_widthxheight_px(); | |
} | |
height_dp = (int) (((double) ((real_height_px == Unknown) ? height_px | |
: real_height_px) / mDisplayMetrics.density) + 0.5); | |
if (Build.VERSION.SDK_INT >= 13) { | |
width_dp = mConfig.screenWidthDp; | |
} else { | |
width_dp = (int) (((double) ((real_width_px == Unknown) ? width_px | |
: real_width_px) / mDisplayMetrics.density) + 0.5); | |
} | |
widthxheight_dp = width_dp + " x " + height_dp; | |
return widthxheight_dp; | |
} | |
public int getWidth_px() { | |
return width_px; | |
} | |
public int getHeight_px() { | |
return height_px; | |
} | |
public int getWidth_dp() { | |
return width_dp; | |
} | |
public int getHeight_dp() { | |
return height_dp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment