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. --- .../infinityforreddit/PostFragment.java | 2 +- .../infinityforreddit/PostRecyclerViewAdapter.java | 67 ++++++++++++++++++---- .../SubscribedSubredditRecyclerViewAdapter.java | 45 +++++++++++++-- .../SubscribedUserRecyclerViewAdapter.java | 43 +++++++++++--- .../ViewSubredditDetailActivity.java | 43 +++++++++++--- 5 files changed, 169 insertions(+), 31 deletions(-) (limited to 'app/src/main/java/ml') 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); -- cgit v1.2.3