Skip to content

Commit

Permalink
git animation callback added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabari committed May 31, 2018
1 parent c441d83 commit 7c2ad93
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 14 deletions.
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ android {
minSdkVersion MIN_SDK_VERSION as int
targetSdkVersion TARGET_SDK_VERSION as int
versionName VERSION_NAME as String
vectorDrawables.useSupportLibrary = true
}

compileOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class GifDrawable extends Drawable implements GifFrameLoader.FrameCallbac
private boolean applyGravity;
private Paint paint;
private Rect destRect;
private AnimationListener animationListener;

/**
* Constructor for GifDrawable.
Expand All @@ -94,7 +95,6 @@ public class GifDrawable extends Drawable implements GifFrameLoader.FrameCallbac
* @param gifDecoder The decoder to use to decode GIF data.
* @param firstFrame The decoded and transformed first frame of this GIF.
* @see #setFrameTransformation(com.bumptech.glide.load.Transformation, android.graphics.Bitmap)
*
* @deprecated Use {@link #GifDrawable(Context, GifDecoder, Transformation, int, int, Bitmap)}
*/
@SuppressWarnings("deprecation")
Expand All @@ -110,7 +110,7 @@ public GifDrawable(
this(context, gifDecoder, frameTransformation, targetFrameWidth, targetFrameHeight, firstFrame);
}

/**
/**
* Constructor for GifDrawable.
*
* @param context A context.
Expand Down Expand Up @@ -234,12 +234,18 @@ private void startRunning() {
isRunning = true;
state.frameLoader.subscribe(this);
invalidateSelf();
if (animationListener != null) {
animationListener.onAnimationStarted();
}
}
}

private void stopRunning() {
isRunning = false;
state.frameLoader.unsubscribe(this);
if (animationListener != null) {
animationListener.onAnimationEnded();
}
}

@Override
Expand Down Expand Up @@ -348,6 +354,7 @@ public void onFrameReady() {

if (getFrameIndex() == getFrameCount() - 1) {
loopCount++;
//This is the last index of Frame
}

if (maxLoopCount != LOOP_FOREVER && loopCount >= maxLoopCount) {
Expand Down Expand Up @@ -391,8 +398,7 @@ public void setLoopCount(int loopCount) {
}

static final class GifState extends ConstantState {
@VisibleForTesting
final GifFrameLoader frameLoader;
@VisibleForTesting final GifFrameLoader frameLoader;

GifState(GifFrameLoader frameLoader) {
this.frameLoader = frameLoader;
Expand All @@ -415,4 +421,14 @@ public int getChangingConfigurations() {
return 0;
}
}

public void setOnAnimationListener(AnimationListener listener) {
this.animationListener = listener;
}

public interface AnimationListener {
void onAnimationStarted();

void onAnimationEnded();
}
}
2 changes: 2 additions & 0 deletions samples/giphy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.2'
implementation "com.android.support:recyclerview-v7:${ANDROID_SUPPORT_VERSION}"
implementation "com.android.support:support-fragment:${ANDROID_SUPPORT_VERSION}"
implementation "com.android.support:support-vector-drawable:${ANDROID_SUPPORT_VERSION}"
annotationProcessor project(':annotation:compiler')
}

Expand All @@ -18,6 +19,7 @@ android {
applicationId 'com.bumptech.glide.samples.giphy'
minSdkVersion MIN_SDK_VERSION as int
targetSdkVersion TARGET_SDK_VERSION as int
vectorDrawables.useSupportLibrary = true
versionCode 1
versionName '1.0'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.load.resource.gif.GifDrawable.AnimationListener;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.google.gson.Gson;
Expand All @@ -39,8 +44,9 @@ protected void onCreate(Bundle savedInstanceState) {
String resultJson = getIntent().getStringExtra(EXTRA_RESULT_JSON);
final Api.GifResult result = new Gson().fromJson(resultJson, Api.GifResult.class);

ImageView gifView = (ImageView) findViewById(R.id.fullscreen_gif);

ImageView gifView = findViewById(R.id.gif_preview);
final ImageView gifPlay = findViewById(R.id.gif_play);
gifPlay.setImageDrawable(getTintedVectorAsset(this, R.drawable.ic_gif_watermark));
gifView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down Expand Up @@ -77,12 +83,38 @@ public boolean onResourceReady(Drawable resource, Object model, Target<Drawable>
DataSource dataSource, boolean isFirstResource) {
if (resource instanceof GifDrawable) {
gifDrawable = (GifDrawable) resource;
gifDrawable.setOnAnimationListener(new AnimationListener() {
@Override
public void onAnimationStarted() {
Log.d("onAnimation", "Started");
gifPlay.setVisibility(View.GONE);
}

@Override
public void onAnimationEnded() {
Log.d("onAnimation", "Stopped");
gifPlay.setVisibility(View.VISIBLE);
}
});
} else {
gifDrawable = null;
}
return false;
}
})
.into(gifView);
.into(new GifDrawableImageViewTarget(gifView, 3));
}

/**
* @param context UI context.
* @param drawableVectorRes Drawable vector resource.
* @return get tintd vector drawable
*/
public static Drawable getTintedVectorAsset(Context context,
@DrawableRes int drawableVectorRes) {
VectorDrawableCompat nonWhite = VectorDrawableCompat.create(context.getResources(),
drawableVectorRes, context.getTheme());
Drawable white = DrawableCompat.wrap(nonWhite);
return white;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.bumptech.glide.samples.giphy;

import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.target.ImageViewTarget;

/**
* Created by sabari on 30/3/18.
*/

public class GifDrawableImageViewTarget extends ImageViewTarget<Drawable> {

private int mLoopCount = GifDrawable.LOOP_FOREVER;

GifDrawableImageViewTarget(ImageView view, int loopCount) {
super(view);
mLoopCount = loopCount;
}

@Override
protected void setResource(@Nullable Drawable resource) {
if (resource instanceof GifDrawable) {
((GifDrawable) resource).setLoopCount(mLoopCount);
}
view.setImageDrawable(resource);
}

}
13 changes: 13 additions & 0 deletions samples/giphy/src/main/res/drawable/ic_gif_watermark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="30dp"
android:viewportHeight="30.0"
android:viewportWidth="50.0"
android:width="50dp">
<path
android:fillAlpha="0.6"
android:fillColor="@android:color/transparent"
android:pathData="M5.846,1L43.15,1C44.487,1 44.972,1.139 45.461,1.401C45.95,1.662 46.333,2.046 46.595,2.535C46.856,3.024 46.996,3.509 46.996,4.846L46.996,24.424C46.996,25.761 46.856,26.246 46.595,26.735C46.333,27.224 45.95,27.608 45.461,27.869C44.972,28.131 44.487,28.27 43.15,28.27L5.846,28.27C4.509,28.27 4.024,28.131 3.535,27.869C3.046,27.608 2.662,27.224 2.401,26.735C2.139,26.246 2,25.761 2,24.424L2,4.846C2,3.509 2.139,3.024 2.401,2.535C2.662,2.046 3.046,1.662 3.535,1.401C4.024,1.139 4.509,1 5.846,1ZM21.942,18.586L21.942,14.675L18.162,14.675L18.162,15.811L20.499,15.811L20.499,18.235C20.333,18.44 20.057,18.623 19.671,18.784C19.285,18.945 18.79,19.026 18.184,19.026C17.325,19.026 16.617,18.719 16.06,18.107C15.504,17.494 15.225,16.714 15.225,15.767L15.225,13.555C15.225,12.617 15.474,11.843 15.972,11.233C16.47,10.623 17.127,10.317 17.943,10.317C18.748,10.317 19.365,10.518 19.792,10.918C20.219,11.318 20.455,11.829 20.499,12.449L21.876,12.449L21.89,12.405C21.876,11.458 21.519,10.684 20.821,10.083C20.123,9.482 19.163,9.182 17.943,9.182C16.717,9.182 15.716,9.59 14.94,10.405C14.163,11.221 13.775,12.275 13.775,13.569L13.775,15.767C13.775,17.061 14.191,18.115 15.024,18.931C15.856,19.746 16.91,20.154 18.184,20.154C19.18,20.154 19.992,19.99 20.62,19.663C21.247,19.336 21.688,18.977 21.942,18.586ZM25.963,20L25.963,9.336L24.512,9.336L24.512,20L25.963,20ZM34.986,15.283L34.986,14.148L30.123,14.148L30.123,10.471L35.733,10.471L35.733,9.336L28.68,9.336L28.68,20L30.123,20L30.123,15.283L34.986,15.283Z"
android:strokeAlpha="0.6"
android:strokeColor="#F5F5F5"
android:strokeWidth="1"/>
</vector>
30 changes: 23 additions & 7 deletions samples/giphy/src/main/res/layout/fullscreen_activity.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fullscreen_gif"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/fullscreen_description"
/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/gif_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/gif_preview"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"/>

<ImageButton
android:id="@+id/gif_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@null"
android:visibility="visible"/>
</FrameLayout>

0 comments on commit 7c2ad93

Please sign in to comment.