Created
November 6, 2018 16:39
-
-
Save vigonotion/2365d0f72340afa6a1fbf313d07bd2ed to your computer and use it in GitHub Desktop.
Easier draw a bitmap with aspect ratio in Android Java
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
/** | |
* 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