我正在尝试使用 android.graphics.Matrix 旋转然后缩放图像。代码如下:
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 是为了应用纹理而得到一个正方形的 2 次方图像。 bmp2 的高度和宽度为 -1,即无效图像。我错过了什么吗?
谢谢,
拉贾特
最佳答案
下面是我用于解决我的问题的解决方案,以备将来有人需要。
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);
关于android - 使用矩阵在android中旋转和缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5415960/