Skip to content

Commit 37c2504

Browse files
committed
Added resizing with keeping aspect ratio when dimension is 0.
1 parent 4e52154 commit 37c2504

File tree

5 files changed

+66
-28
lines changed

5 files changed

+66
-28
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
@@ -451,12 +451,15 @@ static Bitmap transformResult(Request data, Bitmap result, int exifRotation) {
451451
float heightRatio = targetHeight / (float) inHeight;
452452
float scale = widthRatio < heightRatio ? widthRatio : heightRatio;
453453
matrix.preScale(scale, scale);
454-
} else if (targetWidth != 0 && targetHeight != 0 //
454+
} else if ((targetWidth != 0 || targetHeight != 0) //
455455
&& (targetWidth != inWidth || targetHeight != inHeight)) {
456456
// If an explicit target size has been specified and they do not match the results bounds,
457457
// pre-scale the existing matrix appropriately.
458-
float sx = targetWidth / (float) inWidth;
459-
float sy = targetHeight / (float) inHeight;
458+
// Keep aspect ratio if one dimension is set to 0.
459+
float sx = targetWidth != 0 ? targetWidth / (float) inWidth
460+
: targetHeight / (float) inHeight;
461+
float sy = targetHeight != 0 ? targetHeight / (float) inHeight
462+
: targetWidth / (float) inWidth;
460463
matrix.preScale(sx, sy);
461464
}
462465
}

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

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

159159
public boolean hasSize() {
160-
return targetWidth != 0;
160+
return targetWidth != 0 || targetHeight != 0;
161161
}
162162

163163
boolean needsTransformation() {
164164
return needsMatrixTransform() || hasCustomTransformations();
165165
}
166166

167167
boolean needsMatrixTransform() {
168-
return targetWidth != 0 || rotationDegrees != 0;
168+
return hasSize() || rotationDegrees != 0;
169169
}
170170

171171
boolean hasCustomTransformations() {
@@ -230,7 +230,7 @@ boolean hasImage() {
230230
}
231231

232232
boolean hasSize() {
233-
return targetWidth != 0;
233+
return targetWidth != 0 || targetHeight != 0;
234234
}
235235

236236
boolean hasPriority() {
@@ -265,13 +265,19 @@ public Builder setResourceId(int resourceId) {
265265
return this;
266266
}
267267

268-
/** Resize the image to the specified size in pixels. */
268+
/**
269+
* Resize the image to the specified size in pixels.
270+
* Use 0 as desired dimension to resize keeping aspect ratio.
271+
*/
269272
public Builder resize(int targetWidth, int targetHeight) {
270-
if (targetWidth <= 0) {
271-
throw new IllegalArgumentException("Width must be positive number.");
273+
if (targetWidth < 0) {
274+
throw new IllegalArgumentException("Width must be positive number or 0.");
275+
}
276+
if (targetHeight < 0) {
277+
throw new IllegalArgumentException("Height must be positive number or 0.");
272278
}
273-
if (targetHeight <= 0) {
274-
throw new IllegalArgumentException("Height must be positive number.");
279+
if (targetHeight == 0 && targetWidth == 0) {
280+
throw new IllegalArgumentException("At least one dimension has to be positive number.");
275281
}
276282
this.targetWidth = targetWidth;
277283
this.targetHeight = targetHeight;
@@ -387,11 +393,13 @@ public Request build() {
387393
if (centerInside && centerCrop) {
388394
throw new IllegalStateException("Center crop and center inside can not be used together.");
389395
}
390-
if (centerCrop && targetWidth == 0) {
391-
throw new IllegalStateException("Center crop requires calling resize.");
396+
if (centerCrop && (targetWidth == 0 || targetHeight == 0)) {
397+
throw new IllegalStateException(
398+
"Center crop requires calling resize with positive width and height.");
392399
}
393-
if (centerInside && targetWidth == 0) {
394-
throw new IllegalStateException("Center inside requires calling resize.");
400+
if (centerInside && (targetWidth == 0 || targetHeight == 0)) {
401+
throw new IllegalStateException(
402+
"Center inside requires calling resize with positive width and height.");
395403
}
396404
if (priority == null) {
397405
priority = Priority.NORMAL;

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,19 @@ static void calculateInSampleSize(int reqWidth, int reqHeight, int width, int he
145145
BitmapFactory.Options options, Request request) {
146146
int sampleSize = 1;
147147
if (height > reqHeight || width > reqWidth) {
148-
final int heightRatio = (int) Math.floor((float) height / (float) reqHeight);
149-
final int widthRatio = (int) Math.floor((float) width / (float) reqWidth);
150-
sampleSize = request.centerInside
151-
? Math.max(heightRatio, widthRatio)
152-
: Math.min(heightRatio, widthRatio);
148+
final int heightRatio;
149+
final int widthRatio;
150+
if (reqHeight == 0) {
151+
sampleSize = (int) Math.floor((float) width / (float) reqWidth);
152+
} else if (reqWidth == 0) {
153+
sampleSize = (int) Math.floor((float) height / (float) reqHeight);
154+
} else {
155+
heightRatio = (int) Math.floor((float) height / (float) reqHeight);
156+
widthRatio = (int) Math.floor((float) width / (float) reqWidth);
157+
sampleSize = request.centerInside
158+
? Math.max(heightRatio, widthRatio)
159+
: Math.min(heightRatio, widthRatio);
160+
}
153161
}
154162
options.inSampleSize = sampleSize;
155163
options.inJustDecodeBounds = false;

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

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

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,30 @@ public void intoRemoteViewsNotificationWithFitThrows() throws Exception {
495495
}
496496
}
497497

498+
@Test
499+
public void intoTargetResizeWith0WithCenterInsideOrCenterCropThrows() throws Exception {
500+
try {
501+
new RequestCreator(picasso, URI_1, 0).resize(0, 10).centerInside().into(mockTarget());
502+
fail("Center inside with unknown width should throw exception.");
503+
} catch (IllegalStateException expected) {
504+
}
505+
try {
506+
new RequestCreator(picasso, URI_1, 0).resize(10, 0).centerInside().into(mockTarget());
507+
fail("Center inside with unknown height should throw exception.");
508+
} catch (IllegalStateException expected) {
509+
}
510+
try {
511+
new RequestCreator(picasso, URI_1, 0).resize(0, 10).centerCrop().into(mockTarget());
512+
fail("Center inside with unknown width should throw exception.");
513+
} catch (IllegalStateException expected) {
514+
}
515+
try {
516+
new RequestCreator(picasso, URI_1, 0).resize(10, 0).centerCrop().into(mockTarget());
517+
fail("Center inside with unknown height should throw exception.");
518+
} catch (IllegalStateException expected) {
519+
}
520+
}
521+
498522
@Test public void appWidgetActionWithDefaultPriority() throws Exception {
499523
new RequestCreator(picasso, URI_1, 0).into(mockRemoteViews(), 0, new int[] { 1, 2, 3 });
500524
verify(picasso).enqueueAndSubmit(actionCaptor.capture());
@@ -559,13 +583,8 @@ public void intoRemoteViewsNotificationWithFitThrows() throws Exception {
559583
} catch (IllegalArgumentException expected) {
560584
}
561585
try {
562-
new RequestCreator().resize(0, 10);
563-
fail("Zero width should throw exception.");
564-
} catch (IllegalArgumentException expected) {
565-
}
566-
try {
567-
new RequestCreator().resize(10, 0);
568-
fail("Zero height should throw exception.");
586+
new RequestCreator().resize(0, 0);
587+
fail("Zero dimensions should throw exception.");
569588
} catch (IllegalArgumentException expected) {
570589
}
571590
}

0 commit comments

Comments
 (0)