From 60b659e651b4d8e7d9ca26b17ba8990497022ed1 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 22 Dec 2018 10:42:24 +0800 Subject: Use GifImageView to display all the images in order to prevent slow playing of the gifs. Extend GifImageView as AspectRatioGifImageView to retain the features of AspectRatioImageView. --- app/build.gradle | 5 +- .../java/CustomView/AspectRatioGifImageView.java | 54 +++++++++++++++++ .../infinityforreddit/PostFragment.java | 2 +- .../infinityforreddit/PostRecyclerViewAdapter.java | 67 ++++++++++++++++++---- .../SubscribedSubredditRecyclerViewAdapter.java | 45 +++++++++++++-- .../SubscribedUserRecyclerViewAdapter.java | 43 +++++++++++--- .../ViewSubredditDetailActivity.java | 43 +++++++++++--- .../res/layout/activity_view_subreddit_detail.xml | 4 +- app/src/main/res/layout/item_post.xml | 8 +-- .../main/res/layout/item_subscribed_subreddit.xml | 4 +- 10 files changed, 234 insertions(+), 41 deletions(-) create mode 100644 app/src/main/java/CustomView/AspectRatioGifImageView.java (limited to 'app') diff --git a/app/build.gradle b/app/build.gradle index 7a97d6a9..317de2a4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,7 +43,7 @@ dependencies { implementation 'com.android.support:customtabs:28.0.0' implementation 'com.alexvasilkov:gesture-views:2.5.2' implementation 'com.android.support:cardview-v7:28.0.0' - implementation 'com.github.bumptech.glide:glide:4.7.1' + implementation 'com.github.bumptech.glide:glide:4.8.0' implementation 'com.github.pwittchen:swipe-rx2:0.3.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1' // Room components @@ -58,11 +58,12 @@ dependencies { implementation 'org.sufficientlysecure:html-textview:3.6' implementation 'com.squareup.retrofit2:retrofit:2.4.0' implementation 'com.squareup.retrofit2:converter-scalars:2.4.0' - implementation 'jp.wasabeef:glide-transformations:3.3.0' + implementation 'jp.wasabeef:glide-transformations:4.0.0' implementation 'com.muditsen.multilevelrecyclerview:multilevelview:1.0.0' implementation 'com.google.dagger:dagger:2.17' 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' + implementation 'com.felipecsl:gifimageview:2.2.0' } diff --git a/app/src/main/java/CustomView/AspectRatioGifImageView.java b/app/src/main/java/CustomView/AspectRatioGifImageView.java new file mode 100644 index 00000000..379c8697 --- /dev/null +++ b/app/src/main/java/CustomView/AspectRatioGifImageView.java @@ -0,0 +1,54 @@ +package CustomView; + +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; + +import com.felipecsl.gifimageview.library.GifImageView; + +public class AspectRatioGifImageView extends GifImageView { + private float ratio; + + public final float getRatio() { + return this.ratio; + } + + public final void setRatio(float var1) { + this.ratio = var1; + } + + private final void init(Context context, AttributeSet attrs) { + if (attrs != null) { + TypedArray a = context.obtainStyledAttributes(attrs, com.santalu.aspectratioimageview.R.styleable.AspectRatioImageView); + this.ratio = a.getFloat(com.santalu.aspectratioimageview.R.styleable.AspectRatioImageView_ari_ratio, 1.0F); + a.recycle(); + } + + } + + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + int width = this.getMeasuredWidth(); + int height = this.getMeasuredHeight(); + if (width != 0 || height != 0) { + if (width > 0) { + height = (int)((float)width * this.ratio); + } else { + width = (int)((float)height / this.ratio); + } + + this.setMeasuredDimension(width, height); + } + } + + public AspectRatioGifImageView(Context context) { + super(context); + this.ratio = 1.0F; + } + + public AspectRatioGifImageView(Context context, AttributeSet attrs) { + super(context, attrs); + this.ratio = 1.0F; + this.init(context, attrs); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 0b856945..e3a2ba5a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -61,7 +61,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private PostRecyclerViewAdapter mAdapter; - private PostViewModel mPostViewModel; + PostViewModel mPostViewModel; @Inject @Named("no_oauth") Retrofit mRetrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index a3a7e504..66c29d65 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.ColorFilter; +import android.graphics.drawable.Animatable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.support.annotation.NonNull; @@ -32,14 +33,14 @@ 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; +import CustomView.AspectRatioGifImageView; import butterknife.BindView; import butterknife.ButterKnife; -import de.hdodenhof.circleimageview.CircleImageView; import jp.wasabeef.glide.transformations.BlurTransformation; +import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import retrofit2.Retrofit; /** @@ -115,10 +116,29 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + if(resource instanceof Animatable) { + //This is a gif + //((Animatable) resource).start(); + ((DataViewHolder) holder).subredditIconGifImageView.startAnimation(); + } + return false; + } + }) + .into(((DataViewHolder) holder).subredditIconGifImageView); } else { glide.load(R.drawable.subreddit_default_icon) - .into(((DataViewHolder) holder).subredditIconCircleImageView); + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(((DataViewHolder) holder).subredditIconGifImageView); } if(holder.getAdapterPosition() >= 0) { @@ -128,9 +148,30 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + if(resource instanceof Animatable) { + //This is a gif + //((Animatable) resource).start(); + ((DataViewHolder) holder).subredditIconGifImageView.startAnimation(); + } + return false; + } + }) + .into(((DataViewHolder) holder).subredditIconGifImageView); } else { - glide.load(R.drawable.subreddit_default_icon).into(((DataViewHolder) holder).subredditIconCircleImageView); + glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(((DataViewHolder) holder).subredditIconGifImageView); } ((DataViewHolder) holder).cardView.setOnClickListener(new View.OnClickListener() { @@ -146,7 +187,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter { private Context mContext; @@ -54,9 +63,30 @@ class SubscribedSubredditRecyclerViewAdapter extends RecyclerView.Adapter() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + if(resource instanceof Animatable) { + //This is a gif + //((Animatable) resource).start(); + ((SubredditViewHolder) viewHolder).iconGifImageView.startAnimation(); + } + return false; + } + }).into(((SubredditViewHolder) viewHolder).iconGifImageView); + } else { - glide.load(R.drawable.subreddit_default_icon).into(((SubredditViewHolder) viewHolder).iconCircleImageView); + glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(((SubredditViewHolder) viewHolder).iconGifImageView); } ((SubredditViewHolder) viewHolder).subredditNameTextView.setText(mSubscribedSubredditData.get(i).getName()); } @@ -71,7 +101,10 @@ class SubscribedSubredditRecyclerViewAdapter extends RecyclerView.Adapter subscribedSubreddits){ @@ -81,12 +114,12 @@ class SubscribedSubredditRecyclerViewAdapter extends RecyclerView.Adapter { private List mSubscribedUserData; @@ -36,7 +45,7 @@ class SubscribedUserRecyclerViewAdapter extends RecyclerView.Adapter() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + if(resource instanceof Animatable) { + //This is a gif + ((Animatable) resource).start(); + ((SubscribedUserRecyclerViewAdapter.UserViewHolder) viewHolder).iconGifImageView.startAnimation(); + } + return false; + } + }).into(((UserViewHolder) viewHolder).iconGifImageView); } else { - glide.load(R.drawable.subreddit_default_icon).into(((UserViewHolder) viewHolder).iconCircleImageView); + glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(((UserViewHolder) viewHolder).iconGifImageView); } ((UserViewHolder) viewHolder).subredditNameTextView.setText(mSubscribedUserData.get(i).getName()); } @@ -63,7 +92,7 @@ class SubscribedUserRecyclerViewAdapter extends RecyclerView.Adapter subscribedUsers){ @@ -73,12 +102,12 @@ class SubscribedUserRecyclerViewAdapter extends RecyclerView.Adapter() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + if(resource instanceof Animatable) { + //This is a gif + ((Animatable) resource).start(); + iconGifImageView.startAnimation(); + } + return false; + } + }) + .into(iconGifImageView); + iconGifImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageActivity.class); diff --git a/app/src/main/res/layout/activity_view_subreddit_detail.xml b/app/src/main/res/layout/activity_view_subreddit_detail.xml index 67bf3928..528518e8 100644 --- a/app/src/main/res/layout/activity_view_subreddit_detail.xml +++ b/app/src/main/res/layout/activity_view_subreddit_detail.xml @@ -31,8 +31,8 @@ android:scaleType="centerCrop" android:layout_height="180dp" /> - - @@ -148,7 +148,7 @@ android:layout_height="wrap_content" android:layout_centerInParent="true" /> - -