Android 旋转位图并调整大小
我通过相机拍摄了一张照片。收到图像后,我的应用程序会有一个旋转按钮。假设图像返回宽度=900,高度=1200。旋转图像后,我仍然希望宽度为900,高度为1200,而不是宽度为1200,高度为900。有人知道我会怎么做吗 这是一段不起作用的代码Android 旋转位图并调整大小,android,android-layout,bitmap,Android,Android Layout,Bitmap,我通过相机拍摄了一张照片。收到图像后,我的应用程序会有一个旋转按钮。假设图像返回宽度=900,高度=1200。旋转图像后,我仍然希望宽度为900,高度为1200,而不是宽度为1200,高度为900。有人知道我会怎么做吗 这是一段不起作用的代码 Bitmap bmp = readBitmap(context.getContentResolver(), imageUri, sampleScale, options); float widthScale = 0.9f * deviceDimension
Bitmap bmp = readBitmap(context.getContentResolver(), imageUri, sampleScale, options);
float widthScale = 0.9f * deviceDimension[1] / bmp.getWidth();
float heightScale = 0.8f * deviceDimension[0] / bmp.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(widthScale, heightScale);
matrix.preRotate(angle);//90x
return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
因此,基本上,我希望图像仍然适合屏幕上的当前尺寸,无论我如何定位图像。现在的面积是,比如说,900乘1200,看看这是否有效
public static Bitmap ScaleBitmap(Bitmap bm, float scalingFactor) {
int scaleHeight = (int) (bm.getHeight() * scalingFactor);
int scaleWidth = (int) (bm.getWidth() * scalingFactor);
return Bitmap.createScaledBitmap(bm, scaleWidth, scaleHeight, true);
}
private float getBitmapScalingFactor(Bitmap bm) {
// Get display width from device
int displayWidth = getWindowManager().getDefaultDisplay().getWidth();
// Get margin to use it for calculating to max width of the ImageView
LinearLayout.LayoutParams layoutParams =
(LinearLayout.LayoutParams)this.imageView.getLayoutParams();
int leftMargin = layoutParams.leftMargin;
int rightMargin = layoutParams.rightMargin;
// Calculate the max width of the imageView
int imageViewWidth = displayWidth - (leftMargin + rightMargin);
// Calculate scaling factor and return it
return ( (float) imageViewWidth / (float) bm.getWidth() );
}
使用
而不是
return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
有什么不同于那个?这有什么特别的??它是如何更有用的?如果你想使图像适合屏幕,请获取缩放因子,然后将该因子应用于宽度和高度,这样图像将缩小以适合屏幕。
return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);