Created
October 17, 2011 22:11
-
-
Save evandrix/1293997 to your computer and use it in GitHub Desktop.
Android: Dynamic screen sizes
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
/** | |
* Sample class from Pulse | |
* | |
* Class to store and provide useful dimensions | |
*/ | |
public class DimensionCalculator { | |
private static DimensionCalculator mInstance = null; | |
private int mScreenWidth; | |
private int mTileWidth; | |
/** | |
* This class is a singleton | |
*/ | |
public static DimensionCalculator getInstance(Context context) { | |
if (mInstance == null) { | |
mInstance = new DimensionCalculator(context); | |
} | |
return mInstance; | |
} | |
/** | |
* Initialize the class with the proper tile size | |
*/ | |
protected DimensionCalculator(Context context) { | |
DisplayMetrics dm = context.getResources().getDisplayMetrics(); | |
mScreenWidth = Math.min(context.getResources().getDisplayMetrics().widthPixels, | |
context.getResources().getDisplayMetrics().heightPixels); | |
int numTiles = 3; | |
int tileGap = 2; | |
mTileWidth = (int) ((mScreenWidth - 4 * tileGap) / numTiles); | |
} | |
/** | |
* Return the appropriate tile size for this device | |
*/ | |
public int getTileWidth() { | |
return mTileWidth; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment