@Test public void multiplePreDrawsOnlyTriggersOnce() { Picasso picasso = mock(Picasso.class); when(picasso.transformRequest(any(Request.class))).thenAnswer(TRANSFORM_REQUEST_ANSWER); RequestCreator creator = new RequestCreator(picasso, URI_1, 0); ImageView target = mockFitImageViewTarget(true); when(target.getWidth()).thenReturn(100); when(target.getHeight()).thenReturn(100); ViewTreeObserver observer = target.getViewTreeObserver(); DeferredRequestCreator request = new DeferredRequestCreator(creator, target); request.onPreDraw(); request.onPreDraw(); verify(observer).removeOnPreDrawListener(request); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); }
@Override public boolean onPreDraw() { ImageView target = this.target.get(); if (target == null) { return true; } ViewTreeObserver vto = target.getViewTreeObserver(); if (!vto.isAlive()) { return true; } int width = target.getWidth(); int height = target.getHeight(); if (width <= 0 || height <= 0 || target.isLayoutRequested()) { return true; } vto.removeOnPreDrawListener(this); this.target.clear(); this.creator.unfit().resize(width, height).into(target, callback); return true; }
DeferredRequestCreator implements ViewTreeObserver.OnPreDrawListener { @Override public boolean onPreDraw() { ImageView target = this.target.get(); if (target == null) { return true; } ViewTreeObserver vto = target.getViewTreeObserver(); if (!vto.isAlive()) { return true; } int width = target.getWidth(); int height = target.getHeight(); if (width <= 0 || height <= 0 || target.isLayoutRequested()) { return true; } vto.removeOnPreDrawListener(this); this.target.clear(); this.creator.unfit().resize(width, height).into(target, callback); return true; } }
DeferredRequestCreator implements ViewTreeObserver.OnPreDrawListener { @Override public boolean onPreDraw() { ImageView target = this.target.get(); if (target == null) { return true; } ViewTreeObserver vto = target.getViewTreeObserver(); if (!vto.isAlive()) { return true; } int width = target.getWidth(); int height = target.getHeight(); if (width <= 0 || height <= 0 || target.isLayoutRequested()) { return true; } vto.removeOnPreDrawListener(this); this.target.clear(); this.creator.unfit().resize(width, height).into(target, callback); return true; } @VisibleForTesting  DeferredRequestCreator(RequestCreator creator, ImageView target);   DeferredRequestCreator(RequestCreator creator, ImageView target, Callback callback); }
DeferredRequestCreator implements ViewTreeObserver.OnPreDrawListener { @Override public boolean onPreDraw() { ImageView target = this.target.get(); if (target == null) { return true; } ViewTreeObserver vto = target.getViewTreeObserver(); if (!vto.isAlive()) { return true; } int width = target.getWidth(); int height = target.getHeight(); if (width <= 0 || height <= 0 || target.isLayoutRequested()) { return true; } vto.removeOnPreDrawListener(this); this.target.clear(); this.creator.unfit().resize(width, height).into(target, callback); return true; } @VisibleForTesting  DeferredRequestCreator(RequestCreator creator, ImageView target);   DeferredRequestCreator(RequestCreator creator, ImageView target, Callback callback); @Override boolean onPreDraw(); }
DeferredRequestCreator implements ViewTreeObserver.OnPreDrawListener { @Override public boolean onPreDraw() { ImageView target = this.target.get(); if (target == null) { return true; } ViewTreeObserver vto = target.getViewTreeObserver(); if (!vto.isAlive()) { return true; } int width = target.getWidth(); int height = target.getHeight(); if (width <= 0 || height <= 0 || target.isLayoutRequested()) { return true; } vto.removeOnPreDrawListener(this); this.target.clear(); this.creator.unfit().resize(width, height).into(target, callback); return true; } @VisibleForTesting  DeferredRequestCreator(RequestCreator creator, ImageView target);   DeferredRequestCreator(RequestCreator creator, ImageView target, Callback callback); @Override boolean onPreDraw();  }