I have 5 variables.
The bitmap width and height.
The screen width and height.
The desired percentage
How do I make the bitmap a desired percentage of the screen using a matrix (I.e 1.0 normal, 0.5 half size, 2.0 double size).
The bitmaps are sprites that are different frame widths (I.E explosion is 200×4000 (20 frames of 200×200 while the object is 65×195 so 65×65 per object if that helps).
My aim is to pass something into ResizeBitmap like (Bitmap, 100) which would be 100% of the screen while keeping the aspect ratio.
So ResizeBitmap(Bitmap, 10) would make it 10% of the screen.
This is what I’ve got so far, it crashes show’s the wrong sizes.
public Bitmap ResizeBitmap(Bitmap bitmap, int screen_percentage)
{
float scale;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float percentage_of_screen;
float percentage_of_percentage_screen;
if(screen_width > screen_height)
{
percentage_of_screen = (screen_width / 100) * width;
percentage_of_percentage_screen = (screen_width / 100) * screen_percentage;
scale = percentage_of_screen / percentage_of_percentage_screen;
}
else
{
percentage_of_screen = (screen_height / 100) * height;
percentage_of_percentage_screen = (screen_height / 100) * screen_percentage;
scale = percentage_of_screen / percentage_of_percentage_screen;
}
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scale, scale);
// RECREATE THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
Can anyone fix my code or at least help me because I’ve wasted so much time on this function.
Thanks.
Tested:
To avoid scaling ratio problems use this in your main.xml