Skip to content

Instantly share code, notes, and snippets.

@vigonotion
Created November 6, 2018 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vigonotion/2365d0f72340afa6a1fbf313d07bd2ed to your computer and use it in GitHub Desktop.
Save vigonotion/2365d0f72340afa6a1fbf313d07bd2ed to your computer and use it in GitHub Desktop.
Easier draw a bitmap with aspect ratio in Android Java
/**
* Given a Rect with params left, top, right, bottom where each corner gets a value from the
* top left corner, return the Rect for a scaled bitmap at (x,y).
*
* size is the new width of the image.
*/
public Rect drawBitmapWithAspectRatio(Bitmap bitmap, float x, float y, float size, boolean centered) {
// Get the nee height based on the desired size and the bitmaps aspect ratio
float ratioHeight = bitmap.getHeight() * size / bitmap.getWidth();
if(centered)
return new Rect(x-(size/2), y-(ratioHeight/2), x+size/2, y+ratioHeight/2);
return new Rect(x, y, x+size, y+ratioHeight);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment