Skip to content

Commit bada4aa

Browse files
committed
Added resizing with keeping aspect ratio when dimension is 0.
1 parent 5bdb180 commit bada4aa

File tree

5 files changed

+67
-25
lines changed

5 files changed

+67
-25
lines changed

picasso/src/main/java/com/squareup/picasso/BitmapHunter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,15 @@ static Bitmap transformResult(Request data, Bitmap result, int exifRotation) {
388388
float heightRatio = targetHeight / (float) inHeight;
389389
float scale = widthRatio < heightRatio ? widthRatio : heightRatio;
390390
matrix.preScale(scale, scale);
391-
} else if (targetWidth != 0 && targetHeight != 0 //
391+
} else if ((targetWidth != 0 || targetHeight != 0) //
392392
&& (targetWidth != inWidth || targetHeight != inHeight)) {
393393
// If an explicit target size has been specified and they do not match the results bounds,
394394
// pre-scale the existing matrix appropriately.
395-
float sx = targetWidth / (float) inWidth;
396-
float sy = targetHeight / (float) inHeight;
395+
// Keep aspect ratio if one dimension is set to 0.
396+
float sx = targetWidth != 0 ? targetWidth / (float) inWidth
397+
: targetHeight / (float) inHeight;
398+
float sy = targetHeight != 0 ? targetHeight / (float) inHeight
399+
: targetWidth / (float) inWidth;
397400
matrix.preScale(sx, sy);
398401
}
399402
}

picasso/src/main/java/com/squareup/picasso/Request.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ String getName() {
152152
}
153153

154154
public boolean hasSize() {
155-
return targetWidth != 0;
155+
return targetWidth != 0 || targetHeight != 0;
156156
}
157157

158158
boolean needsTransformation() {
159159
return needsMatrixTransform() || hasCustomTransformations();
160160
}
161161

162162
boolean needsMatrixTransform() {
163-
return targetWidth != 0 || rotationDegrees != 0;
163+
return hasSize() || rotationDegrees != 0;
164164
}
165165

166166
boolean hasCustomTransformations() {
@@ -223,7 +223,7 @@ boolean hasImage() {
223223
}
224224

225225
boolean hasSize() {
226-
return targetWidth != 0;
226+
return targetWidth != 0 || targetHeight != 0;
227227
}
228228

229229
/**
@@ -254,13 +254,19 @@ public Builder setResourceId(int resourceId) {
254254
return this;
255255
}
256256

257-
/** Resize the image to the specified size in pixels. */
257+
/**
258+
* Resize the image to the specified size in pixels.
259+
* Use 0 as desired dimension to resize keeping aspect ratio.
260+
*/
258261
public Builder resize(int targetWidth, int targetHeight) {
259-
if (targetWidth <= 0) {
260-
throw new IllegalArgumentException("Width must be positive number.");
262+
if (targetWidth < 0) {
263+
throw new IllegalArgumentException("Width must be positive number or 0.");
264+
}
265+
if (targetHeight < 0) {
266+
throw new IllegalArgumentException("Height must be positive number or 0.");
261267
}
262-
if (targetHeight <= 0) {
263-
throw new IllegalArgumentException("Height must be positive number.");
268+
if (targetHeight == 0 && targetWidth == 0) {
269+
throw new IllegalArgumentException("At least one dimension has to be positive number.");
264270
}
265271
this.targetWidth = targetWidth;
266272
this.targetHeight = targetHeight;
@@ -364,11 +370,13 @@ public Request build() {
364370
if (centerInside && centerCrop) {
365371
throw new IllegalStateException("Center crop and center inside can not be used together.");
366372
}
367-
if (centerCrop && targetWidth == 0) {
368-
throw new IllegalStateException("Center crop requires calling resize.");
373+
if (centerCrop && (targetWidth == 0 || targetHeight == 0)) {
374+
throw new IllegalStateException(
375+
"Center crop requires calling resize with positive width and height.");
369376
}
370-
if (centerInside && targetWidth == 0) {
371-
throw new IllegalStateException("Center inside requires calling resize.");
377+
if (centerInside && (targetWidth == 0 || targetHeight == 0)) {
378+
throw new IllegalStateException(
379+
"Center inside requires calling resize with positive width and height.");
372380
}
373381
return new Request(uri, resourceId, transformations, targetWidth, targetHeight, centerCrop,
374382
centerInside, rotationDegrees, rotationPivotX, rotationPivotY, hasRotationPivot, config);

picasso/src/main/java/com/squareup/picasso/RequestHandler.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,20 @@ static void calculateInSampleSize(int reqWidth, int reqHeight, int width, int he
143143
BitmapFactory.Options options) {
144144
int sampleSize = 1;
145145
if (height > reqHeight || width > reqWidth) {
146-
final int heightRatio = (int) Math.floor((float) height / (float) reqHeight);
147-
final int widthRatio = (int) Math.floor((float) width / (float) reqWidth);
146+
final int heightRatio;
147+
final int widthRatio;
148+
if (reqHeight == 0) {
149+
int ratio = (int) Math.floor((float) width / (float) reqWidth);
150+
heightRatio = ratio;
151+
widthRatio = ratio;
152+
} else if (reqWidth == 0) {
153+
int ratio = (int) Math.floor((float) height / (float) reqHeight);
154+
heightRatio = ratio;
155+
widthRatio = ratio;
156+
} else {
157+
heightRatio = (int) Math.floor((float) height / (float) reqHeight);
158+
widthRatio = (int) Math.floor((float) width / (float) reqWidth);
159+
}
148160
sampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
149161
}
150162
options.inSampleSize = sampleSize;

picasso/src/main/java/com/squareup/picasso/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static String createKey(Request data, StringBuilder builder) {
179179
}
180180
builder.append('\n');
181181
}
182-
if (data.targetWidth != 0) {
182+
if (data.hasSize()) {
183183
builder.append("resize:").append(data.targetWidth).append('x').append(data.targetHeight);
184184
builder.append('\n');
185185
}

picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,30 @@ public void intoRemoteViewsNotificationWithFitThrows() throws Exception {
432432
}
433433
}
434434

435+
@Test
436+
public void intoTargetResizeWith0WithCenterInsideOrCenterCropThrows() throws Exception {
437+
try {
438+
new RequestCreator(picasso, URI_1, 0).resize(0, 10).centerInside().into(mockTarget());
439+
fail("Center inside with unknown width should throw exception.");
440+
} catch (IllegalStateException expected) {
441+
}
442+
try {
443+
new RequestCreator(picasso, URI_1, 0).resize(10, 0).centerInside().into(mockTarget());
444+
fail("Center inside with unknown height should throw exception.");
445+
} catch (IllegalStateException expected) {
446+
}
447+
try {
448+
new RequestCreator(picasso, URI_1, 0).resize(0, 10).centerCrop().into(mockTarget());
449+
fail("Center inside with unknown width should throw exception.");
450+
} catch (IllegalStateException expected) {
451+
}
452+
try {
453+
new RequestCreator(picasso, URI_1, 0).resize(10, 0).centerCrop().into(mockTarget());
454+
fail("Center inside with unknown height should throw exception.");
455+
} catch (IllegalStateException expected) {
456+
}
457+
}
458+
435459
@Test public void invalidResize() throws Exception {
436460
try {
437461
new RequestCreator().resize(-1, 10);
@@ -444,13 +468,8 @@ public void intoRemoteViewsNotificationWithFitThrows() throws Exception {
444468
} catch (IllegalArgumentException expected) {
445469
}
446470
try {
447-
new RequestCreator().resize(0, 10);
448-
fail("Zero width should throw exception.");
449-
} catch (IllegalArgumentException expected) {
450-
}
451-
try {
452-
new RequestCreator().resize(10, 0);
453-
fail("Zero height should throw exception.");
471+
new RequestCreator().resize(0, 0);
472+
fail("Zero dimensions should throw exception.");
454473
} catch (IllegalArgumentException expected) {
455474
}
456475
}

0 commit comments

Comments
 (0)