提交 5c3697ff 编写于 作者: S Sam Judd

Use explicit GlideDrawable class.

上级 193e2f8f
......@@ -13,34 +13,35 @@ import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.ImageVideoWrapper;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper;
import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperTransformation;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import com.bumptech.glide.manager.Lifecycle;
import com.bumptech.glide.manager.RequestTracker;
import com.bumptech.glide.provider.LoadProvider;
import com.bumptech.glide.request.animation.DrawableCrossFadeViewAnimation;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.DrawableCrossFadeViewAnimation;
import com.bumptech.glide.request.animation.ViewPropertyAnimation;
import com.bumptech.glide.request.target.Target;
import java.io.File;
/**
* A class for creating a request to load a {@link android.graphics.drawable.Drawable}.
* A class for creating a request to load a {@link GlideDrawable}.
*
* @param <ModelType> The type of model that will be loaded into the target.
*/
public class DrawableRequestBuilder<ModelType>
extends GenericRequestBuilder<ModelType, ImageVideoWrapper, GifBitmapWrapper, Drawable>
extends GenericRequestBuilder<ModelType, ImageVideoWrapper, GifBitmapWrapper, GlideDrawable>
implements BitmapOptions, DrawableOptions {
private final Glide glide;
private final Context context;
DrawableRequestBuilder(Context context, ModelType model,
LoadProvider<ModelType, ImageVideoWrapper, GifBitmapWrapper, Drawable> loadProvider, Glide glide,
LoadProvider<ModelType, ImageVideoWrapper, GifBitmapWrapper, GlideDrawable> loadProvider, Glide glide,
RequestTracker requestTracker, Lifecycle lifecycle) {
super(context, model, loadProvider, Drawable.class, glide, requestTracker, lifecycle);
super(context, model, loadProvider, GlideDrawable.class, glide, requestTracker, lifecycle);
this.context = context;
this.glide = glide;
......@@ -49,17 +50,17 @@ public class DrawableRequestBuilder<ModelType>
}
/**
* Loads and displays the {@link Drawable} retrieved by the given thumbnail request if it finishes before this
* request. Best used for loading thumbnail {@link Drawable}s that are smaller and will be loaded more quickly
* than the fullsize {@link Drawable}. There are no guarantees about the order in which the requests will actually
* finish. However, if the thumb request completes after the full request, the thumb {@link Drawable} will never
* replace the full image.
* Loads and displays the {@link GlideDrawable} retrieved by the given thumbnail request if it finishes before this
* request. Best used for loading thumbnail {@link GlideDrawable}s that are smaller and will be loaded more quickly
* than the fullsize {@link GlideDrawable}. There are no guarantees about the order in which the requests will
* actually finish. However, if the thumb request completes after the full request, the thumb {@link GlideDrawable}
* will never replace the full image.
*
* @see #thumbnail(float)
*
* <p>
* Note - Any options on the main request will not be passed on to the thumbnail request. For example, if
* you want an animation to occur when either the full {@link Drawable} loads or the thumbnail loads,
* you want an animation to occur when either the full {@link GlideDrawable} loads or the thumbnail loads,
* you need to call {@link #animate(int)} on both the thumb and the full request. For a simpler thumbnail
* option where these options are applied to the humbnail as well, see {@link #thumbnail(float)}.
* </p>
......@@ -82,7 +83,7 @@ public class DrawableRequestBuilder<ModelType>
*/
@Override
public DrawableRequestBuilder<ModelType> thumbnail(
GenericRequestBuilder<ModelType, ImageVideoWrapper, GifBitmapWrapper, Drawable> thumbnailRequest) {
GenericRequestBuilder<ModelType, ImageVideoWrapper, GifBitmapWrapper, GlideDrawable> thumbnailRequest) {
super.thumbnail(thumbnailRequest);
return this;
}
......@@ -142,7 +143,7 @@ public class DrawableRequestBuilder<ModelType>
}
/**
* Transform {@link android.graphics.drawable.Drawable}s using the given
* Transform {@link GlideDrawable}s using the given
* {@link com.bumptech.glide.load.resource.bitmap.BitmapTransformation}s.
*
* <p>
......@@ -163,7 +164,7 @@ public class DrawableRequestBuilder<ModelType>
}
/**
* Transform {@link Drawable}s using {@link com.bumptech.glide.load.resource.bitmap.CenterCrop}.
* Transform {@link GlideDrawable}s using {@link com.bumptech.glide.load.resource.bitmap.CenterCrop}.
*
* @see #fitCenter()
* @see #transform(com.bumptech.glide.load.resource.bitmap.BitmapTransformation...)
......@@ -177,8 +178,7 @@ public class DrawableRequestBuilder<ModelType>
}
/**
* Transform {@link android.graphics.drawable.Drawable}s using
* {@link com.bumptech.glide.load.resource.bitmap.FitCenter}.
* Transform {@link GlideDrawable}s using {@link com.bumptech.glide.load.resource.bitmap.FitCenter}.
*
* @see #centerCrop()
* @see #transform(com.bumptech.glide.load.resource.bitmap.BitmapTransformation...)
......@@ -192,8 +192,8 @@ public class DrawableRequestBuilder<ModelType>
}
/**
* Transform {@link android.graphics.drawable.Drawable}s using the given {@link android.graphics.Bitmap}
* transformations. Replaces any previous transformations.
* Transform {@link GlideDrawable}s using the given {@link android.graphics.Bitmap} transformations. Replaces any
* previous transformations.
*
* @see #fitCenter()
* @see #centerCrop()
......@@ -228,7 +228,8 @@ public class DrawableRequestBuilder<ModelType>
* {@inheritDoc}
*/
@Override
public DrawableRequestBuilder<ModelType> transcoder(ResourceTranscoder<GifBitmapWrapper, Drawable> transcoder) {
public DrawableRequestBuilder<ModelType> transcoder(
ResourceTranscoder<GifBitmapWrapper, GlideDrawable> transcoder) {
super.transcoder(transcoder);
return this;
}
......@@ -237,7 +238,7 @@ public class DrawableRequestBuilder<ModelType>
* {@inheritDoc}
*/
public DrawableRequestBuilder<ModelType> crossFade() {
super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory<Drawable>());
super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory<GlideDrawable>());
return this;
}
......@@ -245,7 +246,7 @@ public class DrawableRequestBuilder<ModelType>
* {@inheritDoc}
*/
public DrawableRequestBuilder<ModelType> crossFade(int duration) {
super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory<Drawable>(duration));
super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory<GlideDrawable>(duration));
return this;
}
......@@ -253,7 +254,7 @@ public class DrawableRequestBuilder<ModelType>
* {@inheritDoc}
*/
public DrawableRequestBuilder<ModelType> crossFade(Animation animation, int duration) {
super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory<Drawable>(animation, duration));
super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory<GlideDrawable>(animation, duration));
return this;
}
......@@ -261,7 +262,7 @@ public class DrawableRequestBuilder<ModelType>
* {@inheritDoc}
*/
public DrawableRequestBuilder<ModelType> crossFade(int animationId, int duration) {
super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory<Drawable>(context, animationId,
super.animate(new DrawableCrossFadeViewAnimation.DrawableCrossFadeFactory<GlideDrawable>(context, animationId,
duration));
return this;
}
......@@ -342,7 +343,7 @@ public class DrawableRequestBuilder<ModelType>
* {@inheritDoc}
*/
@Override
public DrawableRequestBuilder<ModelType> listener(RequestListener<ModelType, Drawable> requestListener) {
public DrawableRequestBuilder<ModelType> listener(RequestListener<ModelType, GlideDrawable> requestListener) {
super.listener(requestListener);
return this;
}
......@@ -405,7 +406,7 @@ public class DrawableRequestBuilder<ModelType>
* @return {@inheritDoc}
*/
@Override
public Target<Drawable> into(ImageView view) {
public Target<GlideDrawable> into(ImageView view) {
return super.into(view);
}
......
package com.bumptech.glide;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.ParcelFileDescriptor;
import com.bumptech.glide.load.model.ImageVideoModelLoader;
import com.bumptech.glide.load.model.ImageVideoWrapper;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import com.bumptech.glide.manager.Lifecycle;
......@@ -61,7 +61,7 @@ public class DrawableTypeRequest<ModelType> extends DrawableRequestBuilder<Model
RequestTracker requestTracker, Lifecycle lifecycle, RequestManager.OptionsApplier optionsApplier) {
super(context, model,
buildProvider(glide, streamModelLoader, fileDescriptorModelLoader, GifBitmapWrapper.class,
Drawable.class, null),
GlideDrawable.class, null),
glide, requestTracker, lifecycle);
this.model = model;
this.streamModelLoader = streamModelLoader;
......
......@@ -39,14 +39,15 @@ import com.bumptech.glide.load.resource.bitmap.FitCenter;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.load.resource.bitmap.ImageVideoDataLoadProvider;
import com.bumptech.glide.load.resource.bitmap.StreamBitmapDataLoadProvider;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.file.StreamFileDataLoadProvider;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.load.resource.gif.GifDrawableLoadProvider;
import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper;
import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperTransformation;
import com.bumptech.glide.load.resource.gifbitmap.ImageVideoGifDrawableLoadProvider;
import com.bumptech.glide.load.resource.transcode.GlideBitmapDrawableTranscoder;
import com.bumptech.glide.load.resource.transcode.GifBitmapWrapperDrawableTranscoder;
import com.bumptech.glide.load.resource.transcode.GlideBitmapDrawableTranscoder;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import com.bumptech.glide.load.resource.transcode.TranscoderRegistry;
import com.bumptech.glide.manager.RequestManagerRetriever;
......@@ -211,7 +212,7 @@ public class Glide {
transcoderRegistry.register(Bitmap.class, GlideBitmapDrawable.class,
new GlideBitmapDrawableTranscoder(context.getResources(), bitmapPool));
transcoderRegistry.register(GifBitmapWrapper.class, Drawable.class,
transcoderRegistry.register(GifBitmapWrapper.class, GlideDrawable.class,
new GifBitmapWrapperDrawableTranscoder(
new GlideBitmapDrawableTranscoder(context.getResources(), bitmapPool)));
......
......@@ -151,7 +151,7 @@ class GifFrameManager {
}
@Override
public void onResourceReady(final Bitmap resource, GlideAnimation<Bitmap> glideAnimation) {
public void onResourceReady(final Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
this.resource = resource;
mainHandler.postAtTime(this, targetTime);
}
......
package com.bumptech.glide.load.resource.transcode;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper;
/**
......@@ -11,28 +12,28 @@ import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper;
* {@link Bitmap} or an {@link com.bumptech.glide.load.resource.gif.GifDrawable} into an
* {@link android.graphics.drawable.Drawable}.
*/
public class GifBitmapWrapperDrawableTranscoder implements ResourceTranscoder<GifBitmapWrapper, Drawable> {
private final ResourceTranscoder<Bitmap, ? extends Drawable> bitmapDrawableResourceTranscoder;
public class GifBitmapWrapperDrawableTranscoder implements ResourceTranscoder<GifBitmapWrapper, GlideDrawable> {
private final ResourceTranscoder<Bitmap, GlideBitmapDrawable> bitmapDrawableResourceTranscoder;
public GifBitmapWrapperDrawableTranscoder(
ResourceTranscoder<Bitmap, ? extends Drawable> bitmapDrawableResourceTranscoder) {
ResourceTranscoder<Bitmap, GlideBitmapDrawable> bitmapDrawableResourceTranscoder) {
this.bitmapDrawableResourceTranscoder = bitmapDrawableResourceTranscoder;
}
@SuppressWarnings("unchecked")
@Override
public Resource<Drawable> transcode(Resource<GifBitmapWrapper> toTranscode) {
public Resource<GlideDrawable> transcode(Resource<GifBitmapWrapper> toTranscode) {
GifBitmapWrapper gifBitmap = toTranscode.get();
Resource<Bitmap> bitmapResource = gifBitmap.getBitmapResource();
final Resource<? extends Drawable> result;
final Resource<? extends GlideDrawable> result;
if (bitmapResource != null) {
result = bitmapDrawableResourceTranscoder.transcode(bitmapResource);
} else {
result = gifBitmap.getGifResource();
}
// This is unchecked but always safe, anything that extends a Drawable can be safely cast to a Drawable.
return (Resource<Drawable>) result;
return (Resource<GlideDrawable>) result;
}
@Override
......
......@@ -29,6 +29,6 @@ public class GlideBitmapDrawableTranscoder implements ResourceTranscoder<Bitmap,
@Override
public String getId() {
return "BitmapDrawableTranscoder.com.bumptech.glide.load.resource.transcode";
return "GlideBitmapDrawableTranscoder.com.bumptech.glide.load.resource.transcode";
}
}
......@@ -176,7 +176,7 @@ public class RequestFutureTarget<T, R> implements FutureTarget<R>, Runnable {
* A callback that should never be invoked directly.
*/
@Override
public synchronized void onResourceReady(R resource, GlideAnimation<R> glideAnimation) {
public synchronized void onResourceReady(R resource, GlideAnimation<? super R> glideAnimation) {
// We might get a null result.
resultReceived = true;
this.resource = resource;
......
package com.bumptech.glide.request.target;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
/**
* A {@link com.bumptech.glide.request.target.Target} that can display an {@link android.graphics.drawable.Drawable} in
* an {@link android.widget.ImageView}.
*/
public class DrawableImageViewTarget extends ImageViewTarget<Drawable> {
public class DrawableImageViewTarget extends ImageViewTarget<GlideDrawable> {
private static final float SQUARE_RATIO_MARGIN = 0.05f;
private final ImageView view;
private Drawable resource;
private int maxLoopCount;
private GlideDrawable resource;
/**
* Constructor for an {@link com.bumptech.glide.request.target.Target} that can display an
* {@link com.bumptech.glide.load.resource.drawable.GlideDrawable} in an {@link android.widget.ImageView}.
*
* @param view The view to display the drawable in.
*/
public DrawableImageViewTarget(ImageView view) {
this(view, GlideDrawable.LOOP_FOREVER);
}
/**
* Constructor for an {@link com.bumptech.glide.request.target.Target} that can display an
* {@link com.bumptech.glide.load.resource.drawable.GlideDrawable} in an {@link android.widget.ImageView}.
*
* @param view The view to display the drawable in.
* @param maxLoopCount A value to pass to to {@link com.bumptech.glide.load.resource.drawable.GlideDrawable}s
* indicating how many times they should repeat their animation (if they have one). See
* {@link com.bumptech.glide.load.resource.drawable.GlideDrawable#setLoopCount(int)}.
*/
public DrawableImageViewTarget(ImageView view, int maxLoopCount) {
super(view);
this.view = view;
this.maxLoopCount = maxLoopCount;
}
/**
......@@ -30,8 +50,8 @@ public class DrawableImageViewTarget extends ImageViewTarget<Drawable> {
* @param animation {@inheritDoc}
*/
@Override
public void onResourceReady(Drawable resource, GlideAnimation<Drawable> animation) {
if (!(resource instanceof Animatable)) {
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
if (!resource.isAnimated()) {
//TODO: Try to generalize this to other sizes/shapes.
// This is a dirty hack that tries to make loading square thumbnails and then square full images less costly
// by forcing both the smaller thumb and the larger version to have exactly the same intrinsic dimensions.
......@@ -47,9 +67,8 @@ public class DrawableImageViewTarget extends ImageViewTarget<Drawable> {
}
super.onResourceReady(resource, animation);
this.resource = resource;
if (resource instanceof Animatable) {
((Animatable) resource).start();
}
resource.setLoopCount(maxLoopCount);
resource.start();
}
/**
......@@ -59,21 +78,21 @@ public class DrawableImageViewTarget extends ImageViewTarget<Drawable> {
* @param resource The {@link android.graphics.drawable.Drawable} to display in the view.
*/
@Override
protected void setResource(Drawable resource) {
protected void setResource(GlideDrawable resource) {
view.setImageDrawable(resource);
}
@Override
public void onStart() {
if (resource instanceof Animatable) {
((Animatable) resource).start();
if (resource != null) {
resource.start();
}
}
@Override
public void onStop() {
if (resource instanceof Animatable) {
((Animatable) resource).stop();
if (resource != null) {
resource.stop();
}
}
}
......@@ -71,7 +71,7 @@ public abstract class ImageViewTarget<Z> extends ViewTarget<ImageView, Z> implem
}
@Override
public void onResourceReady(Z resource, GlideAnimation<Z> glideAnimation) {
public void onResourceReady(Z resource, GlideAnimation<? super Z> glideAnimation) {
if (glideAnimation == null || !glideAnimation.animate(resource, this)) {
setResource(resource);
}
......
......@@ -7,17 +7,19 @@ import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
/**
* A wrapper drawable to square the wrapped drawable so that it expands to fill a square with exactly the given side
* length. The goal of this drawable is to ensure that square thumbnail drawables always match the size of the view
* they will be displayed in to avoid a costly requestLayout call. This class should not be used with views or drawables
* that are not square.
*/
public class SquaringDrawable extends Drawable {
private final Drawable wrapped;
private int side;
public class SquaringDrawable extends GlideDrawable {
private final GlideDrawable wrapped;
private final int side;
public SquaringDrawable(Drawable wrapped, int side) {
public SquaringDrawable(GlideDrawable wrapped, int side) {
this.wrapped = wrapped;
this.side = side;
}
......@@ -146,4 +148,29 @@ public class SquaringDrawable extends Drawable {
public int getOpacity() {
return wrapped.getOpacity();
}
@Override
public boolean isAnimated() {
return wrapped.isAnimated();
}
@Override
public void setLoopCount(int loopCount) {
wrapped.setLoopCount(loopCount);
}
@Override
public void start() {
wrapped.start();
}
@Override
public void stop() {
wrapped.stop();
}
@Override
public boolean isRunning() {
return wrapped.isRunning();
}
}
......@@ -64,7 +64,7 @@ public interface Target<R> extends LifecycleListener {
*
* @param resource the loaded resource.
*/
public void onResourceReady(R resource, GlideAnimation<R> glideAnimation);
public void onResourceReady(R resource, GlideAnimation<? super R> glideAnimation);
/**
* A lifecycle callback that is called when a load is cancelled and its resources are freed.
......
......@@ -29,6 +29,7 @@ import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.stream.StreamModelLoader;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import com.bumptech.glide.load.resource.bytes.BytesResource;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import com.bumptech.glide.request.Request;
......@@ -392,7 +393,7 @@ public class GlideTest {
private void runTestStringDefaultLoader(String string) {
Glide.with(getContext())
.load(string)
.listener(new RequestListener<String, Drawable>() {
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target target, boolean isFirstResource) {
if (!(e instanceof IOException)) {
......@@ -402,7 +403,7 @@ public class GlideTest {
}
@Override
public boolean onResourceReady(Drawable resource, String model, Target target,
public boolean onResourceReady(GlideDrawable resource, String model, Target target,
boolean isFromMemoryCache,
boolean isFirstResource) {
return false;
......
package com.bumptech.glide.load.resource.transcode;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper;
import com.bumptech.glide.tests.Util;
......@@ -21,7 +21,7 @@ import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class GifBitmapWrapperDrawableTranscoderTest {
private GifBitmapWrapperDrawableTranscoder transcoder;
private ResourceTranscoder<Bitmap, Drawable> bitmapTranscoder;
private ResourceTranscoder<Bitmap, GlideBitmapDrawable> bitmapTranscoder;
@Before
public void setUp() {
......@@ -52,7 +52,7 @@ public class GifBitmapWrapperDrawableTranscoderTest {
private static class TranscoderHarness {
Resource<GifBitmapWrapper> gifBitmapResource = mock(Resource.class);
GifBitmapWrapper gifBitmap = mock(GifBitmapWrapper.class);
Resource<Drawable> expected = mock(Resource.class);
Resource<GlideBitmapDrawable> expected = mock(Resource.class);
public TranscoderHarness() {
when(gifBitmapResource.get()).thenReturn(gifBitmap);
......
package com.bumptech.glide.request.target;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
......@@ -33,7 +31,7 @@ public class DrawableImageViewTargetTest {
public void testSetsDrawableOnViewInSetResource() {
ImageView view = new ImageView(Robolectric.application);
DrawableImageViewTarget target = new DrawableImageViewTarget(view);
Drawable expected = new ColorDrawable(Color.GRAY);
GlideDrawable expected = new MockAnimatedDrawable();
target.setResource(expected);
......@@ -46,7 +44,7 @@ public class DrawableImageViewTargetTest {
when(mockView.getWidth()).thenReturn(100);
when(mockView.getHeight()).thenReturn(100);
DrawableImageViewTarget target = new DrawableImageViewTarget(mockView);
Drawable drawable = new ColorDrawable(Color.RED) {
GlideDrawable drawable = new MockAnimatedDrawable() {
@Override
public int getIntrinsicHeight() {
return 100;
......@@ -78,7 +76,7 @@ public class DrawableImageViewTargetTest {
when(mockView.getWidth()).thenReturn(100);
when(mockView.getHeight()).thenReturn(100);
DrawableImageViewTarget target = new DrawableImageViewTarget(mockView);
Drawable drawable = new AnimationDrawable() {
GlideDrawable drawable = new MockAnimatedDrawable() {
@Override
public int getIntrinsicHeight() {
return 100;
......@@ -108,7 +106,7 @@ public class DrawableImageViewTargetTest {
when(mockView.getWidth()).thenReturn(100);
when(mockView.getHeight()).thenReturn(100);
DrawableImageViewTarget target = new DrawableImageViewTarget(mockView);
Drawable drawable = new ColorDrawable(Color.RED) {
GlideDrawable drawable = new MockAnimatedDrawable() {
@Override
public int getIntrinsicHeight() {
return 100;
......@@ -138,7 +136,7 @@ public class DrawableImageViewTargetTest {
when(mockView.getWidth()).thenReturn(100);
when(mockView.getHeight()).thenReturn(150);
DrawableImageViewTarget target = new DrawableImageViewTarget(mockView);
Drawable drawable = new ColorDrawable(Color.RED) {
GlideDrawable drawable = new MockAnimatedDrawable() {
@Override
public int getIntrinsicHeight() {
return 100;
......@@ -182,6 +180,12 @@ public class DrawableImageViewTargetTest {
assertTrue(drawable.isStarted);
}
@Test
public void testDoesNotStartNullDrawablesOnStart() {
DrawableImageViewTarget target = new DrawableImageViewTarget(new ImageView(Robolectric.application));
target.onStart();
}
@Test
public void testStopsAnimatedDrawablesOnStop() {
MockAnimatedDrawable drawable = new MockAnimatedDrawable();
......@@ -192,8 +196,25 @@ public class DrawableImageViewTargetTest {
assertFalse(drawable.isStarted);
}
private static class MockAnimatedDrawable extends Drawable implements Animatable {
@Test
public void testDoesNotStopNullDrawablesOnStop() {
DrawableImageViewTarget target = new DrawableImageViewTarget(new ImageView(Robolectric.application));
target.onStop();
}
@Test
public void testSetsLoopCountOnDrawable() {
int maxLoopCount = 6;
MockAnimatedDrawable drawable = new MockAnimatedDrawable();
DrawableImageViewTarget target = new DrawableImageViewTarget(new ImageView(Robolectric.application),
maxLoopCount);
target.onResourceReady(drawable, null);
assertEquals(maxLoopCount, drawable.loopCount);
}
private static class MockAnimatedDrawable extends GlideDrawable {
private boolean isStarted;
private int loopCount;
@Override
public void start() {
......@@ -229,5 +250,15 @@ public class DrawableImageViewTargetTest {
public int getOpacity() {
return 0;
}
@Override
public boolean isAnimated() {
return false;
}
@Override
public void setLoopCount(int loopCount) {
this.loopCount = loopCount;
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册