Skip to content

Instantly share code, notes, and snippets.

@judeebene
Last active October 10, 2015 21:34
Show Gist options
  • Save judeebene/08cd9ad13d9a00651e2d to your computer and use it in GitHub Desktop.
Save judeebene/08cd9ad13d9a00651e2d to your computer and use it in GitHub Desktop.
Image Scaling
//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