提交 b46fae74 编写于 作者: S Sam Judd

Allow overriding target sizes.

上级 53166742
......@@ -64,6 +64,8 @@ public class GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeT
private boolean isCacheable = true;
private ResourceEncoder<ResourceType> preSkipEncoder;
private GlideAnimationFactory<TranscodeType> animationFactory = NoAnimation.getFactory();
private int overrideHeight = -1;
private int overrideWidth = -1;
GenericRequestBuilder(Context context, ModelType model,
LoadProvider<ModelType, DataType, ResourceType, TranscodeType> loadProvider,
......@@ -421,6 +423,28 @@ public class GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeT
return this;
}
/**
* Overrides the {@link Target}'s width and height with the given values. This is useful almost exclusively for
* thumbnails, and should only be used when you both need a very specific sized image and when it is impossible or
* impractical to return that size from {@link Target#getSize(Target.SizeReadyCallback)}.
*
* @param width The width to use to load the resource.
* @param height The height to use to load the resource.
* @return This RequestBuilder.
*/
public GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> override(int width, int height) {
if (width <= 0) {
throw new IllegalArgumentException("Width must be >= 0");
}
if (height <= 0) {
throw new IllegalArgumentException("Height must be >= 0");
}
this.overrideWidth = width;
this.overrideHeight = height;
return this;
}
/**
* Set the target the image will be loaded into.
*
......@@ -539,7 +563,9 @@ public class GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeT
getFinalTransformation(),
transcodeClass,
isCacheable,
animationFactory);
animationFactory,
overrideWidth,
overrideHeight);
}
@SuppressWarnings("unchecked")
......
......@@ -19,7 +19,7 @@ public class Engine implements EngineJobListener, MemoryCache.ResourceRemovedLis
private final Map<Key, ResourceRunner> runners;
private final ResourceRunnerFactory factory;
private KeyFactory keyFactory;
private final KeyFactory keyFactory;
private final MemoryCache cache;
public static class LoadStatus {
......
......@@ -42,6 +42,8 @@ public class GenericRequest<A, T, Z, R> implements Request, Target.SizeReadyCall
private final float sizeMultiplier;
private final Engine engine;
private final GlideAnimationFactory<R> animationFactory;
private final int overrideWidth;
private final int overrideHeight;
private Drawable placeholderDrawable;
private Drawable errorDrawable;
......@@ -69,7 +71,9 @@ public class GenericRequest<A, T, Z, R> implements Request, Target.SizeReadyCall
Transformation<Z> transformation,
Class<R> transcodeClass,
boolean isMemoryCacheable,
GlideAnimationFactory<R> animationFactory) {
GlideAnimationFactory<R> animationFactory,
int overrideWidth,
int overrideHeight) {
this.loadProvider = loadProvider;
this.model = model;
this.context = context;
......@@ -87,6 +91,8 @@ public class GenericRequest<A, T, Z, R> implements Request, Target.SizeReadyCall
this.transcodeClass = transcodeClass;
this.isMemoryCacheable = isMemoryCacheable;
this.animationFactory = animationFactory;
this.overrideWidth = overrideWidth;
this.overrideHeight = overrideHeight;
// We allow null models by just setting an error drawable. Null models will always have empty providers, we
// simply skip our sanity checks in that unusual case.
......@@ -117,7 +123,11 @@ public class GenericRequest<A, T, Z, R> implements Request, Target.SizeReadyCall
return;
}
target.getSize(this);
if (overrideWidth > 0 && overrideHeight > 0) {
onSizeReady(overrideWidth, overrideHeight);
} else {
target.getSize(this);
}
if (!isComplete() && !isFailed()) {
setPlaceHolder();
......
......@@ -46,6 +46,26 @@ public class GenericRequestBuilderTest {
getNullModelRequest().animate((GlideAnimationFactory) null);
}
@Test(expected = IllegalArgumentException.class)
public void testThrowsWhenOverrideWidthLessThanZero() {
getNullModelRequest().override(-1, 100);
}
@Test(expected = IllegalArgumentException.class)
public void testThrowsWhenOverrideWidthEqualToZero() {
getNullModelRequest().override(0, 100);
}
@Test(expected = IllegalArgumentException.class)
public void testThrowsWhenOverrideHeightLessThanZero() {
getNullModelRequest().override(100, -5);
}
@Test(expected = IllegalArgumentException.class)
public void testThrowsWhenOverrideHeightEqualToZero() {
getNullModelRequest().override(100, 0);
}
@Test
public void testDoesNotThrowWhenModelAndLoaderNull() {
new GenericRequestBuilder(Robolectric.application, null, null, Object.class, mock(Glide.class),
......
......@@ -67,6 +67,8 @@ public class GenericRequestTest {
RequestListener<Object, Object> requestListener = mock(RequestListener.class);
boolean skipMemoryCache;
GlideAnimationFactory<Object> factory = mock(GlideAnimationFactory.class);
int overrideWidth = -1;
int overrideHeight = -1;
public RequestHarness() {
ModelLoader<Object, Object> modelLoader = mock(ModelLoader.class);
......@@ -83,9 +85,25 @@ public class GenericRequestTest {
}
public GenericRequest<Object, Object, Object, Object> getRequest() {
return new GenericRequest<Object, Object, Object, Object>(loadProvider, model, context, priority, target,
1f, placeholderDrawable, placeholderResourceId, errorDrawable, errorResourceId, requestListener,
requestCoordinator, engine, mock(Transformation.class), Object.class, skipMemoryCache, factory);
return new GenericRequest<Object, Object, Object, Object>(loadProvider,
model,
context,
priority,
target,
1f,
placeholderDrawable,
placeholderResourceId,
errorDrawable,
errorResourceId,
requestListener,
requestCoordinator,
engine,
mock(Transformation.class),
Object.class,
skipMemoryCache,
factory,
overrideWidth,
overrideHeight);
}
}
......@@ -523,6 +541,49 @@ public class GenericRequestTest {
verify(harness.target).onResourceReady(eq(harness.resource.get()), eq(glideAnimation));
}
@Test
public void testCallsGetSizeIfOverrideWidthIsLessThanZero() {
harness.overrideWidth = -1;
harness.overrideHeight = 100;
GenericRequest<Object, Object, Object, Object> request = harness.getRequest();
request.run();
verify(harness.target).getSize(any(Target.SizeReadyCallback.class));
}
@Test
public void testCallsGetSizeIfOverrideHeightIsLessThanZero() {
harness.overrideHeight = -1;
harness.overrideWidth = 100;
GenericRequest<Object, Object, Object, Object> request = harness.getRequest();
request.run();
verify(harness.target).getSize(any(Target.SizeReadyCallback.class));
}
@Test
public void testDoesNotCallGetSizeIfOverrideWidthAndHeightAreSet() {
harness.overrideWidth = 100;
harness.overrideHeight = 100;
GenericRequest<Object, Object, Object, Object> request = harness.getRequest();
request.run();
verify(harness.target, never()).getSize(any(Target.SizeReadyCallback.class));
}
@Test
public void testCallsEngingWithOverrideWidthAndHeightIfSet() {
harness.overrideWidth = 1;
harness.overrideHeight = 2;
GenericRequest<Object, Object, Object, Object> request = harness.getRequest();
request.run();
verify(harness.engine).load(anyString(), eq(harness.overrideWidth), eq(harness.overrideHeight),
any(ResourceDecoder.class), any(DataFetcher.class), any(ResourceDecoder.class),
any(Transformation.class), any(ResourceEncoder.class), any(ResourceTranscoder.class),
any(Priority.class), anyBoolean(), any(ResourceCallback.class));
}
private Context mockContextToReturn(int resourceId, Drawable drawable) {
Resources resources = mock(Resources.class);
Context context = mock(Context.class);
......
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:scaleType="center"
android:scaleType="centerCrop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
......@@ -12,6 +12,7 @@ import com.actionbarsherlock.app.SherlockFragment;
import com.bumptech.glide.GenericRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.ListPreloader;
import com.bumptech.glide.samples.flickr.api.Api;
import com.bumptech.glide.samples.flickr.api.Photo;
import java.util.ArrayList;
......@@ -98,8 +99,11 @@ public class FlickrPhotoGrid extends SherlockFragment implements PhotoViewer {
@Override
protected GenericRequestBuilder getRequestBuilder(Photo item) {
return Glide.with(context)
return Glide.with(FlickrPhotoGrid.this)
.loadFromImage(item)
.thumbnail(Glide.with(FlickrPhotoGrid.this)
.loadFromImage(item)
.override(Api.THUMB_SIZE, Api.THUMB_SIZE))
.centerCrop();
}
}
......@@ -147,12 +151,14 @@ public class FlickrPhotoGrid extends SherlockFragment implements PhotoViewer {
Glide.with(FlickrPhotoGrid.this)
.loadFromImage(current)
.thumbnail(Glide.with(FlickrPhotoGrid.this)
.loadFromImage(current)
.override(Api.THUMB_SIZE, Api.THUMB_SIZE))
.animate(R.anim.fade_in)
.centerCrop()
.into(imageView);
return imageView;
}
}
}
......@@ -15,6 +15,7 @@ import com.actionbarsherlock.app.SherlockFragment;
import com.bumptech.glide.GenericRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.ListPreloader;
import com.bumptech.glide.samples.flickr.api.Api;
import com.bumptech.glide.samples.flickr.api.Photo;
import java.util.ArrayList;
......@@ -114,9 +115,11 @@ public class FlickrPhotoList extends SherlockFragment implements PhotoViewer {
@Override
protected GenericRequestBuilder getRequestBuilder(Photo item) {
return Glide.with(context)
return Glide.with(FlickrPhotoList.this)
.loadFromImage(item)
.thumbnail(0.1f)
.thumbnail(Glide.with(FlickrPhotoList.this)
.loadFromImage(item)
.override(Api.THUMB_SIZE, Api.THUMB_SIZE))
.centerCrop();
}
}
......@@ -174,7 +177,9 @@ public class FlickrPhotoList extends SherlockFragment implements PhotoViewer {
Glide.with(FlickrPhotoList.this)
.loadFromImage(current)
.placeholder(new ColorDrawable(Color.GRAY))
.thumbnail(0.1f)
.thumbnail(Glide.with(FlickrPhotoList.this)
.loadFromImage(current)
.override(Api.THUMB_SIZE, Api.THUMB_SIZE))
.centerCrop()
.crossFade(R.anim.fade_in, 150)
.into(viewHolder.imageView);
......
......@@ -29,6 +29,7 @@ public class Api {
//incomplete size independent url for photos that can be cached per photo
private static final String CACHEABLE_PHOTO_URL = "http://farm%s.staticflickr.com/%s/%s_%s_";
private static final Map<Integer, String> EDGE_TO_SIZE_KEY = new HashMap<Integer, String>() {{
put(75, "s");
put(100, "t");
......@@ -44,6 +45,8 @@ public class Api {
Collections.sort(SORTED_SIZE_KEYS);
}
public static final int THUMB_SIZE = SORTED_SIZE_KEYS.get(0);
private static String getSizeKey(int width, int height) {
final int largestEdge = Math.max(width, height);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册