From 90419413b9700038381d7abb61936909f74aaaf2 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 2 Nov 2021 21:35:28 +0800 Subject: Try optimizing image loading in PostRecyclerViewAdapter. --- .../adapters/PostRecyclerViewAdapter.java | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index 448013f1..464a4ab1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -39,6 +39,7 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.RequestManager; import com.bumptech.glide.load.DataSource; +import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; @@ -1444,29 +1445,31 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter 10_000_000) { + if (preview.getPreviewWidth() * preview.getPreviewHeight() > 5_000_000) { for (int i = previews.size() - 1; i >= 1; i--) { preview = previews.get(i); if (mImageViewWidth >= preview.getPreviewWidth()) { - if (preview.getPreviewWidth() * preview.getPreviewHeight() <= 10_000_000) { + if (preview.getPreviewWidth() * preview.getPreviewHeight() <= 5_000_000) { return preview; } } else { int height = mImageViewWidth / preview.getPreviewWidth() * preview.getPreviewHeight(); - if (mImageViewWidth * height <= 10_000_000) { + if (mImageViewWidth * height <= 5_000_000) { + preview.setPreviewWidth(mImageViewWidth); + preview.setPreviewHeight(height); return preview; } } } } - if (preview.getPreviewWidth() * preview.getPreviewHeight() > 10_000_000) { + if (preview.getPreviewWidth() * preview.getPreviewHeight() > 5_000_000) { int divisor = 2; do { preview.setPreviewWidth(preview.getPreviewWidth() / divisor); preview.setPreviewHeight(preview.getPreviewHeight() / divisor); divisor *= 2; - } while (preview.getPreviewWidth() * preview.getPreviewHeight() > 10_000_000); + } while (preview.getPreviewWidth() * preview.getPreviewHeight() > 5_000_000); } return preview; } @@ -1492,11 +1495,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter preview.getPreviewWidth()) { - imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostWithPreviewTypeViewHolder) holder).imageView); - } else { - imageRequestBuilder.into(((PostWithPreviewTypeViewHolder) holder).imageView); - } + imageRequestBuilder.diskCacheStrategy(DiskCacheStrategy.NONE).override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostWithPreviewTypeViewHolder) holder).imageView); } } } else if (holder instanceof PostCompactBaseViewHolder) { @@ -1740,6 +1739,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter