Last active
October 10, 2015 21:34
-
-
Save judeebene/08cd9ad13d9a00651e2d to your computer and use it in GitHub Desktop.
Image Scaling
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
//getDisplayHeight(context) => (display height in pixels) | |
public static int getDisplayHeight(Context context) { | |
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); | |
return displayMetrics.heightPixels; | |
} | |
// getDisplayWidth(context) => (display width in pixels) | |
public static int getDisplayWidth(Context context) { | |
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); | |
return displayMetrics.widthPixels; | |
} | |
// Scale and maintain aspect ratio given a desired width | |
// scaleToFitWidth(bitmap, 100); | |
public static Bitmap scaleToFitWidth(Bitmap b, int width) | |
{ | |
float factor = width / (float) b.getWidth(); | |
return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true); | |
} | |
// Scale and maintain aspect ratio given a desired height | |
// BitmapScaler.scaleToFitHeight(bitmap, 100); | |
public static Bitmap scaleToFitHeight(Bitmap b, int height) | |
{ | |
float factor = height / (float) b.getHeight(); | |
return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment