What i want to achieve is to set an image to an imageview without distroting the image and all solution i have tried is not working as expected. I just want the image to fit the image view then i can position the image in the imageview as i want. Here are my codes so far, help me out:
//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));
here is the code to resize the bitmap:
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 codes:
<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>