Last active
August 29, 2015 13:57
-
-
Save togramago/dfccc387452ccffb0d16 to your computer and use it in GitHub Desktop.
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
/** | |
* Manages dimensions (px) via etalon (STANDARD is for Nexus7). | |
* Use for texts, not for pictures | |
* (as this one has two scale coefficients: one for width and one for height). | |
*/ | |
@SuppressLint("UseValueOf") | |
public class DimenManager { | |
private static final Point STANDARD = new Point(1200, 1776); | |
private Point now; | |
private boolean portrait; | |
private static DimenManager instance; | |
private DimenManager() { | |
} | |
public boolean isSetup() { | |
return now != null; | |
} | |
public static DimenManager getInstance() { | |
if (instance == null) { | |
instance = new DimenManager(); | |
} | |
return instance; | |
} | |
public DimenManager setup(Context applicationContext) { | |
Display display = ((WindowManager) applicationContext | |
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); | |
DisplayMetrics metrics = new DisplayMetrics(); | |
display.getMetrics(metrics); | |
now = new Point(metrics.widthPixels, metrics.heightPixels); | |
portrait = now.x <= now.y; | |
if (!portrait) { | |
now = new Point(metrics.heightPixels, metrics.widthPixels); | |
} | |
if (now.x > STANDARD.x || now.y > STANDARD.y) { | |
now = STANDARD; | |
} | |
return this; | |
} | |
public int getWidth(int width) { | |
double theWidth = (double) now.x * width; | |
return new Double(theWidth / STANDARD.x).intValue(); | |
} | |
public int getHeight(int height) { | |
double theHeight = (double) now.y * height; | |
return new Double(theHeight / STANDARD.y).intValue(); | |
} | |
/** | |
* Text size is calculated from height. | |
*/ | |
public static int getTextSize(int size) { | |
DimenManager manager = DimenManager.getInstance(); | |
if (manager.isLoaded()) { | |
return manager.getHeight(size); | |
} | |
return 0; | |
} | |
/** | |
* Set text size of the view if it is textview and new size is positive number. | |
*/ | |
public static void setTextViewSize(View view, int pxSize) { | |
if (view != null && view instanceof TextView && pxSize > 0) { | |
((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, pxSize); | |
} | |
} | |
/** | |
* Example of use | |
*/ | |
public static void setTextSize(View option) { | |
setTextViewSize(option, getTextSize(56)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment