diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-11-21 14:55:58 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-11-21 14:55:58 +0000 |
commit | 50a2db2a398e257e236505cb222c289cb0d5331f (patch) | |
tree | 659c1ae4518eb62718238faf06a67ba7608fe60e /app/src | |
parent | 725921dac16541a1eb4724130bf87ee509c0591b (diff) | |
download | infinity-for-reddit-50a2db2a398e257e236505cb222c289cb0d5331f.tar infinity-for-reddit-50a2db2a398e257e236505cb222c289cb0d5331f.tar.gz infinity-for-reddit-50a2db2a398e257e236505cb222c289cb0d5331f.tar.bz2 infinity-for-reddit-50a2db2a398e257e236505cb222c289cb0d5331f.tar.lz infinity-for-reddit-50a2db2a398e257e236505cb222c289cb0d5331f.tar.xz infinity-for-reddit-50a2db2a398e257e236505cb222c289cb0d5331f.tar.zst infinity-for-reddit-50a2db2a398e257e236505cb222c289cb0d5331f.zip |
Implement SaveMemoryCenterInsideDownsampleStrategy to smartly downsample images in PostRecyclerViewAdapter and PostDetailRecyclerViewAdapter.
Diffstat (limited to 'app/src')
6 files changed, 62 insertions, 134 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SaveMemoryCenterInisdeDownsampleStrategy.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SaveMemoryCenterInisdeDownsampleStrategy.java new file mode 100644 index 00000000..8516c53e --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SaveMemoryCenterInisdeDownsampleStrategy.java @@ -0,0 +1,29 @@ +package ml.docilealligator.infinityforreddit; + +import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy; + +public class SaveMemoryCenterInisdeDownsampleStrategy extends DownsampleStrategy { + + @Override + public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth, int requestedHeight) { + int originalSourceWidth = sourceWidth; + int originalSourceHeight = sourceHeight; + if (sourceWidth * sourceHeight > 5_000_000) { + int divisor = 2; + do { + sourceWidth /= divisor; + sourceHeight /= divisor; + } while (sourceWidth * sourceHeight > 5_000_000); + } + + float widthPercentage = (float) requestedWidth / (float) sourceWidth; + float heightPercentage = (float) requestedHeight / (float) sourceHeight; + + return Math.min((float) sourceWidth / (float) originalSourceWidth, (float) sourceHeight / (float) originalSourceHeight) * Math.min(1.f, Math.min(widthPercentage, heightPercentage)); + } + + @Override + public SampleSizeRounding getSampleSizeRounding(int sourceWidth, int sourceHeight, int requestedWidth, int requestedHeight) { + return SampleSizeRounding.MEMORY; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index 0b2c7f94..0eb69416 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -87,6 +87,7 @@ import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import me.saket.bettermovementmethod.BetterLinkMovementMethod; import ml.docilealligator.infinityforreddit.FetchGfycatOrRedgifsVideoLinks; +import ml.docilealligator.infinityforreddit.SaveMemoryCenterInisdeDownsampleStrategy; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SaveThing; @@ -140,7 +141,6 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler private RequestManager mGlide; private Markwon mPostDetailMarkwon; private final MarkwonAdapter mMarkwonAdapter; - private int mImageViewWidth; private String mAccessToken; private String mAccountName; private Post mPost; @@ -209,7 +209,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler Executor executor, CustomThemeWrapper customThemeWrapper, Retrofit retrofit, Retrofit oauthRetrofit, Retrofit gfycatRetrofit, Retrofit redgifsRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, - RequestManager glide, int imageViewWidth, + RequestManager glide, boolean separatePostAndComments, String accessToken, String accountName, Post post, Locale locale, SharedPreferences sharedPreferences, @@ -342,7 +342,6 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler .tableLayout(R.layout.adapter_table_block, R.id.table_layout) .textLayoutIsRoot(R.layout.view_table_entry_cell))) .build(); - mImageViewWidth = imageViewWidth; mSeparatePostAndComments = separatePostAndComments; mLegacyAutoplayVideoControllerUI = sharedPreferences.getBoolean(SharedPreferencesUtils.LEGACY_AUTOPLAY_VIDEO_CONTROLLER_UI, false); mAccessToken = accessToken; @@ -676,11 +675,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler Post.Preview preview = getSuitablePreview(mPost.getPreviews()); if (preview != null) { ((PostDetailVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) preview.getPreviewWidth() / preview.getPreviewHeight()); - if (mImageViewWidth > preview.getPreviewWidth()) { - mGlide.load(preview.getPreviewUrl()).override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView); - } else { - mGlide.load(preview.getPreviewUrl()).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView); - } + mGlide.load(preview.getPreviewUrl()).centerInside().downsample(new SaveMemoryCenterInisdeDownsampleStrategy()).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView); } else { ((PostDetailVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio(1); } @@ -736,8 +731,6 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler int height = (int) (400 * mScale); ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP); ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.getLayoutParams().height = height; - preview.setPreviewWidth(mImageViewWidth); - preview.setPreviewHeight(height); } else { ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.setRatio((float) preview.getPreviewHeight() / (float) preview.getPreviewWidth()); } @@ -854,31 +847,15 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler previewIndex = 0; } preview = previews.get(previewIndex); - if (preview.getPreviewWidth() * preview.getPreviewHeight() > 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) { - return preview; - } - } else { - int height = mImageViewWidth / preview.getPreviewWidth() * preview.getPreviewHeight(); - if (mImageViewWidth * height <= 10_000_000) { - return preview; - } + if (preview.getPreviewWidth() * preview.getPreviewHeight() <= 5_000_000) { + return preview; } } } - if (preview.getPreviewWidth() * preview.getPreviewHeight() > 10_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); - } - return preview; } @@ -890,7 +867,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler boolean blurImage = (mPost.isNSFW() && mNeedBlurNsfw && !(mDoNotBlurNsfwInNsfwSubreddits && mFragment != null && mFragment.getIsNsfwSubreddit()) && !(mPost.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || (mPost.isSpoiler() && mNeedBlurSpoiler); String url = mPost.getPostType() == Post.IMAGE_TYPE || blurImage ? preview.getPreviewUrl() : mPost.getUrl(); RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(url) - .listener(new RequestListener<Drawable>() { + .listener(new RequestListener<>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { ((PostDetailImageAndGifAutoplayViewHolder) holder).mLoadImageProgressBar.setVisibility(View.GONE); @@ -913,15 +890,11 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler if (blurImage) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView); } else { - if (mImageViewWidth > preview.getPreviewWidth()) { - imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView); - } else { - imageRequestBuilder.into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView); - } + imageRequestBuilder.centerInside().downsample(new SaveMemoryCenterInisdeDownsampleStrategy()).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView); } } else if (holder instanceof PostDetailVideoAndGifPreviewHolder) { RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(preview.getPreviewUrl()) - .listener(new RequestListener<Drawable>() { + .listener(new RequestListener<>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { ((PostDetailVideoAndGifPreviewHolder) holder).mLoadImageProgressBar.setVisibility(View.GONE); @@ -945,15 +918,11 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) .into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); } else { - if (mImageViewWidth > preview.getPreviewWidth()) { - imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); - } else { - imageRequestBuilder.into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); - } + imageRequestBuilder.centerInside().downsample(new SaveMemoryCenterInisdeDownsampleStrategy()).into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); } } else if (holder instanceof PostDetailLinkViewHolder) { RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(preview.getPreviewUrl()) - .listener(new RequestListener<Drawable>() { + .listener(new RequestListener<>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { ((PostDetailLinkViewHolder) holder).mLoadImageProgressBar.setVisibility(View.GONE); @@ -977,15 +946,11 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) .into(((PostDetailLinkViewHolder) holder).mImageView); } else { - if (mImageViewWidth > preview.getPreviewWidth()) { - imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostDetailLinkViewHolder) holder).mImageView); - } else { - imageRequestBuilder.into(((PostDetailLinkViewHolder) holder).mImageView); - } + imageRequestBuilder.centerInside().downsample(new SaveMemoryCenterInisdeDownsampleStrategy()).into(((PostDetailLinkViewHolder) holder).mImageView); } } else if (holder instanceof PostDetailGalleryViewHolder) { RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(preview.getPreviewUrl()) - .listener(new RequestListener<Drawable>() { + .listener(new RequestListener<>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { ((PostDetailGalleryViewHolder) holder).mLoadImageProgressBar.setVisibility(View.GONE); @@ -1008,11 +973,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler if ((mPost.isNSFW() && mNeedBlurNsfw && !(mDoNotBlurNsfwInNsfwSubreddits && mFragment != null && mFragment.getIsNsfwSubreddit()) && !(mPost.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || (mPost.isSpoiler() && mNeedBlurSpoiler)) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).into(((PostDetailGalleryViewHolder) holder).mImageView); } else { - if (mImageViewWidth > preview.getPreviewWidth()) { - imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostDetailGalleryViewHolder) holder).mImageView); - } else { - imageRequestBuilder.into(((PostDetailGalleryViewHolder) holder).mImageView); - } + imageRequestBuilder.centerInside().downsample(new SaveMemoryCenterInisdeDownsampleStrategy()).into(((PostDetailGalleryViewHolder) holder).mImageView); } } } 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 57ce12b0..a8cf40d2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -69,6 +69,7 @@ import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.FetchGfycatOrRedgifsVideoLinks; import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; +import ml.docilealligator.infinityforreddit.SaveMemoryCenterInisdeDownsampleStrategy; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.SaveThing; import ml.docilealligator.infinityforreddit.VoteThing; @@ -126,7 +127,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie private Retrofit mOauthRetrofit; private Retrofit mGfycatRetrofit; private Retrofit mRedgifsRetrofit; - private int mImageViewWidth; private String mAccessToken; private RequestManager mGlide; private Locale mLocale; @@ -206,7 +206,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie public PostRecyclerViewAdapter(AppCompatActivity activity, PostFragment fragment, Executor executor, Retrofit oauthRetrofit, Retrofit gfycatRetrofit, Retrofit redgifsRetrofit, - CustomThemeWrapper customThemeWrapper, Locale locale, int imageViewWidth, + CustomThemeWrapper customThemeWrapper, Locale locale, String accessToken, String accountName, int postType, int postLayout, boolean displaySubredditName, SharedPreferences sharedPreferences, SharedPreferences nsfwAndSpoilerSharedPreferences, SharedPreferences postHistorySharedPreferences, @@ -220,7 +220,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie mOauthRetrofit = oauthRetrofit; mGfycatRetrofit = gfycatRetrofit; mRedgifsRetrofit = redgifsRetrofit; - mImageViewWidth = imageViewWidth; mAccessToken = accessToken; mPostType = postType; mDisplaySubredditName = displaySubredditName; @@ -676,11 +675,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie Post.Preview preview = getSuitablePreview(post.getPreviews()); if (preview != null) { ((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) preview.getPreviewWidth() / preview.getPreviewHeight()); - if (mImageViewWidth > preview.getPreviewWidth()) { - mGlide.load(preview.getPreviewUrl()).override(Target.SIZE_ORIGINAL).into(((PostVideoAutoplayViewHolder) holder).previewImageView); - } else { - mGlide.load(preview.getPreviewUrl()).into(((PostVideoAutoplayViewHolder) holder).previewImageView); - } + mGlide.load(preview.getPreviewUrl()).centerInside().downsample(new SaveMemoryCenterInisdeDownsampleStrategy()).into(((PostVideoAutoplayViewHolder) holder).previewImageView); } else { ((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio(1); } @@ -774,8 +769,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie int height = (int) (400 * mScale); ((PostWithPreviewTypeViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); ((PostWithPreviewTypeViewHolder) holder).imageView.getLayoutParams().height = height; - preview.setPreviewWidth(mImageViewWidth); - preview.setPreviewHeight(height); } else { ((PostWithPreviewTypeViewHolder) holder).imageView .setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); @@ -809,11 +802,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie Post.Preview preview = getSuitablePreview(post.getPreviews()); if (preview != null) { ((PostCard2VideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) preview.getPreviewWidth() / preview.getPreviewHeight()); - if (mImageViewWidth > preview.getPreviewWidth()) { - mGlide.load(preview.getPreviewUrl()).override(Target.SIZE_ORIGINAL).into(((PostCard2VideoAutoplayViewHolder) holder).previewImageView); - } else { - mGlide.load(preview.getPreviewUrl()).into(((PostCard2VideoAutoplayViewHolder) holder).previewImageView); - } + mGlide.load(preview.getPreviewUrl()).centerInside().downsample(new SaveMemoryCenterInisdeDownsampleStrategy()).into(((PostCard2VideoAutoplayViewHolder) holder).previewImageView); } else { ((PostCard2VideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio(1); } @@ -909,8 +898,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie int height = (int) (400 * mScale); ((PostCard2WithPreviewViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); ((PostCard2WithPreviewViewHolder) holder).imageView.getLayoutParams().height = height; - preview.setPreviewWidth(mImageViewWidth); - preview.setPreviewHeight(height); } else { ((PostCard2WithPreviewViewHolder) holder).imageView .setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); @@ -1291,8 +1278,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie int height = (int) (400 * mScale); ((PostGalleryViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); ((PostGalleryViewHolder) holder).imageView.getLayoutParams().height = height; - preview.setPreviewWidth(mImageViewWidth); - preview.setPreviewHeight(height); } else { ((PostGalleryViewHolder) holder).imageView .setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); @@ -1327,8 +1312,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie int height = (int) (400 * mScale); ((PostGalleryViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); ((PostGalleryViewHolder) holder).imageView.getLayoutParams().height = height; - preview.setPreviewWidth(mImageViewWidth); - preview.setPreviewHeight(height); } else { ((PostGalleryViewHolder) holder).imageView .setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); @@ -1353,8 +1336,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie int height = (int) (400 * mScale); ((PostGalleryViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); ((PostGalleryViewHolder) holder).imageView.getLayoutParams().height = height; - preview.setPreviewWidth(mImageViewWidth); - preview.setPreviewHeight(height); } else { ((PostGalleryViewHolder) holder).imageView .setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); @@ -1379,8 +1360,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie int height = (int) (400 * mScale); ((PostGalleryViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); ((PostGalleryViewHolder) holder).imageView.getLayoutParams().height = height; - preview.setPreviewWidth(mImageViewWidth); - preview.setPreviewHeight(height); } else { ((PostGalleryViewHolder) holder).imageView .setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); @@ -1415,8 +1394,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie int height = (int) (400 * mScale); ((PostGalleryViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); ((PostGalleryViewHolder) holder).imageView.getLayoutParams().height = height; - preview.setPreviewWidth(mImageViewWidth); - preview.setPreviewHeight(height); } else { ((PostGalleryViewHolder) holder).imageView .setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); @@ -1447,29 +1424,11 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie 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() <= 5_000_000) { - return preview; - } - } else { - int height = mImageViewWidth / preview.getPreviewWidth() * preview.getPreviewHeight(); - if (mImageViewWidth * height <= 5_000_000) { - preview.setPreviewWidth(mImageViewWidth); - preview.setPreviewHeight(height); - return preview; - } + if (preview.getPreviewWidth() * preview.getPreviewHeight() <= 5_000_000) { + return preview; } } } - - 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() > 5_000_000); - } return preview; } @@ -1494,7 +1453,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) .into(((PostWithPreviewTypeViewHolder) holder).imageView); } else { - imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).centerInside().into(((PostWithPreviewTypeViewHolder) holder).imageView); + imageRequestBuilder.centerInside().downsample(new SaveMemoryCenterInisdeDownsampleStrategy()).into(((PostWithPreviewTypeViewHolder) holder).imageView); } } } else if (holder instanceof PostCompactBaseViewHolder) { @@ -1534,11 +1493,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) .into(((PostGalleryViewHolder) holder).imageView); } else { - if (mImageViewWidth > preview.getPreviewWidth()) { - imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostGalleryViewHolder) holder).imageView); - } else { - imageRequestBuilder.into(((PostGalleryViewHolder) holder).imageView); - } + imageRequestBuilder.centerInside().downsample(new SaveMemoryCenterInisdeDownsampleStrategy()).into(((PostGalleryViewHolder) holder).imageView); } } } else if (holder instanceof PostCard2WithPreviewViewHolder) { @@ -1558,11 +1513,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) .into(((PostCard2WithPreviewViewHolder) holder).imageView); } else { - if (mImageViewWidth > preview.getPreviewWidth()) { - imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostCard2WithPreviewViewHolder) holder).imageView); - } else { - imageRequestBuilder.into(((PostCard2WithPreviewViewHolder) holder).imageView); - } + imageRequestBuilder.centerInside().downsample(new SaveMemoryCenterInisdeDownsampleStrategy()).into(((PostCard2WithPreviewViewHolder) holder).imageView); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index 4a9703f7..257f604e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -17,7 +17,6 @@ import android.os.Build; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; -import android.util.DisplayMetrics; import android.view.HapticFeedbackConstants; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -338,10 +337,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } - DisplayMetrics displayMetrics = new DisplayMetrics(); - activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); - int windowWidth = displayMetrics.widthPixels; - mGlide = Glide.with(activity); lazyModeRunnable = new LazyModeRunnable() { @@ -452,7 +447,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, mCustomThemeWrapper, locale, - windowWidth, accessToken, accountName, postType, postLayout, true, + accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @Override @@ -529,7 +524,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, mCustomThemeWrapper, locale, - windowWidth, accessToken, accountName, postType, postLayout, displaySubredditName, + accessToken, accountName, postType, postLayout, displaySubredditName, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @Override @@ -600,7 +595,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, mCustomThemeWrapper, locale, - windowWidth, accessToken, accountName, postType, postLayout, true, + accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @Override @@ -665,7 +660,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, mCustomThemeWrapper, locale, - windowWidth, accessToken, accountName, postType, postLayout, true, + accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @Override @@ -726,7 +721,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, mCustomThemeWrapper, locale, - windowWidth, accessToken, accountName, postType, postLayout, true, + accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @Override @@ -786,7 +781,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, mCustomThemeWrapper, locale, - windowWidth, accessToken, accountName, postType, postLayout, true, + accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @Override @@ -840,7 +835,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, mCustomThemeWrapper, locale, - windowWidth, accessToken, accountName, postType, postLayout, true, + accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @Override @@ -891,7 +886,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { StaggeredGridLayoutManagerItemOffsetDecoration itemDecoration = new StaggeredGridLayoutManagerItemOffsetDecoration(activity, R.dimen.staggeredLayoutManagerItemOffset, nColumns); mPostRecyclerView.addItemDecoration(itemDecoration); - windowWidth /= 2; } if (recyclerViewPosition > 0) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index e912418d..de32b033 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -16,7 +16,6 @@ import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Handler; -import android.util.DisplayMetrics; import android.view.HapticFeedbackConstants; import android.view.LayoutInflater; import android.view.Menu; @@ -224,7 +223,6 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic private boolean mExpandChildren; private boolean mSeparatePostAndComments = false; private boolean mMarkPostsAsRead; - private int mWindowWidth; private ConcatAdapter mConcatAdapter; private PostDetailRecyclerViewAdapter mPostAdapter; private CommentsRecyclerViewAdapter mCommentsAdapter; @@ -290,10 +288,6 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic showToast = true; } - DisplayMetrics displayMetrics = new DisplayMetrics(); - activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); - mWindowWidth = displayMetrics.widthPixels; - mLockFab = mSharedPreferences.getBoolean(SharedPreferencesUtils.LOCK_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON, false); mSwipeUpToHideFab = mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_UP_TO_HIDE_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON, false); mExpandChildren = !mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_TOP_LEVEL_COMMENTS_FIRST, false); @@ -550,7 +544,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mPostAdapter = new PostDetailRecyclerViewAdapter(activity, this, mExecutor, mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, mRedditDataRoomDatabase, mGlide, - mWindowWidth, mSeparatePostAndComments, mAccessToken, mAccountName, mPost, mLocale, + mSeparatePostAndComments, mAccessToken, mAccountName, mPost, mLocale, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostDetailsSharedPreferences, mExoCreator, post -> EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition))); mCommentsAdapter = new CommentsRecyclerViewAdapter(activity, @@ -1195,7 +1189,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mPostAdapter = new PostDetailRecyclerViewAdapter(activity, ViewPostDetailFragment.this, mExecutor, mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, - mRedditDataRoomDatabase, mGlide, mWindowWidth, mSeparatePostAndComments, + mRedditDataRoomDatabase, mGlide, mSeparatePostAndComments, mAccessToken, mAccountName, mPost, mLocale, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostDetailsSharedPreferences, mExoCreator, diff --git a/app/src/main/res/layout/item_post_with_preview.xml b/app/src/main/res/layout/item_post_with_preview.xml index 91014578..e0162b1c 100644 --- a/app/src/main/res/layout/item_post_with_preview.xml +++ b/app/src/main/res/layout/item_post_with_preview.xml @@ -216,8 +216,7 @@ android:id="@+id/image_view_item_post_with_preview" android:layout_width="match_parent" android:layout_height="wrap_content" - android:adjustViewBounds="true" - android:scaleType="fitStart" /> + android:adjustViewBounds="true" /> <ImageView android:id="@+id/video_or_gif_indicator_image_view_item_post_with_preview" |