From 8976c51516c2394d5f7f1007cf51cc9cec98dcb3 Mon Sep 17 00:00:00 2001 From: Tambet Ingo Date: Tue, 21 Apr 2015 17:49:07 +0300 Subject: [PATCH 1/2] Allow setting original image size for Requests --- .../java/com/squareup/picasso/Request.java | 39 ++++++++++++++++--- .../com/squareup/picasso/RequestCreator.java | 6 +++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/picasso/src/main/java/com/squareup/picasso/Request.java b/picasso/src/main/java/com/squareup/picasso/Request.java index 1a3fe045f3..342748ab03 100644 --- a/picasso/src/main/java/com/squareup/picasso/Request.java +++ b/picasso/src/main/java/com/squareup/picasso/Request.java @@ -54,6 +54,10 @@ public final class Request { public final String stableKey; /** List of custom transformations to be applied after the built-in transformations. */ public final List transformations; + /** Original image width. */ + public final int width; + /** Original image height. */ + public final int height; /** Target image width for resizing. */ public final int targetWidth; /** Target image height for resizing. */ @@ -85,9 +89,9 @@ public final class Request { public final Priority priority; private Request(Uri uri, int resourceId, String stableKey, List transformations, - int targetWidth, int targetHeight, boolean centerCrop, boolean centerInside, - boolean onlyScaleDown, float rotationDegrees, float rotationPivotX, float rotationPivotY, - boolean hasRotationPivot, Bitmap.Config config, Priority priority) { + int width, int height, int targetWidth, int targetHeight, boolean centerCrop, + boolean centerInside, boolean onlyScaleDown, float rotationDegrees, float rotationPivotX, + float rotationPivotY, boolean hasRotationPivot, Bitmap.Config config, Priority priority) { this.uri = uri; this.resourceId = resourceId; this.stableKey = stableKey; @@ -96,6 +100,8 @@ private Request(Uri uri, int resourceId, String stableKey, List } else { this.transformations = unmodifiableList(transformations); } + this.width = width; + this.height = height; this.targetWidth = targetWidth; this.targetHeight = targetHeight; this.centerCrop = centerCrop; @@ -167,6 +173,10 @@ String getName() { return Integer.toHexString(resourceId); } + public boolean hasOriginalSize() { + return width != 0 || height != 0; + } + public boolean hasSize() { return targetWidth != 0 || targetHeight != 0; } @@ -192,6 +202,8 @@ public static final class Builder { private Uri uri; private int resourceId; private String stableKey; + private int width; + private int height; private int targetWidth; private int targetHeight; private boolean centerCrop; @@ -225,6 +237,8 @@ private Builder(Request request) { uri = request.uri; resourceId = request.resourceId; stableKey = request.stableKey; + width = request.width; + height = request.height; targetWidth = request.targetWidth; targetHeight = request.targetHeight; centerCrop = request.centerCrop; @@ -245,6 +259,10 @@ boolean hasImage() { return uri != null || resourceId != 0; } + boolean hasOriginalSize() { + return width != 0 || height != 0; + } + boolean hasSize() { return targetWidth != 0 || targetHeight != 0; } @@ -290,6 +308,15 @@ public Builder stableKey(String stableKey) { return this; } + /** + * Set the original image size. + */ + public Builder originalSize(int width, int height) { + this.width = width; + this.height = height; + return this; + } + /** * Resize the image to the specified size in pixels. * Use 0 as desired dimension to resize keeping aspect ratio. @@ -465,9 +492,9 @@ public Request build() { if (priority == null) { priority = Priority.NORMAL; } - return new Request(uri, resourceId, stableKey, transformations, targetWidth, targetHeight, - centerCrop, centerInside, onlyScaleDown, rotationDegrees, rotationPivotX, rotationPivotY, - hasRotationPivot, config, priority); + return new Request(uri, resourceId, stableKey, transformations, width, height, targetWidth, + targetHeight, centerCrop, centerInside, onlyScaleDown, rotationDegrees, rotationPivotX, + rotationPivotY, hasRotationPivot, config, priority); } } } diff --git a/picasso/src/main/java/com/squareup/picasso/RequestCreator.java b/picasso/src/main/java/com/squareup/picasso/RequestCreator.java index ae9da6b079..b7f0071976 100644 --- a/picasso/src/main/java/com/squareup/picasso/RequestCreator.java +++ b/picasso/src/main/java/com/squareup/picasso/RequestCreator.java @@ -206,6 +206,12 @@ RequestCreator unfit() { return this; } + /** Set the original image size. */ + public RequestCreator originalSize(int width, int height) { + data.originalSize(width, height); + return this; + } + /** Resize the image to the specified dimension size. */ public RequestCreator resizeDimen(int targetWidthResId, int targetHeightResId) { Resources resources = picasso.context.getResources(); From e80ee40ed1ee68265e25b51de1b8ca8f4c0041f0 Mon Sep 17 00:00:00 2001 From: Tambet Ingo Date: Tue, 21 Apr 2015 17:49:31 +0300 Subject: [PATCH 2/2] Use the original image size from Request if available --- .../main/java/com/squareup/picasso/RequestHandler.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/picasso/src/main/java/com/squareup/picasso/RequestHandler.java b/picasso/src/main/java/com/squareup/picasso/RequestHandler.java index 7cf8c5840c..b93fcbccdb 100644 --- a/picasso/src/main/java/com/squareup/picasso/RequestHandler.java +++ b/picasso/src/main/java/com/squareup/picasso/RequestHandler.java @@ -129,15 +129,20 @@ boolean supportsReplay() { * {@link Request}, only instantiating them if needed. */ static BitmapFactory.Options createBitmapOptions(Request data) { - final boolean justBounds = data.hasSize(); + final boolean hasOriginalSize = data.hasOriginalSize(); + final boolean justBounds = !hasOriginalSize && data.hasSize(); final boolean hasConfig = data.config != null; BitmapFactory.Options options = null; - if (justBounds || hasConfig) { + if (hasOriginalSize || justBounds || hasConfig) { options = new BitmapFactory.Options(); options.inJustDecodeBounds = justBounds; if (hasConfig) { options.inPreferredConfig = data.config; } + if (hasOriginalSize && data.hasSize()) { + calculateInSampleSize(data.targetWidth, data.targetHeight, data.width, data.height, options, + data); + } } return options; }