From 7ace7e959b5d44fde04526eed4f5e0abb5a33980 Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Sun, 12 Oct 2014 13:32:55 -0700 Subject: [PATCH] Add from() api to allow builders to be re-used. --- .../bumptech/glide/GenericRequestBuilder.java | 1 - .../main/java/com/bumptech/glide/Glide.java | 14 +- .../com/bumptech/glide/RequestManager.java | 290 +++++++++++++----- .../glide/samples/flickr/FlickrPhotoGrid.java | 46 +-- .../glide/samples/flickr/FlickrPhotoList.java | 43 ++- .../glide/samples/giphy/MainActivity.java | 26 +- .../bumptech/svgsample/app/MainActivity.java | 52 ++-- 7 files changed, 317 insertions(+), 155 deletions(-) diff --git a/library/src/main/java/com/bumptech/glide/GenericRequestBuilder.java b/library/src/main/java/com/bumptech/glide/GenericRequestBuilder.java index 12da07eb6..802b78425 100644 --- a/library/src/main/java/com/bumptech/glide/GenericRequestBuilder.java +++ b/library/src/main/java/com/bumptech/glide/GenericRequestBuilder.java @@ -559,7 +559,6 @@ public class GenericRequestBuilder ModelLoader buildModelLoader(Class modelClass, Class resourceClass, Context context) { + if (modelClass == null) { + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "Unable to load null model, setting placeholder only"); + } + return null; + } return Glide.get(context).getLoaderFactory().buildModelLoader(modelClass, resourceClass, context); } @@ -492,13 +498,7 @@ public class Glide { */ @SuppressWarnings("unchecked") public static ModelLoader buildModelLoader(T model, Class resourceClass, Context context) { - if (model == null) { - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Unable to load null model, setting placeholder only"); - } - return null; - } - return buildModelLoader((Class) model.getClass(), resourceClass, context); + return buildModelLoader(model != null ? (Class) model.getClass() : null, resourceClass, context); } /** diff --git a/library/src/main/java/com/bumptech/glide/RequestManager.java b/library/src/main/java/com/bumptech/glide/RequestManager.java index 80853404a..17a3f2803 100644 --- a/library/src/main/java/com/bumptech/glide/RequestManager.java +++ b/library/src/main/java/com/bumptech/glide/RequestManager.java @@ -220,9 +220,21 @@ public class RequestManager implements LifecycleListener { } /** - * Returns a request builder to load the data represented by the given {@link String} using an empty + * Returns a request builder to load the given {@link java.lang.String}. * signature. * + * @see #fromString() + * @see #load(Object) + * + * @param string A file path, or a uri or url handled by {@link com.bumptech.glide.load.model.UriLoader}. + */ + public DrawableTypeRequest load(String string) { + return (DrawableTypeRequest) fromString().load(string); + } + + /** + * Returns a request builder that loads data from {@link String}s using an empty signature. + * *

* Note - this method caches data using only the given String as the cache key. If the data is a Uri outside of * your control, or you otherwise expect the data represented by the given String to change without the String @@ -233,16 +245,28 @@ public class RequestManager implements LifecycleListener { * {@link com.bumptech.glide.DrawableRequestBuilder#skipMemoryCache(boolean)} may be appropriate. *

* - * @see com.bumptech.glide.GenericRequestBuilder#signature(com.bumptech.glide.load.Key) + * @see #from(Class) + * @see #load(String) + */ + public DrawableTypeRequest fromString() { + return loadGeneric(String.class); + } + + /** + * Returns a request builder to load the given {@link Uri}. * - * @param string A file path, or a uri or url handled by {@link com.bumptech.glide.load.model.UriLoader}. + * @see #fromUri() + * @see #load(Object) + * + * @param uri The Uri representing the image. Must be of a type handled by + * {@link com.bumptech.glide.load.model.UriLoader}. */ - public DrawableTypeRequest load(String string) { - return loadGeneric(string); + public DrawableTypeRequest load(Uri uri) { + return (DrawableTypeRequest) fromUri().load(uri); } /** - * Returns a request builder to load the data represented by the given {@link Uri} using an empty signature. + * Returns a request builder to load data from {@link android.net.Uri}s using no signature. * *

* Note - this method caches data at Uris using only the Uri itself as the cache key. The data represented by @@ -254,16 +278,13 @@ public class RequestManager implements LifecycleListener { * {@link com.bumptech.glide.DrawableRequestBuilder#skipMemoryCache(boolean)} may be appropriate. *

* - * @see #using(com.bumptech.glide.load.model.stream.StreamModelLoader) + * @see #from(Class) * @see #loadFromMediaStore(android.net.Uri) * @see #loadFromMediaStore(android.net.Uri, String, long, int) * @see com.bumptech.glide.GenericRequestBuilder#signature(com.bumptech.glide.load.Key) - * - * @param uri The Uri representing the image. Must be of a type handled by - * {@link com.bumptech.glide.load.model.UriLoader}. */ - public DrawableTypeRequest load(Uri uri) { - return loadGeneric(uri); + public DrawableTypeRequest fromUri() { + return loadGeneric(Uri.class); } /** @@ -288,13 +309,25 @@ public class RequestManager implements LifecycleListener { */ public DrawableTypeRequest loadFromMediaStore(Uri uri, String mimeType, long dateModified, int orientation) { Key signature = new MediaStoreSignature(mimeType, dateModified, orientation); - return (DrawableTypeRequest) loadFromMediaStore(uri) - .signature(signature); + return (DrawableTypeRequest) loadFromMediaStore(uri).signature(signature); + } + + /** + * Returns a request builder to load the given media store {@link android.net.Uri}. + * + * @see #fromMediaStore() + * @see #load(Object) + * + * @param uri The uri representing the media. + */ + public DrawableTypeRequest loadFromMediaStore(Uri uri) { + return (DrawableTypeRequest) fromMediaStore().load(uri); } /** * Returns a request builder that uses {@link android.provider.MediaStore.Images.Thumbnails} and - * {@link android.provider.MediaStore.Video.Thumbnails} to retrieve pre-generated thumbnails for the given uri. + * {@link android.provider.MediaStore.Video.Thumbnails} to retrieve pre-generated thumbnails for + * {@link android.net.Uri}s. * *

* Falls back to the registered {@link com.bumptech.glide.load.model.ModelLoaderFactory} registered for @@ -314,29 +347,38 @@ public class RequestManager implements LifecycleListener { * {@link com.bumptech.glide.GenericRequestBuilder#skipMemoryCache(boolean)}. *

* + * @see #from(Class) * @see #loadFromMediaStore(android.net.Uri, String, long, int) * @see #load(android.net.Uri) - * @see com.bumptech.glide.GenericRequestBuilder#signature(com.bumptech.glide.load.Key) * @see com.bumptech.glide.signature.MediaStoreSignature - * - * @param uri The uri representing the media. */ - public DrawableTypeRequest loadFromMediaStore(Uri uri) { - ModelLoader genericStreamLoader = Glide.buildStreamModelLoader(uri, context); + public DrawableTypeRequest fromMediaStore() { + ModelLoader genericStreamLoader = Glide.buildStreamModelLoader(Uri.class, context); ModelLoader mediaStoreLoader = new MediaStoreStreamLoader(context, genericStreamLoader); - ModelLoader fileDescriptorModelLoader = Glide.buildFileDescriptorModelLoader(uri, - context); + ModelLoader fileDescriptorModelLoader = + Glide.buildFileDescriptorModelLoader(Uri.class, context); + + return optionsApplier.apply(new DrawableTypeRequest(Uri.class, mediaStoreLoader, + fileDescriptorModelLoader, context, glide, requestTracker, lifecycle, optionsApplier)); + } - return (DrawableTypeRequest) optionsApplier.apply(new DrawableTypeRequest(Uri.class, mediaStoreLoader, - fileDescriptorModelLoader, context, glide, requestTracker, lifecycle, optionsApplier)) - .load(uri); + /** + * Returns a request builder to load the given {@link File}. + * + * @see #fromFile() + * @see #load(Object) + * + * @param file The File containing the image + */ + public DrawableTypeRequest load(File file) { + return (DrawableTypeRequest) fromFile().load(file); } /** * Returns a request builder that uses the {@link com.bumptech.glide.load.model.ModelLoaderFactory} currently * registered for {@link File} to load the image represented by the given {@link File}. Defaults to * {@link com.bumptech.glide.load.model.stream.StreamFileLoader.Factory} and - * {@link com.bumptech.glide.load.model.stream.StreamFileLoader} to load the given model. + * {@link com.bumptech.glide.load.model.stream.StreamFileLoader} to load images from {@link File}s. * *

* Note - this method caches data for Files using only the file path itself as the cache key. The data in the @@ -349,19 +391,29 @@ public class RequestManager implements LifecycleListener { *

* * @see #load(java.io.File) - * @see #using(StreamModelLoader) + * @see #from(Class) + */ + public DrawableTypeRequest fromFile() { + return loadGeneric(File.class); + } + + /** + * Returns a request builder to load the given resource id. * - * @param file The File containing the image + * @see #fromResource() + * @see #load(Object) + * + * @param resourceId the id of the resource containing the image */ - public DrawableTypeRequest load(File file) { - return loadGeneric(file); + public DrawableTypeRequest load(Integer resourceId) { + return (DrawableTypeRequest) fromResource().load(resourceId); } /** * Returns a request builder that uses the {@link com.bumptech.glide.load.model.ModelLoaderFactory} currently * registered for {@link Integer} to load the image represented by the given {@link Integer} resource id. Defaults * to {@link com.bumptech.glide.load.model.stream.StreamResourceLoader.Factory} and - * {@link com.bumptech.glide.load.model.stream.StreamResourceLoader} to load the given model. + * {@link com.bumptech.glide.load.model.stream.StreamResourceLoader} to load resource id models. * *

* By default this method adds a version code based signature to the cache key used to cache this resource in @@ -374,34 +426,48 @@ public class RequestManager implements LifecycleListener { * {@link com.bumptech.glide.load.engine.DiskCacheStrategy#RESULT} for release builds. *

* + * @see #from(Class) * @see #load(Integer) - * @see #using(StreamModelLoader) * @see com.bumptech.glide.signature.ApplicationVersionSignature * @see com.bumptech.glide.GenericRequestBuilder#signature(com.bumptech.glide.load.Key) - * - * @param resourceId the id of the resource containing the image */ - public DrawableTypeRequest load(Integer resourceId) { - return (DrawableTypeRequest) loadGeneric(resourceId) + public DrawableTypeRequest fromResource() { + return (DrawableTypeRequest) loadGeneric(Integer.class) .signature(ApplicationVersionSignature.obtain(context)); } + /** + * Returns a request builder to load the given {@link URL}. + * + * @see #fromUrl() + * @see #load(Object) + * + * @deprecated The {@link java.net.URL} class has + * a number of performance problems and should generally be avoided when + * possible. Prefer {@link #load(android.net.Uri)} or {@link #load(String)}. + * @param url The URL representing the image. + */ + @Deprecated + public DrawableTypeRequest load(URL url) { + return (DrawableTypeRequest) fromUrl().load(url); + } + /** * Returns a request builder that uses the {@link com.bumptech.glide.load.model.ModelLoaderFactory} currently * registered for {@link URL} to load the image represented by the given {@link URL}. Defaults to * {@link com.bumptech.glide.load.model.stream.HttpUrlGlideUrlLoader} and - * {@link com.bumptech.glide.load.data.HttpUrlFetcher} to load the given model. + * {@link com.bumptech.glide.load.data.HttpUrlFetcher} to load {@link java.net.URL} models. * - * @see #using(StreamModelLoader) + * @see #from(Class) + * @see #load(java.net.URL) * * @deprecated The {@link java.net.URL} class has * a number of performance problems and should generally be avoided when * possible. Prefer {@link #load(android.net.Uri)} or {@link #load(String)}. - * @param url The URL representing the image. */ @Deprecated - public DrawableTypeRequest load(URL url) { - return loadGeneric(url); + public DrawableTypeRequest fromUrl() { + return loadGeneric(URL.class); } /** @@ -409,7 +475,7 @@ public class RequestManager implements LifecycleListener { * * *

- * Note - This method does not cache results in either the disk cache or the memory cache. + * Note - by default loads for bytes are not cached in either the memory or the disk cache. *

* * @see #load(byte[]) @@ -419,29 +485,37 @@ public class RequestManager implements LifecycleListener { * @param model The data to load. * @param id A unique id that identifies the image represented by the model suitable for use as a cache key * (url, filepath etc). If there is no suitable id, use {@link #load(byte[])} instead. - * @return A {@link DrawableTypeRequest} to set options for the load and ultimately the target to load the image - * into. */ @Deprecated public DrawableTypeRequest load(byte[] model, final String id) { - return (DrawableTypeRequest) loadGeneric(model) - .signature(new StringSignature(id)) - .diskCacheStrategy(DiskCacheStrategy.NONE) - .skipMemoryCache(true /*skipMemoryCache*/); + return (DrawableTypeRequest) load(model).signature(new StringSignature(id)); } /** - * Returns a request builder that uses {@link StreamByteArrayLoader} to load an image from the given byte array. - * Suitable when there is no simple id that represents the given data. + * Returns a request to load the given byte array. * - * @see #load(byte[], String) + * @see #fromBytes() + * @see #load(Object) * * @param model the data to load. - * @return A {@link DrawableTypeRequest} to set options for the load and ultimately the target to load the image - * into. */ public DrawableTypeRequest load(byte[] model) { - return (DrawableTypeRequest) loadGeneric(model) + return (DrawableTypeRequest) fromBytes().load(model); + } + + /** + * Returns a request builder that uses {@link com.bumptech.glide.load.model.stream.StreamByteArrayLoader} to load + * images from byte arrays. + * + *

+ * Note - by default loads for bytes are not cached in either the memory or the disk cache. + *

+ * + * @see #from(Class) + * @see #load(byte[]) + */ + public DrawableTypeRequest fromBytes() { + return (DrawableTypeRequest) loadGeneric(byte[].class) .signature(new StringSignature(UUID.randomUUID().toString())) .diskCacheStrategy(DiskCacheStrategy.NONE) .skipMemoryCache(true /*skipMemoryCache*/); @@ -449,33 +523,59 @@ public class RequestManager implements LifecycleListener { /** * Returns a request builder that uses the {@link com.bumptech.glide.load.model.ModelLoaderFactory}s currently - * registered for the given model Class for {@link InputStream}s and {@link ParcelFileDescriptor}s to load a + * registered for the given model class for {@link InputStream}s and {@link ParcelFileDescriptor}s to load a * thumbnail from either the image or the video represented by the given model. * - * @see #load(Object) + *

+ * Note - for maximum efficiency, consider using {@link #from(Class)}} to avoid repeatedly allocating builder + * objects. + *

+ * + * @see #from(Class) * * @param model The model the load. * @param The type of the model to load. */ public DrawableTypeRequest load(T model) { - return loadGeneric(model); + return (DrawableTypeRequest) loadGeneric(getSafeClass(model)).load(model); } - @SuppressWarnings("unchecked") - private DrawableTypeRequest loadGeneric(T model) { - ModelLoader streamModelLoader = Glide.buildStreamModelLoader(model, context); + /** + * Returns a request builder that can be used for multiple loads that uses the + * {@link com.bumptech.glide.load.model.ModelLoaderFactory}s registered for the given model class for + * {@link java.io.InputStream}s and {@link android.os.ParcelFileDescriptor}s to load a thumbnail from objects of + * the given modelClass. + * + *

+ * Note - you must use {@link com.bumptech.glide.DrawableRequestBuilder#load(Object)}} to set a concrete model + * to be loaded before calling + * {@link com.bumptech.glide.DrawableRequestBuilder#into(com.bumptech.glide.request.target.Target)}. You may + * also use this object for repeated loads by calling request.load(model).into(target). You may + * also adjust the options after calling {@link com.bumptech.glide.DrawableRequestBuilder#load(Object)}} and/or + * {@link com.bumptech.glide.DrawableRequestBuilder#into(com.bumptech.glide.request.target.Target)}}. However, + * keep in mind that any changes in options will apply to all future loads. + *

+ * + * @param modelClass The class of model requests built by this class will load data from. + * @param The type of the model. + */ + public DrawableTypeRequest from(Class modelClass) { + return loadGeneric(modelClass); + } + + private DrawableTypeRequest loadGeneric(Class modelClass) { + ModelLoader streamModelLoader = Glide.buildStreamModelLoader(modelClass, context); ModelLoader fileDescriptorModelLoader = - Glide.buildFileDescriptorModelLoader(model, context); - if (model != null && streamModelLoader == null && fileDescriptorModelLoader == null) { - throw new IllegalArgumentException("Unknown type " + model + ". You must provide a Model of a type for" + Glide.buildFileDescriptorModelLoader(modelClass, context); + if (modelClass != null && streamModelLoader == null && fileDescriptorModelLoader == null) { + throw new IllegalArgumentException("Unknown type " + modelClass + ". You must provide a Model of a type for" + " which there is a registered ModelLoader, if you are using a custom model, you must first call" + " Glide#register with a ModelLoaderFactory for your custom model class"); } - return (DrawableTypeRequest) optionsApplier.apply( - new DrawableTypeRequest(getSafeClass(model), streamModelLoader, fileDescriptorModelLoader, context, - glide, requestTracker, lifecycle, optionsApplier)) - .load(model); + return optionsApplier.apply( + new DrawableTypeRequest(modelClass, streamModelLoader, fileDescriptorModelLoader, context, + glide, requestTracker, lifecycle, optionsApplier)); } @SuppressWarnings("unchecked") @@ -516,10 +616,27 @@ public class RequestManager implements LifecycleListener { this.loader = loader; } + /** + * Returns a request builder that uses the provided {@link com.bumptech.glide.load.model.ModelLoader} to load + * images from an {@link java.io.InputStream}s obtained from models of the given model class. + * + * @param modelClass The class of model to load images from. + */ + public DrawableTypeRequest from(Class modelClass) { + return optionsApplier.apply(new DrawableTypeRequest(modelClass, loader, null, context, glide, + requestTracker, lifecycle, optionsApplier)); + } + + /** + * Returns a request builder that uses the provided {@link com.bumptech.glide.load.model.ModelLoader} to load + * an image from an {@link java.io.InputStream} obtained from the given model. + * + * @see #from(Class) + * + * @param model The model to load an image from. + */ public DrawableTypeRequest load(T model) { - return (DrawableTypeRequest) optionsApplier.apply(new DrawableTypeRequest(getSafeClass(model), loader, - null, context, glide, requestTracker, lifecycle, optionsApplier)) - .load(model); + return (DrawableTypeRequest) from(getSafeClass(model)).load(model); } } @@ -540,11 +657,21 @@ public class RequestManager implements LifecycleListener { this.dataClass = dataClass; } + /** + * Sets the type of model that will be loaded. + * + * @param modelClass the class of model to use. + * @return A request builder + */ + public GenericTypeRequest from(Class modelClass) { + return new GenericTypeRequest(modelClass); + } + /** * Sets the specific model that will be loaded. * * @param model The model to use. - * @return This request builder. + * @return A request builder. */ public GenericTypeRequest load(A model) { return new GenericTypeRequest(model); @@ -557,9 +684,19 @@ public class RequestManager implements LifecycleListener { */ public final class GenericTypeRequest { private final A model; + private final Class modelClass; + private final boolean providedModel; GenericTypeRequest(A model) { + providedModel = true; this.model = model; + this.modelClass = getSafeClass(model); + } + + GenericTypeRequest(Class modelClass) { + providedModel = false; + this.model = null; + this.modelClass = modelClass; } /** @@ -570,10 +707,13 @@ public class RequestManager implements LifecycleListener { * @return This request builder. */ public GenericTranscodeRequest as(Class resourceClass) { - return (GenericTranscodeRequest) optionsApplier.apply( - new GenericTranscodeRequest(context, glide, getSafeClass(model), modelLoader, - dataClass, resourceClass, requestTracker, lifecycle, optionsApplier)) - .load(model); + GenericTranscodeRequest result = + optionsApplier.apply(new GenericTranscodeRequest(context, glide, modelClass, + modelLoader, dataClass, resourceClass, requestTracker, lifecycle, optionsApplier)); + if (providedModel) { + result.load(model); + } + return result; } } } diff --git a/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoGrid.java b/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoGrid.java index 11d25e45d..4f9739138 100644 --- a/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoGrid.java +++ b/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoGrid.java @@ -37,6 +37,9 @@ public class FlickrPhotoGrid extends Fragment implements PhotoViewer { private int photoSize; private GridView grid; private boolean thumbnail; + private DrawableRequestBuilder fullRequest; + private DrawableRequestBuilder thumbnailRequest; + private DrawableRequestBuilder preloadRequest; public static FlickrPhotoGrid newInstance(int size, int preloadCount, boolean thumbnail) { FlickrPhotoGrid photoGrid = new FlickrPhotoGrid(); @@ -54,6 +57,22 @@ public class FlickrPhotoGrid extends Fragment implements PhotoViewer { photoSize = args.getInt(IMAGE_SIZE_KEY); thumbnail = args.getBoolean(THUMBNAIL_KEY); + fullRequest = Glide.with(this) + .from(Photo.class) + .centerCrop() + .crossFade(R.anim.fade_in, 150); + + thumbnailRequest = Glide.with(this) + .from(Photo.class) + .priority(Priority.HIGH) + .diskCacheStrategy(DiskCacheStrategy.SOURCE) + .override(Api.SQUARE_THUMB_SIZE, Api.SQUARE_THUMB_SIZE); + + preloadRequest = thumbnail ? thumbnailRequest : Glide.with(this) + .from(Photo.class) + .priority(Priority.HIGH) + .centerCrop(); + final View result = inflater.inflate(R.layout.flickr_photo_grid, container, false); grid = (GridView) result.findViewById(R.id.images); grid.setColumnWidth(photoSize); @@ -109,16 +128,7 @@ public class FlickrPhotoGrid extends Fragment implements PhotoViewer { @Override protected GenericRequestBuilder getRequestBuilder(Photo item) { - DrawableRequestBuilder request = Glide.with(FlickrPhotoGrid.this) - .load(item) - .priority(Priority.HIGH); - if (thumbnail) { - request.override(Api.SQUARE_THUMB_SIZE, Api.SQUARE_THUMB_SIZE) - .diskCacheStrategy(DiskCacheStrategy.SOURCE); - } else { - request.centerCrop(); - } - return request; + return preloadRequest.load(item); } } @@ -163,20 +173,10 @@ public class FlickrPhotoGrid extends Fragment implements PhotoViewer { imageView = (ImageView) view; } - DrawableRequestBuilder request = Glide.with(FlickrPhotoGrid.this) + fullRequest .load(current) - .centerCrop() - .crossFade(R.anim.fade_in, 150); - - if (thumbnail) { - request.thumbnail(Glide.with(FlickrPhotoGrid.this) - .load(current) - .override(Api.SQUARE_THUMB_SIZE, Api.SQUARE_THUMB_SIZE) - .diskCacheStrategy(DiskCacheStrategy.SOURCE) - ); - } - - request.into(imageView); + .thumbnail(thumbnail ? thumbnailRequest.load(current) : null) + .into(imageView); return imageView; } diff --git a/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoList.java b/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoList.java index 65e259dbf..19537c98a 100644 --- a/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoList.java +++ b/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoList.java @@ -12,9 +12,11 @@ import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; +import com.bumptech.glide.DrawableRequestBuilder; import com.bumptech.glide.GenericRequestBuilder; import com.bumptech.glide.Glide; import com.bumptech.glide.ListPreloader; +import com.bumptech.glide.Priority; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.samples.flickr.api.Api; import com.bumptech.glide.samples.flickr.api.Photo; @@ -32,6 +34,9 @@ public class FlickrPhotoList extends Fragment implements PhotoViewer { private List currentPhotos; private FlickrListPreloader preloader; private ListView list; + private DrawableRequestBuilder fullRequest; + private DrawableRequestBuilder thumbRequest; + private DrawableRequestBuilder preloadRequest; public static FlickrPhotoList newInstance() { return new FlickrPhotoList(); @@ -57,6 +62,23 @@ public class FlickrPhotoList extends Fragment implements PhotoViewer { adapter.setPhotos(currentPhotos); } + fullRequest = Glide.with(FlickrPhotoList.this) + .from(Photo.class) + .placeholder(new ColorDrawable(Color.GRAY)) + .centerCrop(); + + preloadRequest = Glide.with(FlickrPhotoList.this) + .from(Photo.class) + .placeholder(new ColorDrawable(Color.GRAY)) + .centerCrop() + .priority(Priority.HIGH); + + thumbRequest = Glide.with(FlickrPhotoList.this) + .from(Photo.class) + .diskCacheStrategy(DiskCacheStrategy.SOURCE) + .override(Api.SQUARE_THUMB_SIZE, Api.SQUARE_THUMB_SIZE); + + if (savedInstanceState != null) { int index = savedInstanceState.getInt(STATE_POSITION_INDEX); int offset = savedInstanceState.getInt(STATE_POSITION_OFFSET); @@ -117,14 +139,9 @@ public class FlickrPhotoList extends Fragment implements PhotoViewer { @Override protected GenericRequestBuilder getRequestBuilder(Photo item) { - return Glide.with(FlickrPhotoList.this) - .load(item) - .centerCrop() - .thumbnail(Glide.with(FlickrPhotoList.this) - .load(item) - .diskCacheStrategy(DiskCacheStrategy.SOURCE) - .override(Api.SQUARE_THUMB_SIZE, Api.SQUARE_THUMB_SIZE) - ); + return preloadRequest + .thumbnail(thumbRequest.load(item)) + .load(item); } } @@ -178,15 +195,9 @@ public class FlickrPhotoList extends Fragment implements PhotoViewer { viewHolder = (ViewHolder) view.getTag(); } - Glide.with(FlickrPhotoList.this) + fullRequest + .thumbnail(thumbRequest.load(current)) .load(current) - .placeholder(new ColorDrawable(Color.GRAY)) - .centerCrop() - .crossFade(R.anim.fade_in, 150) - .thumbnail(Glide.with(FlickrPhotoList.this) - .load(current) - .diskCacheStrategy(DiskCacheStrategy.SOURCE) - .override(Api.SQUARE_THUMB_SIZE, Api.SQUARE_THUMB_SIZE)) .into(viewHolder.imageView); viewHolder.titleText.setText(current.getTitle()); diff --git a/samples/giphy/src/main/java/com/bumptech/glide/samples/giphy/MainActivity.java b/samples/giphy/src/main/java/com/bumptech/glide/samples/giphy/MainActivity.java index 0bf0dd264..6f5fd6421 100644 --- a/samples/giphy/src/main/java/com/bumptech/glide/samples/giphy/MainActivity.java +++ b/samples/giphy/src/main/java/com/bumptech/glide/samples/giphy/MainActivity.java @@ -8,9 +8,11 @@ import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; +import com.bumptech.glide.DrawableRequestBuilder; import com.bumptech.glide.GenericRequestBuilder; import com.bumptech.glide.Glide; import com.bumptech.glide.ListPreloader; +import com.bumptech.glide.Priority; import java.io.InputStream; import java.util.ArrayList; @@ -23,6 +25,7 @@ public class MainActivity extends Activity implements Api.Monitor { private static final String TAG = "GiphyActivity"; private GifAdapter adapter; + private DrawableRequestBuilder preloadRequest; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,7 +44,16 @@ public class MainActivity extends Activity implements Api.Monitor { ListView gifList = (ListView) findViewById(R.id.gif_list); GiphyPreloader preloader = new GiphyPreloader(2); - adapter = new GifAdapter(this, preloader); + DrawableRequestBuilder fullRequest = Glide.with(this) + .from(Api.GifResult.class) + .fitCenter(); + + preloadRequest = Glide.with(this) + .from(Api.GifResult.class) + .fitCenter() + .priority(Priority.HIGH); + + adapter = new GifAdapter(this, preloader, fullRequest); gifList.setAdapter(adapter); gifList.setOnScrollListener(preloader); } @@ -87,9 +99,7 @@ public class MainActivity extends Activity implements Api.Monitor { @Override protected GenericRequestBuilder getRequestBuilder(Api.GifResult item) { - return Glide.with(MainActivity.this) - .load(item) - .fitCenter(); + return preloadRequest.load(item); } } @@ -98,12 +108,15 @@ public class MainActivity extends Activity implements Api.Monitor { private final Activity activity; private final GiphyPreloader preloader; + private DrawableRequestBuilder requestBuilder; private Api.GifResult[] results = EMPTY_RESULTS; - public GifAdapter(Activity activity, GiphyPreloader preloader) { + public GifAdapter(Activity activity, GiphyPreloader preloader, + DrawableRequestBuilder requestBuilder) { this.activity = activity; this.preloader = preloader; + this.requestBuilder = requestBuilder; } public void setResults(Api.GifResult[] results) { @@ -142,9 +155,8 @@ public class MainActivity extends Activity implements Api.Monitor { } final ImageView gifView = (ImageView) convertView.findViewById(R.id.gif_view); - Glide.with(activity) + requestBuilder .load(result) - .fitCenter() .into(gifView); if (preloader.dimensions == null) { diff --git a/samples/svg/src/main/java/com/bumptech/svgsample/app/MainActivity.java b/samples/svg/src/main/java/com/bumptech/svgsample/app/MainActivity.java index 15da59510..4d60dc36b 100644 --- a/samples/svg/src/main/java/com/bumptech/svgsample/app/MainActivity.java +++ b/samples/svg/src/main/java/com/bumptech/svgsample/app/MainActivity.java @@ -1,13 +1,15 @@ package com.bumptech.svgsample.app; import android.app.Activity; +import android.content.ContentResolver; import android.graphics.drawable.PictureDrawable; +import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.TextView; - +import com.bumptech.glide.GenericRequestBuilder; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.model.StreamEncoder; @@ -23,8 +25,9 @@ import java.io.InputStream; public class MainActivity extends Activity { private static final String TAG = "SVGActivity"; - ImageView imageViewRes; - ImageView imageViewNet; + private ImageView imageViewRes; + private ImageView imageViewNet; + private GenericRequestBuilder requestBuilder; @Override protected void onCreate(Bundle savedInstanceState) { @@ -33,6 +36,19 @@ public class MainActivity extends Activity { imageViewRes = (ImageView) findViewById(R.id.svg_image_view1); imageViewNet = (ImageView) findViewById(R.id.svg_image_view2); + + requestBuilder = Glide.with(this) + .using(Glide.buildStreamModelLoader(Uri.class, this), InputStream.class) + .from(Uri.class) + .as(SVG.class) + .transcode(new SvgDrawableTranscoder(), PictureDrawable.class) + .sourceEncoder(new StreamEncoder()) + .cacheDecoder(new FileToStreamDecoder(new SvgDecoder())) + .decoder(new SvgDecoder()) + .placeholder(R.drawable.image_loading) + .error(R.drawable.image_error) + .animate(android.R.anim.fade_in) + .listener(new SvgSoftwareLayerSetter()); } @Override @@ -77,38 +93,22 @@ public class MainActivity extends Activity { } private void loadRes() { - Glide.with(this) - .using(Glide.buildStreamModelLoader(Integer.class, this), InputStream.class) - .load(R.raw.android_toy_h) - .as(SVG.class) - .transcode(new SvgDrawableTranscoder(), PictureDrawable.class) + Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + + R.raw.android_toy_h); + requestBuilder .diskCacheStrategy(DiskCacheStrategy.NONE) // SVG cannot be serialized so it's not worth to cache it // and the getResources() should be fast enough when acquiring the InputStream - .decoder(new SvgDecoder()) - .placeholder(R.drawable.image_loading) - .error(R.drawable.image_error) - .animate(android.R.anim.fade_in) - .listener(new SvgSoftwareLayerSetter()) + .load(uri) .into(imageViewRes); } private void loadNet() { - Glide.with(this) - .using(Glide.buildStreamModelLoader(String.class, this), InputStream.class) - .load("http://www.clker.com/cliparts/u/Z/2/b/a/6/android-toy-h.svg") - .as(SVG.class) - .transcode(new SvgDrawableTranscoder(), PictureDrawable.class) + Uri uri = Uri.parse("http://www.clker.com/cliparts/u/Z/2/b/a/6/android-toy-h.svg"); + requestBuilder .diskCacheStrategy(DiskCacheStrategy.SOURCE) // SVG cannot be serialized so it's not worth to cache it - .sourceEncoder(new StreamEncoder()) - // however loading from the network can be cached via StreamEncoder - .cacheDecoder(new FileToStreamDecoder(new SvgDecoder())) - .decoder(new SvgDecoder()) - .placeholder(R.drawable.image_loading) - .error(R.drawable.image_error) - .animate(android.R.anim.fade_in) - .listener(new SvgSoftwareLayerSetter()) + .load(uri) .into(imageViewNet); } } -- GitLab