Reputation: 11926
I'm trying to rotate and then scale an image using android.graphics.Matrix. The code is as follows:
int width = bmp1.getWidth();
int height = bmp1.getHeight();
int smallerVal = (height > width) ? width : height;
int n = 0;
while (smallerVal > 1)
{
smallerVal = smallerVal >> 1;
n++;
}
int pow2 = (int) Math.pow(2, n);
float scaleWidth = ((float) AppBase.width) / (float)pow2;
float scaleHeight = ((float) AppBase.height) / (float)pow2;
Matrix matrix = new Matrix();
matrix.postRotate(90);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bmp2 = Bitmap.createBitmap(bmp1, 0, 0, pow2, pow2, matrix, true);
pow2 is to get a square power-of-2 image for the sake of applying a texture. bmp2 turns out to have -1 as height and width, ie an invalid image. Am I missing something?
Thanks,
Rajath
Upvotes: 1
Views: 4068
Reputation: 11926
The solution I'm using for my problem is below, in case anyone needs it in future.
int pow2 = getSquareVal(bmp1);
int width = bmp1.getWidth();
int height = bmp1.getHeight();
float scaleWidth = ((float) pow2) / (float)width;
float scaleHeight = ((float) pow2) / (float)height;
Matrix matrix = new Matrix();
matrix.reset();
matrix.postScale(scaleWidth, scaleHeight);
if (width > height)
matrix.postRotate(90);
bmp2 = Bitmap.createBitmap(bmp1, 0, 0, width, height, matrix, true);
Upvotes: 1