Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Original sizes #1

Merged
merged 2 commits into from
Apr 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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