如何从正确的方式画廊preVIEW图像? [英] How to preview images from the gallery in the correct way?

查看:170
本文介绍了如何从正确的方式画廊preVIEW图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我知道有些设备不旋转相机自动拍摄的照片,因此,你必须得到取向相机在,当照片被抓获(链接<一个href=\"http://stackoverflow.com/questions/12726860/android-how-to-detect-the-image-orientation-portrait-or-landscape-picked-fro\">here, <一href=\"http://stackoverflow.com/questions/8450539/images-taken-with-action-image-capture-always-returns-1-for-exifinterface-tag-or/8864367#8864367\">here和<一个href=\"http://stackoverflow.com/questions/4517634/picture-orientation-from-gallery-camera-intent\">here)

我需要显示所有这些照片的一格中正确的方向(如果需要旋转),与固定的细胞的大小和使用中心作物方式

问题

使用中心的作物将无法正常工作,因为它不会允许我使用ScaleType.MATRIX。我只是不明白怎么实现它,因为我希望用尽可能少的内存越好,我不希望创建额外的位图...

我已经试过

这是我尝试过,但因为我在这个相当小白,我不知道如何得到任何进一步和解决它:

  / **
 * @参数rotationDegree
 *度旋转图像,目前只应的任何值:0,90,180,270
 * /
公共静态无效setImageRotation(最终ImageView的ImageView的,最终的位图的位图,最终诠释rotationDegree,
        最终诠释dstWidth,最终诠释dstHeight){
    最后矩阵的矩阵=新的Matrix();
    imageView.setScaleType(ScaleType.MATRIX);
    。矩阵preRotate(rotationDegree,bitmap.getWidth()/ 2,bitmap.getHeight()/ 2);
    matrix.postScale((浮点)dstWidth / bitmap.getHeight(),(浮点)dstHeight / bitmap.getWidth());
    imageView.setImageMatrix(矩阵);
}

我也得到了ImageView的为中心作物的Andr​​oid code,并提出下一个功能:

 公共静态无效prepareCenterCropMatrix(最终矩阵的矩阵,最终诠释srcWidth,最终诠释srcheight,
        最终诠释dstWidth,最终诠释dstHeight){
    浮规模;
    浮DX = 0,DY = 0;
    如果(srcWidth * dstHeight&GT; dstWidth * srcheight){
        规模=(浮点)dstHeight /(浮点)srcheight;
        DX =(dstWidth - srcWidth *等级)* 0.5F;
    }其他{
        规模=(浮点)dstWidth /(浮点)srcWidth;
        DY =(dstHeight - srcheight *等级)* 0.5F;
    }
    matrix.setScale(秤,秤);
    matrix.postTranslate((int)的(DX + 0.5F),(INT)(DY + 0.5F));
}

但无论我怎么尝试使用它,这是行不通的。

问题

谁能帮我这个?


编辑:我找到了另一种选择,我已经写了关于这里。这将告诉ImageView的如何绘制一个旋转的位图/绘制。

不过,我还是想知道是否有办法做到这一点,而解码。


解决方案

尝试以下


  imageView.setScaleType(ScaleType.FIT_XY);
要么
       imageView.setScaleType(ScaleType.FIT_END);


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.

解决方案

try below

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

这篇关于如何从正确的方式画廊preVIEW图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆