Skip to content

Commit

Permalink
Play GIFs in home grid when touched.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbutcher committed Dec 1, 2015
1 parent 69f5fa4 commit 6024058
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
67 changes: 59 additions & 8 deletions app/src/main/java/io/plaidapp/ui/FeedAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import android.graphics.Color;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
Expand All @@ -37,6 +39,7 @@
import android.transition.TransitionInflater;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
Expand All @@ -47,6 +50,7 @@
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

Expand Down Expand Up @@ -150,6 +154,17 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
}

@Override
public void onViewRecycled(RecyclerView.ViewHolder holder) {
if (holder instanceof DribbbleShotHolder) {
// reset the badge & ripple which are dynamically determined
DribbbleShotHolder shotHolder = (DribbbleShotHolder) holder;
shotHolder.image.showBadge(false);
shotHolder.image.setForeground(
ContextCompat.getDrawable(host, R.drawable.mid_grey_ripple));
}
}

@NonNull
private DesignerNewsStoryHolder createDesignerNewsStoryHolder(ViewGroup parent) {
final DesignerNewsStoryHolder holder = new DesignerNewsStoryHolder(layoutInflater.inflate(
Expand Down Expand Up @@ -207,7 +222,7 @@ private void bindDesignerNewsStory(final Story story, final DesignerNewsStoryHol
private DribbbleShotHolder createDribbbleShotHolder(ViewGroup parent) {
final DribbbleShotHolder holder = new DribbbleShotHolder(
layoutInflater.inflate(R.layout.dribbble_shot_item, parent, false));
holder.itemView.setOnClickListener(new View.OnClickListener() {
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.itemView.setTransitionName(holder.itemView.getResources().getString(R
Expand All @@ -227,12 +242,45 @@ public void onClick(View view) {
host.startActivity(intent, options.toBundle());
}
});
// play animated GIFs whilst touched
holder.image.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// get the image and check if it's an animated GIF
final Drawable drawable = holder.image.getDrawable();
if (drawable == null) return false;
GifDrawable gif = null;
if (drawable instanceof GifDrawable) {
gif = (GifDrawable) drawable;
} else if (drawable instanceof TransitionDrawable) {
// we fade in images on load which uses a TransitionDrawable; check its layers
TransitionDrawable fadingIn = (TransitionDrawable) drawable;
for (int i = 0; i < fadingIn.getNumberOfLayers(); i++) {
if (fadingIn.getDrawable(i) instanceof GifDrawable) {
gif = (GifDrawable) fadingIn.getDrawable(i);
break;
}
}
}
if (gif == null) return false;
// GIF found, start/stop it on press/lift
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
gif.start();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
gif.stop();
break;
}
return false;
}
});
return holder;
}

private void bindDribbbleShotHolder(final Shot shot,
final DribbbleShotHolder holder) {
final BadgedFourThreeImageView iv = (BadgedFourThreeImageView) holder.itemView;
final int[] imageSize = shot.images.bestSize();
Glide.with(host)
.load(shot.images.best())
Expand All @@ -244,7 +292,7 @@ public boolean onResourceReady(GlideDrawable resource, String model,
isFromMemoryCache, boolean
isFirstResource) {
if (!shot.hasFadedIn) {
iv.setHasTransientState(true);
holder.image.setHasTransientState(true);
final ObservableColorMatrix cm = new ObservableColorMatrix();
ObjectAnimator saturation = ObjectAnimator.ofFloat(cm,
ObservableColorMatrix.SATURATION, 0f, 1f);
Expand All @@ -255,9 +303,9 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) {
// just animating the color matrix does not invalidate the
// drawable so need this update listener. Also have to create a
// new CMCF as the matrix is immutable :(
if (iv.getDrawable() != null) {
iv.getDrawable().setColorFilter(new
ColorMatrixColorFilter(cm));
if (holder.image.getDrawable() != null) {
holder.image.getDrawable().setColorFilter(
new ColorMatrixColorFilter(cm));
}
}
});
Expand All @@ -267,7 +315,7 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) {
saturation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
iv.setHasTransientState(false);
holder.image.setHasTransientState(false);
}
});
saturation.start();
Expand All @@ -287,7 +335,7 @@ public boolean onException(Exception e, String model, Target<GlideDrawable>
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.fitCenter()
.override(imageSize[0], imageSize[1])
.into(new DribbbleTarget(iv, false));
.into(new DribbbleTarget(holder.image, false));
}

@NonNull
Expand Down Expand Up @@ -536,8 +584,11 @@ public Class[] getDividedViewHolderClasses() {

/* protected */ class DribbbleShotHolder extends RecyclerView.ViewHolder {

BadgedFourThreeImageView image;

public DribbbleShotHolder(View itemView) {
super(itemView);
image = (BadgedFourThreeImageView) itemView;
}

}
Expand Down
13 changes: 6 additions & 7 deletions app/src/main/java/io/plaidapp/util/glide/DribbbleTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.bumptech.glide.request.target.SizeReadyCallback;

import io.plaidapp.R;
import io.plaidapp.ui.widget.BadgedFourThreeImageView;
Expand All @@ -39,18 +38,18 @@
public class DribbbleTarget extends GlideDrawableImageViewTarget implements
Palette.PaletteAsyncListener {

private final boolean playGifs;
private final boolean autoplayGifs;

public DribbbleTarget(BadgedFourThreeImageView view, boolean playGifs) {
public DribbbleTarget(BadgedFourThreeImageView view, boolean autoplayGifs) {
super(view);
this.playGifs = playGifs;
this.autoplayGifs = autoplayGifs;
}

@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable>
animation) {
super.onResourceReady(resource, animation);
if (!playGifs) {
if (!autoplayGifs) {
resource.stop();
}

Expand Down Expand Up @@ -81,14 +80,14 @@ public void onResourceReady(GlideDrawable resource, GlideAnimation<? super Glide

@Override
public void onStart() {
if (playGifs) {
if (autoplayGifs) {
super.onStart();
}
}

@Override
public void onStop() {
if (playGifs) {
if (autoplayGifs) {
super.onStop();
}
}
Expand Down

0 comments on commit 6024058

Please sign in to comment.