/** * Resize the image to the specified dimension size. * Use 0 as desired dimension to resize keeping aspect ratio. */ @NonNull public RequestCreator resizeDimen(int targetWidthResId, int targetHeightResId) { Resources resources = picasso.context.getResources(); int targetWidth = resources.getDimensionPixelSize(targetWidthResId); int targetHeight = resources.getDimensionPixelSize(targetHeightResId); return resize(targetWidth, targetHeight); }
@Override public boolean onPreDraw() { ImageView target = this.target; ViewTreeObserver vto = target.getViewTreeObserver(); if (!vto.isAlive()) { return true; } int width = target.getWidth(); int height = target.getHeight(); if (width <= 0 || height <= 0) { return true; } target.removeOnAttachStateChangeListener(this); vto.removeOnPreDrawListener(this); this.creator.unfit().resize(width, height).into(target, callback); return true; }
@Test public void invalidResize() { try { new RequestCreator().resize(-1, 10); fail("Negative width should throw exception."); } catch (IllegalArgumentException ignored) { } try { new RequestCreator().resize(10, -1); fail("Negative height should throw exception."); } catch (IllegalArgumentException ignored) { } try { new RequestCreator().resize(0, 0); fail("Zero dimensions should throw exception."); } catch (IllegalArgumentException ignored) { } }
@Test public void invalidCenterCrop() { try { new RequestCreator().resize(10, 10).centerInside().centerCrop(); fail("Calling center crop after center inside should throw exception."); } catch (IllegalStateException ignored) { } }
@Test public void invalidCenterInside() { try { new RequestCreator().resize(10, 10).centerInside().centerCrop(); fail("Calling center inside after center crop should throw exception."); } catch (IllegalStateException ignored) { } }
@Test public void intoImageViewWithFitAndResizeThrows() { try { ImageView target = mockImageViewTarget(); new RequestCreator(picasso, URI_1, 0).fit().resize(10, 10).into(target); fail("Calling into() ImageView with fit() and resize() should throw exception"); } catch (IllegalStateException ignored) { } }