Android ImageView显示旋转的图像,尽管源未旋转 [英] Android ImageView Displaying Rotated Images Although Source Is Not Rotated
问题描述
我正在尝试在gridview中显示画廊图像,并在单击图像时打开图像的全屏视图.奇怪的是,当图像显示在图库中时,它们遵循正确的旋转方向.但是,当我只是在自定义图库中查询照片时,它们始终会逆时针旋转90度.
I am trying to display gallery images in a gridview, and on image click, open a fullscreen view for the image. The weird thing is, that when the images are displaying in the gallery, they follow the correct rotation. But When I simply query the photos in my custom gallery, they are always rotated by 90 degrees counter clockwise.
我已经看到一些使用exif接口旋转的解决方案,由于加载原始位图和新旋转的位图,因此需要旋转内存.
I have seen some solutions to rotate using exif interface which will require memory to rotate since it loads the original bitmap and the newly rotated one.
我的问题是:为什么简单地在ImageView中显示图库中的图像会导致图像旋转?
谢谢.
推荐答案
尝试这样可以避免OOM错误请参考有效地加载位图和
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and // keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); }
检查图像的旋转并正确显示...为
Check the rotation of the Image and display it properly...as
scaledBitmap=decodeSampledBitmapFromResource(.....); ExifInterface exif=null; try { exif = new ExifInterface(filePath); int orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, 0); Log.d("EXIF", "Exif: " + orientation); Matrix matrix = new Matrix(); if (orientation == 6) { matrix.postRotate(90); Log.d("EXIF", "Exif: " + orientation); } else if (orientation == 3) { matrix.postRotate(180); Log.d("EXIF", "Exif: " + orientation); } else if (orientation == 8) { matrix.postRotate(270); Log.d("EXIF", "Exif: " + orientation); } scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true); } catch (IOException e) { e.printStackTrace(); }
这篇关于Android ImageView显示旋转的图像,尽管源未旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!