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
android:id="@+id/image"
android:layout_width="150dp"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_below="@id/file_name"
android:layout_centerHorizontal="true"
android:scaleType="centerInside"/>
android:adjustViewBounds="true"/>
<Button
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 static final int SIZE_SMALL = 500;
private final WeakReference<ImageView> imageViewReference;
public String path;
public BitmapWorkerTask(ImageView imageView) {
path = null;
// 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.
@ -565,30 +569,37 @@ public class ChatEventsAdapter extends SelectableAdapter<ChatBubbleViewHolder> {
bm = BitmapFactory.decodeFile(path);
}
// Rotate the bitmap if possible/needed, using EXIF data
ImageView imageView = imageViewReference.get();
try {
Bitmap bm_tmp;
// Rotate the bitmap if possible/needed, using EXIF data
Matrix matrix = new Matrix();
ExifInterface exif = new ExifInterface(path);
int pictureOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
Matrix matrix = new Matrix();
if (pictureOrientation == 6 || pictureOrientation == 3 || pictureOrientation == 8) {
if (pictureOrientation == 6) {
matrix.postRotate(90);
} else if (pictureOrientation == 3) {
matrix.postRotate(180);
} else if (pictureOrientation == 8) {
} else {
matrix.postRotate(270);
}
bm_tmp = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
if (bm_tmp != bm) {
if (pictureOrientation == 6 || pictureOrientation == 8) {
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 = bm_tmp;
bm = null;
}
}
} catch (Exception e) {
Log.e(e);
}
if (bm != null) {
thumbnail = ThumbnailUtils.extractThumbnail(bm, SIZE_SMALL, SIZE_SMALL);
if (thumbnail == null && bm != null) {
thumbnail = scaleToFitHeight(bm, imageView.getMeasuredHeight());
bm.recycle();
}
return thumbnail;
@ -601,6 +612,7 @@ public class ChatEventsAdapter extends SelectableAdapter<ChatBubbleViewHolder> {
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap.recycle();
bitmap = null;
}
if (imageViewReference != null && bitmap != null) {