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

Add an API for preloading resources.

上级 accb2312
package com.bumptech.glide.request.target;
import com.bumptech.glide.request.Request;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@RunWith(RobolectricTestRunner.class)
public class PreloadTargetTest {
@Test
public void testCallsSizeReadyWithGivenDimensions() {
int width = 1234;
int height = 456;
PreloadTarget<Object> target = PreloadTarget.obtain(width, height);
SizeReadyCallback cb = mock(SizeReadyCallback.class);
target.getSize(cb);
verify(cb).onSizeReady(eq(width), eq(height));
}
@Test
public void testClearsTargetInOnResourceReady() {
Request request = mock(Request.class);
PreloadTarget<Object> target = PreloadTarget.obtain(100, 100);
target.setRequest(request);
target.onResourceReady(new Object(), null);
verify(request).clear();
}
}
\ No newline at end of file
......@@ -28,6 +28,7 @@ import com.bumptech.glide.request.animation.GlideAnimationFactory;
import com.bumptech.glide.request.animation.NoAnimation;
import com.bumptech.glide.request.animation.ViewAnimation;
import com.bumptech.glide.request.animation.ViewPropertyAnimation;
import com.bumptech.glide.request.target.PreloadTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.util.Util;
......@@ -598,6 +599,21 @@ public class GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeT
return target;
}
/**
* Preloads the resource into the cache using the given width and height.
*
* <p>
* Pre-loading is useful for making sure that resources you are going to to want in the near future are
* available quickly.
* </p>
*
* @see com.bumptech.glide.ListPreloader
*/
public Target<TranscodeType> preload(int width, int height) {
final PreloadTarget<TranscodeType> target = PreloadTarget.obtain(width, height);
return into(target);
}
void applyCenterCrop() {
// To be implemented by subclasses when possible.
}
......
package com.bumptech.glide.request.target;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
/**
* A one time use {@link com.bumptech.glide.request.target.Target} class that loads a resource into memory and then
* clears itself.
*
* @param <Z> The type of resource that will be loaded into memory.
*/
public final class PreloadTarget<Z> extends SimpleTarget<Z> {
/**
* Returns a PreloadTarget.
*
* @param width The width of the desired resourece.
* @param height The height of the desired resource.
* @param <Z> The type of the desired resource.
*/
public static <Z> PreloadTarget<Z> obtain(int width, int height) {
return new PreloadTarget<Z>(width, height);
}
private PreloadTarget(int width, int height) {
super(width, height);
}
@Override
public void onResourceReady(Z resource, GlideAnimation<? super Z> glideAnimation) {
Glide.clear(this);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册