How to preview images from the gallery in the correct way?

眉间皱痕 提交于 2019-12-13 05:24:16

问题


Background

I know that some devices do not rotate the photos taken by the camera automatically, and therefore you have to get the orientation that camera was at, when the photo was captured (links here, here and here) .

I need to show a grid of all of those photos in the correct orientation (rotate if needed), with fixed cell size and using a center crop manner

The problem

using center crop won't work , as it won't allow me to use ScaleType.MATRIX . I just don't get how to achieve it, and since I wish to use as little memory as possible, I don't wish to create extra bitmaps...

What I've tried

This is what I've tried, but since i'm quite a noob at this, I don't know how to get any further and fix it:

/**
 * @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);
}

I've also got the android code of imageView for center crop, and made the next function:

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

but no matter how I try to use it, it doesn't work.

The question

Can anyone help me on this?


EDIT: I've found an alternative, which i've written about here . this will tell the imageView how to draw a rotated bitmap/drawable.

however, I still wish to know if there is a way to do it while decoding.


回答1:


try below

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


来源:https://stackoverflow.com/questions/20948973/how-to-preview-images-from-the-gallery-in-the-correct-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!