【问题标题】:How to preview images from the gallery in the correct way?如何以正确的方式预览图库中的图像?
【发布时间】:2014-01-06 11:23:38
【问题描述】:

背景

我知道有些设备不会自动旋转相机拍摄的照片,因此您必须在拍摄照片时获取相机所在的方向(链接hereherehere ) .

我需要以正确的方向(如果需要旋转)显示所有这些照片的网格,具有固定的单元格大小并使用中心裁剪方式

问题

使用中心裁剪不起作用,因为它不允许我使用 ScaleType.MATRIX 。我只是不知道如何实现它,并且由于我希望使用尽可能少的内存,我不希望创建额外的位图......

我尝试过的

这是我尝试过的,但由于我对此很陌生,所以我不知道如何进一步解决它:

/**
 * @param rotationDegree
 *            degrees to rotate the image, currently should only be any of the values: 0,90,180,270
 */
public static void setImageRotation(final ImageView imageView, final Bitmap bitmap, final int rotationDegree,
        final int dstWidth, final int dstHeight) {
    final Matrix matrix = new Matrix();
    imageView.setScaleType(ScaleType.MATRIX);
    matrix.preRotate(rotationDegree, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
    matrix.postScale((float) dstWidth / bitmap.getHeight(), (float) dstHeight / bitmap.getWidth());
    imageView.setImageMatrix(matrix);
}

我也拿到了imageView的android code for center crop,做了下一个功能:

public static void prepareCenterCropMatrix(final Matrix matrix, final int srcWidth, final int srcheight,
        final int dstWidth, final int dstHeight) {
    float scale;
    float dx = 0, dy = 0;
    if (srcWidth * dstHeight > dstWidth * srcheight) {
        scale = (float) dstHeight / (float) srcheight;
        dx = (dstWidth - srcWidth * scale) * 0.5f;
    } else {
        scale = (float) dstWidth / (float) srcWidth;
        dy = (dstHeight - srcheight * scale) * 0.5f;
    }
    matrix.setScale(scale, scale);
    matrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
}

但是无论我怎么用它,它都不起作用。

问题

谁能帮我解决这个问题?


编辑:我找到了一个替代方案,我写过关于 here 的内容。这将告诉 imageView 如何绘制旋转的位图/drawable。

但是,我仍然想知道是否有办法在解码时做到​​这一点。

【问题讨论】:

    标签: android image orientation android-camera


    【解决方案1】:

    下面试试

           imageView.setScaleType(ScaleType.FIT_XY); 
    or 
           imageView.setScaleType(ScaleType.FIT_END);
    

    【讨论】:

    • 我需要在缩放旋转的图像上使用 center-crop,而且我认为我使用的代码不起作用。
    猜你喜欢
    • 2020-04-17
    • 2012-02-04
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    相关资源
    最近更新 更多