From 6e5a0e7ef37ab148123a2fed2e50f84978e1f45a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 28 Oct 2018 23:18:01 +0800 Subject: Use AspectRatiotImageView instead of ImageView to fully display the preview images of posts and prevent scrolling jump in PostFragment and ViewPostDetailActivity. --- app/build.gradle | 1 + .../infinityforreddit/ParsePost.java | 60 +++++++++++++++++----- .../infinityforreddit/PostData.java | 25 ++++++++- .../infinityforreddit/PostRecyclerViewAdapter.java | 5 +- .../SubscribedSubredditRecyclerViewAdapter.java | 2 +- .../infinityforreddit/ViewPostDetailActivity.java | 22 +++++--- .../main/res/layout/activity_view_post_detail.xml | 6 ++- app/src/main/res/layout/item_post.xml | 9 ++-- 8 files changed, 101 insertions(+), 29 deletions(-) (limited to 'app') diff --git a/app/build.gradle b/app/build.gradle index 1a73fb07..7a97d6a9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -64,4 +64,5 @@ dependencies { annotationProcessor 'com.google.dagger:dagger-compiler:2.17' implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' + implementation 'com.github.santalu:aspect-ratio-imageview:1.0.6' } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index 2f8a4091..5dcb673d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -82,19 +82,27 @@ class ParsePost { String permalink = data.getString(JSONUtils.PERMALINK_KEY); String previewUrl = ""; + int previewWidth = -1; + int previewHeight = -1; if(data.has(JSONUtils.PREVIEW_KEY)) { previewUrl = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0) .getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); + previewWidth = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0) + .getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.WIDTH_KEY); + previewHeight = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0) + .getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.HEIGHT_KEY); } if(data.has(JSONUtils.CROSSPOST_PARENT_LIST)) { //Cross post data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0); parseData(data, permalink, newPostData, id, fullName, subredditNamePrefixed, - formattedPostTime, title, previewUrl, score, voteType, gilded, nsfw, stickied, true, i); + formattedPostTime, title, previewUrl, previewWidth, previewHeight, + score, voteType, gilded, nsfw, stickied, true, i); } else { parseData(data, permalink, newPostData, id, fullName, subredditNamePrefixed, - formattedPostTime, title, previewUrl, score, voteType, gilded, nsfw, stickied, false, i); + formattedPostTime, title, previewUrl, previewWidth, previewHeight, + score, voteType, gilded, nsfw, stickied, false, i); } } } catch (JSONException e) { @@ -117,7 +125,7 @@ class ParsePost { private static void parseData(JSONObject data, String permalink, ArrayList bestPostData, String id, String fullName, String subredditNamePrefixed, String formattedPostTime, - String title, String previewUrl, int score, int voteType, int gilded, + String title, String previewUrl, int previewWidth, int previewHeight ,int score, int voteType, int gilded, boolean nsfw, boolean stickied, boolean isCrosspost, int i) throws JSONException { boolean isVideo = data.getBoolean(JSONUtils.IS_VIDEO_KEY); String url = data.getString(JSONUtils.URL_KEY); @@ -140,7 +148,8 @@ class ParsePost { Log.i("no preview link", Integer.toString(i)); int postType = PostData.NO_PREVIEW_LINK_TYPE; PostData linkPostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, - title, previewUrl, url, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); + title, previewUrl, url, permalink, score, postType, + voteType, gilded, nsfw, stickied, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { linkPostData.setSelfText(""); } else { @@ -156,8 +165,11 @@ class ParsePost { String videoUrl = redditVideoObject.getString(JSONUtils.DASH_URL_KEY); PostData videoPostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, - title, previewUrl, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost, true); + title, previewUrl, permalink, score, postType, voteType, + gilded, nsfw, stickied, isCrosspost, true); + videoPostData.setPreviewWidth(previewWidth); + videoPostData.setPreviewHeight(previewHeight); videoPostData.setVideoUrl(videoUrl); videoPostData.setDownloadableGifOrVideo(false); @@ -171,8 +183,11 @@ class ParsePost { String videoUrl = variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.MP4_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); String gifDownloadUrl = variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.GIF_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); PostData post = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, title, - previewUrl, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost, false); + previewUrl, permalink, score, postType, voteType, + gilded, nsfw, stickied, isCrosspost, false); + post.setPreviewWidth(previewWidth); + post.setPreviewHeight(previewHeight); post.setVideoUrl(videoUrl); post.setDownloadableGifOrVideo(true); post.setGifOrVideoDownloadUrl(gifDownloadUrl); @@ -186,8 +201,11 @@ class ParsePost { .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.DASH_URL_KEY); PostData post = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, title, - previewUrl, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost, true); + previewUrl, permalink, score, postType, voteType, + gilded, nsfw, stickied, isCrosspost, true); + post.setPreviewWidth(previewWidth); + post.setPreviewHeight(previewHeight); post.setVideoUrl(videoUrl); post.setDownloadableGifOrVideo(false); @@ -197,8 +215,15 @@ class ParsePost { //Image post Log.i("image", Integer.toString(i)); int postType = PostData.IMAGE_TYPE; - bestPostData.add(new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, - title, url, url, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost)); + + PostData imagePostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, + title, url, url, permalink, score, postType, + voteType, gilded, nsfw, stickied, isCrosspost); + + imagePostData.setPreviewWidth(previewWidth); + imagePostData.setPreviewHeight(previewHeight); + + bestPostData.add(imagePostData); } else { if (url.contains(permalink)) { //Text post but with a preview @@ -206,6 +231,10 @@ class ParsePost { int postType = PostData.TEXT_TYPE; PostData textWithImagePostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, title, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); + + textWithImagePostData.setPreviewWidth(previewWidth); + textWithImagePostData.setPreviewHeight(previewHeight); + if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { textWithImagePostData.setSelfText(""); } else { @@ -217,12 +246,17 @@ class ParsePost { Log.i("link", Integer.toString(i)); int postType = PostData.LINK_TYPE; PostData linkPostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, - title, previewUrl, url, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); + title, previewUrl, url, permalink, score, + postType, voteType, gilded, nsfw, stickied, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { linkPostData.setSelfText(""); } else { linkPostData.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); } + + linkPostData.setPreviewWidth(previewWidth); + linkPostData.setPreviewHeight(previewHeight); + bestPostData.add(linkPostData); } } @@ -233,13 +267,15 @@ class ParsePost { Log.i("CP no preview image", Integer.toString(i)); int postType = PostData.IMAGE_TYPE; bestPostData.add(new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, title, - url, url, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost)); + url, url, permalink, score, postType, voteType, + gilded, nsfw, stickied, isCrosspost)); } else { //Link post Log.i("CP no preview link", Integer.toString(i)); int postType = PostData.LINK_TYPE; PostData linkPostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, - title, previewUrl, url, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); + title, previewUrl, url, permalink, score, postType, + voteType, gilded, nsfw, stickied, isCrosspost); bestPostData.add(linkPostData); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostData.java index 0ece0b00..02d7be4c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostData.java @@ -31,11 +31,14 @@ class PostData implements Parcelable { private int postType; private int voteType; private int gilded; + private int previewWidth; + private int previewHeight; private boolean nsfw; private boolean stickied; private boolean isCrosspost; private boolean isDashVideo; private boolean isDownloadableGifOrVideo; + private PostData crosspostParentPostData; PostData(String id, String fullName, String subredditNamePrefixed, String postTime, String title, String previewUrl, String permalink, int score, int postType, int voteType, int gilded, @@ -112,6 +115,8 @@ class PostData implements Parcelable { postType = in.readInt(); voteType = in.readInt(); gilded = in.readInt(); + previewWidth = in.readInt(); + previewHeight = in.readInt(); nsfw = in.readByte() != 0; stickied = in.readByte() != 0; isCrosspost = in.readByte() != 0; @@ -223,6 +228,22 @@ class PostData implements Parcelable { return gilded; } + void setPreviewWidth(int previewWidth) { + this.previewWidth = previewWidth; + } + + int getPreviewWidth() { + return previewWidth; + } + + void setPreviewHeight(int previewHeight) { + this.previewHeight = previewHeight; + } + + int getPreviewHeight() { + return previewHeight; + } + public boolean isNSFW() { return nsfw; } @@ -270,10 +291,12 @@ class PostData implements Parcelable { parcel.writeInt(postType); parcel.writeInt(voteType); parcel.writeInt(gilded); + parcel.writeInt(previewWidth); + parcel.writeInt(previewHeight); parcel.writeByte((byte) (nsfw ? 1 : 0)); parcel.writeByte((byte) (stickied ? 1 : 0)); parcel.writeByte((byte) (isCrosspost ? 1 : 0)); parcel.writeByte((byte) (isDashVideo ? 1 : 0)); parcel.writeByte((byte) (isDownloadableGifOrVideo ? 1 : 0)); } -} +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 86b1254a..b750595d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -32,6 +32,7 @@ import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; +import com.santalu.aspectratioimageview.AspectRatioImageView; import java.util.ArrayList; @@ -206,6 +207,8 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter() { + RequestBuilder imageRequestBuilder = Glide.with(this).load(mPostData.getPreviewUrl()) + .apply(new RequestOptions().override(mPostData.getPreviewWidth(), mPostData.getPreviewHeight())) + .listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { mLoadImageProgressBar.setVisibility(View.GONE); diff --git a/app/src/main/res/layout/activity_view_post_detail.xml b/app/src/main/res/layout/activity_view_post_detail.xml index b9c13bcb..6f6bac2a 100644 --- a/app/src/main/res/layout/activity_view_post_detail.xml +++ b/app/src/main/res/layout/activity_view_post_detail.xml @@ -100,6 +100,7 @@ android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_centerVertical="true" + android:textSize="12sp" android:background="@drawable/rounded_corner" android:textColor="@android:color/white" /> @@ -156,7 +157,7 @@ android:layout_marginTop="16dp" android:visibility="gone"> - + android:layout_height="wrap_content" + android:layout_centerInParent="true"> @@ -146,11 +147,11 @@ android:layout_height="wrap_content" android:layout_centerInParent="true" /> - + android:layout_height="wrap_content" + android:scaleType="fitStart" />