Improved image display in chat bubble to keep ratio

This commit is contained in:
Sylvain Berfini 2018-11-09 09:27:35 +01:00
parent 4b074e982b
commit 70f221c807
2 changed files with 32 additions and 20 deletions

View file

@ -101,11 +101,11 @@
<ImageView <ImageView
android:id="@+id/image" android:id="@+id/image"
android:layout_width="150dp" android:layout_width="wrap_content"
android:layout_height="150dp" android:layout_height="150dp"
android:layout_below="@id/file_name" android:layout_below="@id/file_name"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:scaleType="centerInside"/> android:adjustViewBounds="true"/>
<Button <Button
android:id="@+id/open_file" android:id="@+id/open_file"

View file

@ -536,14 +536,18 @@ public class ChatEventsAdapter extends SelectableAdapter<ChatBubbleViewHolder> {
*/ */
private class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> { private class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
private static final int SIZE_SMALL = 500;
private final WeakReference<ImageView> imageViewReference; private final WeakReference<ImageView> imageViewReference;
public String path; public String path;
public BitmapWorkerTask(ImageView imageView) { public BitmapWorkerTask(ImageView imageView) {
path = null; path = null;
// Use a WeakReference to ensure the ImageView can be garbage collected // Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView); imageViewReference = new WeakReference<>(imageView);
}
private Bitmap scaleToFitHeight(Bitmap b, int height) {
float factor = height / (float) b.getHeight();
return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, true);
} }
// Decode image in background. // Decode image in background.
@ -565,30 +569,37 @@ public class ChatEventsAdapter extends SelectableAdapter<ChatBubbleViewHolder> {
bm = BitmapFactory.decodeFile(path); bm = BitmapFactory.decodeFile(path);
} }
// Rotate the bitmap if possible/needed, using EXIF data ImageView imageView = imageViewReference.get();
try { try {
Bitmap bm_tmp; // Rotate the bitmap if possible/needed, using EXIF data
Matrix matrix = new Matrix();
ExifInterface exif = new ExifInterface(path); ExifInterface exif = new ExifInterface(path);
int pictureOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); int pictureOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
Matrix matrix = new Matrix(); if (pictureOrientation == 6 || pictureOrientation == 3 || pictureOrientation == 8) {
if (pictureOrientation == 6) { if (pictureOrientation == 6) {
matrix.postRotate(90); matrix.postRotate(90);
} else if (pictureOrientation == 3) { } else if (pictureOrientation == 3) {
matrix.postRotate(180); matrix.postRotate(180);
} else if (pictureOrientation == 8) { } else {
matrix.postRotate(270); matrix.postRotate(270);
} }
bm_tmp = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); if (pictureOrientation == 6 || pictureOrientation == 8) {
if (bm_tmp != bm) { matrix.postScale(1, imageView.getMeasuredHeight() / (float) bm.getHeight());
} else {
matrix.postScale(imageView.getMeasuredHeight() / (float) bm.getHeight(), 1);
}
thumbnail = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
if (thumbnail != bm) {
bm.recycle(); bm.recycle();
bm = bm_tmp; bm = null;
}
} }
} catch (Exception e) { } catch (Exception e) {
Log.e(e); Log.e(e);
} }
if (bm != null) { if (thumbnail == null && bm != null) {
thumbnail = ThumbnailUtils.extractThumbnail(bm, SIZE_SMALL, SIZE_SMALL); thumbnail = scaleToFitHeight(bm, imageView.getMeasuredHeight());
bm.recycle(); bm.recycle();
} }
return thumbnail; return thumbnail;
@ -601,6 +612,7 @@ public class ChatEventsAdapter extends SelectableAdapter<ChatBubbleViewHolder> {
@Override @Override
protected void onPostExecute(Bitmap bitmap) { protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) { if (isCancelled()) {
bitmap.recycle();
bitmap = null; bitmap = null;
} }
if (imageViewReference != null && bitmap != null) { if (imageViewReference != null && bitmap != null) {