Skip to content

Commit

Permalink
Allow arbitrary transformations
Browse files Browse the repository at this point in the history
Also passes loaders all the way to image manager
to decrease # of objects created for images
in the memory or disk cache.
  • Loading branch information
Sam Judd committed Aug 8, 2013
1 parent ef34a5b commit c572847
Show file tree
Hide file tree
Showing 25 changed files with 190 additions and 366 deletions.
54 changes: 46 additions & 8 deletions library/src/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import com.bumptech.glide.loader.model.StringLoader;
import com.bumptech.glide.loader.model.UriLoader;
import com.bumptech.glide.loader.model.UrlLoader;
import com.bumptech.glide.loader.transformation.CenterCrop;
import com.bumptech.glide.loader.transformation.FitCenter;
import com.bumptech.glide.loader.transformation.TransformationLoader;
import com.bumptech.glide.presenter.ImagePresenter;
import com.bumptech.glide.presenter.ImageReadyCallback;
import com.bumptech.glide.resize.Downsampler;
import com.bumptech.glide.resize.ImageManager;
import com.bumptech.glide.resize.Transformation;
import com.bumptech.glide.resize.loader.Approximate;
import com.bumptech.glide.resize.loader.CenterCrop;
import com.bumptech.glide.resize.loader.FitCenter;
import com.bumptech.glide.resize.loader.ImageManagerLoader;

import java.io.File;
Expand Down Expand Up @@ -338,6 +338,7 @@ public Request<T> load(T model) {
*/
@SuppressWarnings("unused") //public api
public static class Request<T> {

private enum ResizeOption {
APPROXIMATE,
CENTER_CROP,
Expand All @@ -353,8 +354,9 @@ private enum ResizeOption {
private int animationId = -1;
private int placeholderId = -1;
private int errorId = -1;
private Transformation transformation = Transformation.NONE;
private Transformation transformation = null;
private Downsampler downsampler = null;
private TransformationLoader<T> transformationLoader = null;

public Request(T model) {
this.model = model;
Expand All @@ -376,6 +378,7 @@ public Request(T model, ModelLoader<T> modelLoader) {
public Request<T> centerCrop() {
transformation = Transformation.CENTER_CROP;
downsampler = Downsampler.AT_LEAST;
transformationLoader = null;

return this;
}
Expand All @@ -388,6 +391,7 @@ public Request<T> centerCrop() {
public Request<T> fitCenter() {
transformation = Transformation.FIT_CENTER;
downsampler = Downsampler.AT_LEAST;
transformationLoader = null;

return this;
}
Expand All @@ -400,6 +404,7 @@ public Request<T> fitCenter() {
public Request<T> approximate() {
transformation = Transformation.NONE;
downsampler = Downsampler.AT_LEAST;
transformationLoader = null;

return this;
}
Expand All @@ -413,6 +418,7 @@ public Request<T> approximate() {
public Request<T> asIs() {
transformation = Transformation.NONE;
downsampler = Downsampler.NONE;
transformationLoader = null;

return this;
}
Expand All @@ -424,9 +430,18 @@ public Request<T> asIs() {
* @param transformation The transformation to use
* @return This Request
*/
public Request<T> transform(Transformation transformation) {
public Request<T> transform(final Transformation transformation) {
this.transformation = transformation;
downsampler = Downsampler.AT_LEAST;
transformationLoader = null;

return this;
}

public Request<T> transform(TransformationLoader<T> transformationLoader) {
this.transformationLoader = transformationLoader;
transformation = null;
downsampler = Downsampler.AT_LEAST;

return this;
}
Expand Down Expand Up @@ -508,11 +523,13 @@ private ImagePresenter<T> buildImagePresenter(ImageView imageView) {
final Context context = imageView.getContext();

modelLoader = getFinalModelLoader(context);
transformationLoader = getFinalTransformationLoader();

ImagePresenter.Builder<T> builder = new ImagePresenter.Builder<T>()
.setImageView(imageView)
.setModelLoader(modelLoader)
.setImageLoader(new ImageManagerLoader(context, downsampler, transformation));
.setImageLoader(new ImageManagerLoader(context, downsampler))
.setTransformationLoader(transformationLoader);

if (animationId != -1) {
final Animation animation = AnimationUtils.loadAnimation(imageView.getContext(), animationId);
Expand All @@ -533,7 +550,6 @@ public void onImageReady(ImageView view, boolean fromCache) {
builder.setPlaceholderResource(placeholderId);
}


if (errorId != -1) {
builder.setErrorResource(errorId);
}
Expand All @@ -549,6 +565,27 @@ private ModelLoader<T> getFinalModelLoader(Context context) {
}
}

private TransformationLoader<T> getFinalTransformationLoader() {
if (transformationLoader != null) {
return transformationLoader;
} else {
return new TransformationLoader<T>() {
@Override
public Transformation getTransformation(T model) {
return transformation;
}
};
}
}

private String getFinalTransformationId() {
if (transformationLoader != null) {
return transformationLoader.getClass().toString();
} else {
return transformation.getId();
}
}

private Downsampler getFinalDownsampler(ImageView imageView) {
Downsampler result = downsampler;
if (result == null) {
Expand All @@ -562,6 +599,7 @@ private Downsampler getFinalDownsampler(ImageView imageView) {
return result;
}


private static Metadata getMetadataFrom(ImageView imageView) {
return (Metadata) imageView.getTag(R.id.glide_metadata);
}
Expand All @@ -583,7 +621,7 @@ public Metadata(Request request) {
modelClass = request.model.getClass();
modelLoaderClass = request.modelLoaderClass;
downsamplerId = request.downsampler.getId();
transformationId = request.transformation.getId();
transformationId = request.getFinalTransformationId();
animationId = request.animationId;
placeholderId = request.placeholderId;
errorId = request.errorId;
Expand Down
95 changes: 0 additions & 95 deletions library/src/com/bumptech/glide/loader/image/BaseImageLoader.java

This file was deleted.

3 changes: 2 additions & 1 deletion library/src/com/bumptech/glide/loader/image/ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.graphics.Bitmap;
import com.bumptech.glide.loader.stream.StreamLoader;
import com.bumptech.glide.resize.Transformation;

/**
* An interface used by {@link com.bumptech.glide.presenter.ImagePresenter} to fetch a bitmap for a given id and
Expand Down Expand Up @@ -44,7 +45,7 @@ public interface ImageReadyCallback {
*
* @return A reference to the fetch that must be retained by the calling object as long as the fetch is relevant
*/
public Object fetchImage(String id, StreamLoader streamLoader, int width, int height, ImageReadyCallback cb);
public Object fetchImage(String id, StreamLoader streamLoader, Transformation transformation, int width, int height, ImageReadyCallback cb);

/**
* Called when the current image load does not need to continue and any corresponding cleanup to save cpu
Expand Down
29 changes: 0 additions & 29 deletions library/src/com/bumptech/glide/loader/model/BaseModelLoader.java

This file was deleted.

3 changes: 0 additions & 3 deletions library/src/com/bumptech/glide/loader/model/FileLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,4 @@ public String getId(File model) {
//canonical is better, but also slower
return model.getAbsolutePath();
}

@Override
public void clear() { }
}
6 changes: 0 additions & 6 deletions library/src/com/bumptech/glide/loader/model/ModelLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,4 @@ public interface ModelLoader<T> {
* it does not have to.
*/
public String getId(T model);

/**
* A method that allows the ModelLoader to cleanup any old or inprocess {@link StreamLoader}. Primarily oriented
* at allowing the model loader to call {@link com.bumptech.glide.loader.stream.StreamLoader#cancel()};
*/
public void clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,4 @@ public StreamLoader getStreamLoader(Integer model, int width, int height) {
public String getId(Integer model) {
return model.toString();
}

@Override
public void clear() { }
}
4 changes: 2 additions & 2 deletions library/src/com/bumptech/glide/loader/model/StringLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* A model loader for handling certain string models. Handles paths, urls, and any uri string with a scheme handled by
* {@link android.content.ContentResolver#openInputStream(android.net.Uri)}.
*/
public class StringLoader extends BaseModelLoader<String> {
public class StringLoader implements ModelLoader<String> {

private final ModelLoader<Uri> uriLoader;

Expand All @@ -28,7 +28,7 @@ public StringLoader(ModelLoader<Uri> uriLoader) {
}

@Override
protected StreamLoader buildStreamLoader(final String model, final int width, final int height) {
public StreamLoader getStreamLoader(final String model, final int width, final int height) {
Uri uri = Uri.parse(model);

final String scheme = uri.getScheme();
Expand Down
10 changes: 2 additions & 8 deletions library/src/com/bumptech/glide/loader/model/UriLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* A model loader for trying to load Uris. Capable of handling 'http', 'https', 'android.resource', 'content', and
* 'file' schemes. Unsupported schemes will throw an exception in {@link #buildStreamLoader(android.net.Uri, int, int)}.
*/
public class UriLoader extends BaseModelLoader<Uri> {
public class UriLoader implements ModelLoader<Uri> {
private final Context context;
private final ModelLoader<URL> urlLoader;

Expand All @@ -32,7 +32,7 @@ public UriLoader(Context context, ModelLoader<URL> urlLoader) {
}

@Override
protected StreamLoader buildStreamLoader(Uri model, int width, int height) {
public StreamLoader getStreamLoader(Uri model, int width, int height) {
final String scheme = model.getScheme();

StreamLoader result = null;
Expand All @@ -58,12 +58,6 @@ public String getId(Uri model) {
return model.toString();
}

@Override
public void clear() {
super.clear();
urlLoader.clear();
}

private boolean isLocalUri(String scheme) {
return ContentResolver.SCHEME_FILE.equals(scheme)
|| ContentResolver.SCHEME_CONTENT.equals(scheme)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* A base ModelLoader for using Volley to fetch an image from a model that
* can readily be converted into a url
*/
public abstract class VolleyModelLoader<T> extends BaseModelLoader<T> {
public abstract class VolleyModelLoader<T> implements ModelLoader<T> {
private final RequestQueue requestQueue;

/**
Expand All @@ -30,7 +30,7 @@ public VolleyModelLoader(RequestQueue requestQueue) {
}

@Override
protected StreamLoader buildStreamLoader(T model, int width, int height) {
public StreamLoader getStreamLoader(T model, int width, int height) {
return new VolleyStreamLoader(requestQueue, getUrl(model, width, height), getRetryPolicy());
}

Expand Down
Loading

0 comments on commit c572847

Please sign in to comment.