调整大小后无法正确将图像设置为imageview - ANDROID

时间:2016-01-08 19:31:23

标签: android image android-imageview

我想要实现的是将图像设置为imageview而不会分配图像,并且我尝试的所有解决方案都没有按预期工作。我只是希望图像适合图像视图,然后我可以根据需要将图像定位在图像视图中。以下是我的代码,帮助我:

//converting image uri send from another class to bitmap
 try {
        mBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageUri);
    } catch (IOException e) {
        e.printStackTrace();
    }

 bitmapPath = getImagePath(mImageUri);

 try {
        ExifInterface exif = new ExifInterface(bitmapPath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        Log.d("EXIF", "Exif: " + orientation);
        Matrix matrix = new Matrix();
        if (orientation == 6) {
            matrix.postRotate(90);
        }
        else if (orientation == 3) {
            matrix.postRotate(180);
        }
        else if (orientation == 8) {
            matrix.postRotate(270);
        }

        xBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);

        finalBitmap = getResizedBitmap(xBitmap,  mBitmap.getWidth(), mBitmap.getHeight(), true);

    } catch (IOException e) {
        e.printStackTrace();
    }

    mNormalImage.setImageBitmap(finalBitmap);

    mBlurImage.setImageBitmap(createBitmap_ScriptIntrinsicBlur(finalBitmap, 25.0f));

这是调整位图大小的代码:

 public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth, boolean willDelete) {

    int width = bm.getWidth();

    int height = bm.getHeight();

    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;

    // CREATE A MATRIX FOR THE MANIPULATION

    Matrix matrix = new Matrix();

    // RESIZE THE BIT MAP

    matrix.postScale(scaleWidth, scaleHeight);

    // RECREATE THE NEW BITMAP

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);

    return resizedBitmap;
}

xml代码:

 <FrameLayout
    android:id="@+id/blurPicture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:layout_width="446dp"
        android:layout_height="400dp"
        android:id="@+id/blurred_image"
        android:layout_gravity="center"
        android:contentDescription="@string/blurry_background"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        >
    </ImageView>

    <ImageView
        android:id="@+id/normal_image"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:contentDescription="@string/original_image"
        android:layout_gravity="center"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        />
</FrameLayout>

1 个答案:

答案 0 :(得分:0)

获取调整图像大小的文件路径后,请尝试使用此代码。

// your filepath here
if (filePath != null) {
            int orientation = 0;
            private Bitmap imageBitmap;
            try {
                ExifInterface exif = new ExifInterface(filePath);
                orientation = exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                final BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(filePath, options);
                options.inSampleSize = calculateInSampleSize(
                        options, reqWidth, reqHeight); //rewWidth and reqHeight are the width and height for the required image you need.
                options.inJustDecodeBounds = false;
                imageBitmap = BitmapFactory.decodeFile(filePath,
                        options);
                if (orientation == 6) {
                    Matrix matrix = new Matrix();
                    matrix.postRotate(90);
                    imageBitmap = Bitmap.createBitmap(imageBitmap,
                            0, 0, imageBitmap.getWidth(),
                            imageBitmap.getHeight(), matrix, true);
                } else if (orientation == 8) {
                    Matrix matrix = new Matrix();
                    matrix.postRotate(270);
                    imageBitmap = Bitmap.createBitmap(imageBitmap,
                            0, 0, imageBitmap.getWidth(),
                            imageBitmap.getHeight(), matrix, true);
                } else if (orientation == 3) {
                    Matrix matrix = new Matrix();
                    matrix.postRotate(180);
                    imageBitmap = Bitmap.createBitmap(imageBitmap,
                            0, 0, imageBitmap.getWidth(),
                            imageBitmap.getHeight(), matrix, true);
                }
            } catch (OutOfMemoryError e) {
                imageBitmap = null;
                e.printStackTrace();
            } catch (Exception e) {
                imageBitmap = null;
                e.printStackTrace();
            }
        }
        if (imageBitmap != null) {
            // set this imageBitmap to your ImageView
        }

这是采样函数

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;
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }
       return inSampleSize;
    }