旋转位图和大小

时间:2014-02-13 07:43:35

标签: android android-layout bitmap

我通过意图用相机拍照。收到图片后,我的应用程序有一个旋转按钮。说图像返回宽度= 900,高度= 1200。旋转图像后,我仍然需要width = 900和height = 1200而不是width = 1200和height = 900。有谁知道我会怎么做?

这是没有用的代码。

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

2 个答案:

答案 0 :(得分:0)

看看这是否有效?

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() );
}

答案 1 :(得分:0)

使用

return Bitmap.createBitmap(bmp, 0, 0, bmp.getHeight(), bmp.getWidth(), matrix, true);

而不是

return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);