Skip to content

Commit

Permalink
Merge pull request square#1 from tambeti/original-sizes
Browse files Browse the repository at this point in the history
Original sizes
  • Loading branch information
tambeti committed Apr 21, 2015
2 parents b8c5d9e + e80ee40 commit bfdd806
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
39 changes: 33 additions & 6 deletions picasso/src/main/java/com/squareup/picasso/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Transformation> 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. */
Expand Down Expand Up @@ -85,9 +89,9 @@ public final class Request {
public final Priority priority;

private Request(Uri uri, int resourceId, String stableKey, List<Transformation> 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;
Expand All @@ -96,6 +100,8 @@ private Request(Uri uri, int resourceId, String stableKey, List<Transformation>
} else {
this.transformations = unmodifiableList(transformations);
}
this.width = width;
this.height = height;
this.targetWidth = targetWidth;
this.targetHeight = targetHeight;
this.centerCrop = centerCrop;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit bfdd806

Please sign in to comment.