提交 896af010 编写于 作者: S Sam Judd

Handle videos and images in one request.

上级 fcd787c9
......@@ -11,32 +11,54 @@ import com.bumptech.glide.resize.load.Transformation;
/**
* A base {@link BitmapLoadFactory} that composes {@link ModelLoader} and {@link BitmapDecoder} sub-components
* to create an {@link BitmapLoadTask}.
* to create an {@link BitmapLoadTask} capable of loading a model that represents either an image or a video.
*
* @param <T> The type of the model.
* @param <Y> The type of the resource that the {@link ModelLoader} provides and the {@link BitmapDecoder} can
* @param <Y> The type of the resource that the image {@link ModelLoader} provides and the image {@link BitmapDecoder} can
* decode.`
* @param <Z> The type of resource that the video {@link ModelLoader} provides and the video {@link BitmapDecoder} can
* decode.
*/
public class BaseBitmapLoadFactory<T, Y> implements BitmapLoadFactory<T> {
private final ModelLoader<T, Y> modelLoader;
private final BitmapDecoder<Y> decoder;
public class BaseBitmapLoadFactory<T, Y, Z> implements BitmapLoadFactory<T> {
private final ModelLoader<T, Y> imageModelLoader;
private final BitmapDecoder<Y> imageDecoder;
private final ModelLoader<T, Z> videoModelLoader;
private final BitmapDecoder<Z> videoDecoder;
private final TransformationLoader<T> transformationLoader;
public BaseBitmapLoadFactory(ModelLoader<T, Y> modelLoader, BitmapDecoder<Y> decoder) {
this(modelLoader, decoder, new None<T>());
@SuppressWarnings("unused")
public BaseBitmapLoadFactory(ModelLoader<T, Y> imageModelLoader, BitmapDecoder<Y> imageDecoder) {
this(imageModelLoader, imageDecoder, null, null, new None<T>());
}
public BaseBitmapLoadFactory(ModelLoader<T, Y> modelLoader, BitmapDecoder<Y> decoder,
@SuppressWarnings("unused")
public BaseBitmapLoadFactory(ModelLoader<T, Y> imageModelLoader, BitmapDecoder<Y> imageDecoder,
TransformationLoader<T> transformationLoader) {
this.modelLoader = modelLoader;
this.decoder = decoder;
this(imageModelLoader, imageDecoder, null, null, transformationLoader);
}
public BaseBitmapLoadFactory(ModelLoader<T, Y> imageModelLoader, BitmapDecoder<Y> imageDecoder,
ModelLoader<T, Z> videoModelLoader, BitmapDecoder<Z> videoDecoder,
TransformationLoader<T> transformationLoader) {
this.imageModelLoader = imageModelLoader;
this.imageDecoder = imageDecoder;
this.videoModelLoader = videoModelLoader;
this.videoDecoder = videoDecoder;
this.transformationLoader = transformationLoader;
}
@Override
public BitmapLoadTask getLoadTask(T model, int width, int height) {
ResourceFetcher<Y> resourceFetcher = modelLoader.getResourceFetcher(model, width, height);
ResourceFetcher<Y> imageFetcher = null;
if (imageModelLoader != null) {
imageFetcher = imageModelLoader.getResourceFetcher(model, width, height);
}
ResourceFetcher<Z> videoFetcher = null;
if (videoModelLoader != null) {
videoFetcher = videoModelLoader.getResourceFetcher(model, width, height);
}
Transformation transformation = transformationLoader.getTransformation(model);
return new BaseBitmapLoadTask<Y>(resourceFetcher, decoder, transformation, width, height);
return new BaseBitmapLoadTask<Y, Z>(imageFetcher, imageDecoder, videoFetcher, videoDecoder, transformation,
width, height);
}
}
......@@ -39,10 +39,7 @@ public class GenericLoaderFactory {
public <T, Y> ModelLoader<T, Y> buildModelLoader(Class<T> modelClass, Class<Y> resourceClass, Context context) {
final ModelLoaderFactory<T, Y> factory = getFactory(modelClass, resourceClass);
if (factory == null) {
throw new IllegalArgumentException("No ModelLoaderFactory registered for class=" + modelClass);
}
return factory.build(context, this);
return factory != null ? factory.build(context, this) : null;
}
@SuppressWarnings("unchecked")
......
......@@ -24,7 +24,7 @@ public abstract class UriLoader<T> implements ModelLoader<Uri, T>{
ResourceFetcher<T> result = null;
if (isLocalUri(scheme)) {
result = getLocalUriFetcher(context, model);
} else if ("http".equals(scheme) || "https".equals(scheme)) {
} else if (urlLoader != null && ("http".equals(scheme) || "https".equals(scheme))) {
try {
result = urlLoader.getResourceFetcher(new URL(model.toString()), width, height);
} catch (MalformedURLException e) {
......@@ -32,10 +32,6 @@ public abstract class UriLoader<T> implements ModelLoader<Uri, T>{
}
}
if (result == null) {
throw new IllegalArgumentException("No stream loader for uri=" + model);
}
return result;
}
......
......@@ -8,40 +8,47 @@ import com.bumptech.glide.resize.load.Transformation;
/**
* A base {@link BitmapLoadTask} that composes {@link ResourceFetcher} and {@link BitmapDecoder} to decode a
* bitmap.
* bitmap from either an image or a video.
*
* @param <T> The type of the resource returned by the {@link ResourceFetcher} and used by the
* {@link BitmapDecoder} to BitmapResourceLoader the bitmap.
* @param <T> The type of the resource returned by the image {@link ResourceFetcher} and used by the
* image {@link BitmapDecoder} to decode the bitmap.
* @param <Y> The type of the resource returned by the video {@link ResourceFetcher} and used by the
* video {@link BitmapDecoder} to decode the bitmap.
*/
public class BaseBitmapLoadTask<T> implements BitmapLoadTask {
public class BaseBitmapLoadTask<T, Y> implements BitmapLoadTask {
private final String id;
private final int width;
private final int height;
private final ResourceFetcher<Y> videoLoader;
private final BitmapDecoder<Y> videoDecoder;
private final Transformation transformation;
private ResourceFetcher<T> loader;
private BitmapDecoder<T> decoder;
private ResourceFetcher<T> imageLoader;
private BitmapDecoder<T> imageDecoder;
public BaseBitmapLoadTask(ResourceFetcher<T> loader, BitmapDecoder<T> decoder, Transformation transformation,
public BaseBitmapLoadTask(ResourceFetcher<T> imageLoader, BitmapDecoder<T> imageDecoder,
ResourceFetcher<Y> videoLoader, BitmapDecoder<Y> videoDecoder, Transformation transformation,
int width, int height) {
this.loader = loader;
this.decoder = decoder;
this.imageLoader = imageLoader;
this.imageDecoder = imageDecoder;
this.videoLoader = videoLoader;
this.videoDecoder = videoDecoder;
this.transformation = transformation;
this.width = width;
this.height = height;
this.id = loader.getId() + decoder.getId() + transformation.getId() + width + height;
this.id = imageLoader.getId() + imageDecoder.getId() + transformation.getId() + width + height;
}
public void cancel() {
loader.cancel();
imageLoader.cancel();
}
@Override
public Bitmap load(BitmapPool bitmapPool) throws Exception {
T resource = loader.loadResource();
if (resource == null) {
throw new IllegalStateException("Cannot decode null resource");
Bitmap original = loadOriginal(bitmapPool);
if (original == null) {
return null;
}
Bitmap original = decoder.decode(resource, bitmapPool, width, height);
Bitmap transformed = transformation.transform(original, bitmapPool, width, height);
if (original != transformed) {
bitmapPool.put(original);
......@@ -49,6 +56,30 @@ public class BaseBitmapLoadTask<T> implements BitmapLoadTask {
return transformed;
}
private Bitmap loadOriginal(BitmapPool bitmapPool) throws Exception {
Bitmap result = loadOriginal(imageLoader, imageDecoder, bitmapPool);
if (result == null) {
result = loadOriginal(videoLoader, videoDecoder, bitmapPool);
}
return result;
}
private <X> Bitmap loadOriginal(ResourceFetcher<X> fetcher, BitmapDecoder<X> decoder, BitmapPool bitmapPool)
throws Exception {
if (fetcher == null || decoder == null) {
return null;
}
X resource = fetcher.loadResource();
if (resource == null) {
return null;
}
return decoder.decode(resource, bitmapPool, width, height);
}
public String getId() {
return id;
}
......
......@@ -5,7 +5,6 @@ import android.net.Uri;
import android.test.ActivityTestCase;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.loader.bitmap.model.ModelLoader;
import com.bumptech.glide.loader.bitmap.model.stream.StreamModelLoader;
import com.bumptech.glide.loader.bitmap.resource.ResourceFetcher;
import com.bumptech.glide.presenter.ImagePresenter;
......
......@@ -3,7 +3,6 @@ package com.bumptech.glide;
import android.content.Context;
import android.graphics.Bitmap;
import android.test.AndroidTestCase;
import com.bumptech.glide.loader.bitmap.model.ModelLoader;
import com.bumptech.glide.loader.bitmap.model.stream.StreamModelLoader;
import com.bumptech.glide.loader.bitmap.resource.ResourceFetcher;
......
package com.bumptech.glide.samples.flickr;
import android.content.Context;
import com.bumptech.glide.loader.bitmap.model.BaseUrlLoader;
import com.bumptech.glide.loader.bitmap.model.Cache;
import com.bumptech.glide.loader.bitmap.model.stream.BaseUrlLoader;
import com.bumptech.glide.samples.flickr.api.Api;
import com.bumptech.glide.samples.flickr.api.Photo;
......
......@@ -139,7 +139,7 @@ public class FlickrPhotoGrid extends SherlockFragment implements PhotoViewer {
//reason why ImagePresenter is used here and not in FlickrPhotoList.
final Animation fadeIn = AnimationUtils.loadAnimation(context, R.anim.fade_in);
imagePresenter = new ImagePresenter.Builder<Photo>()
.setBitmapLoadFactory(new BaseBitmapLoadFactory<Photo, InputStream>(
.setBitmapLoadFactory(new BaseBitmapLoadFactory<Photo, InputStream, Void>(
new FlickrModelLoader(context, urlCache), Downsampler.AT_LEAST,
new CenterCrop<Photo>()))
.setImageView(imageView)
......
......@@ -31,6 +31,7 @@ import com.bumptech.glide.volley.VolleyUrlLoader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -112,8 +113,7 @@ public class FlickrSearchActivity extends SherlockFragmentActivity {
}
requestQueue = Volley.newRequestQueue(this);
glide.register(URL.class, new VolleyUrlLoader.Factory(requestQueue));
glide.register(URL.class, InputStream.class, new VolleyUrlLoader.Factory(requestQueue));
searching = findViewById(R.id.searching);
searchLoading = findViewById(R.id.search_loading);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册