/** * Asynchronously fulfills the request into the specified {@link RemoteViews} object with the * given {@code viewId}. This is used for loading bitmaps into all instances of a widget. */ public void into(RemoteViews remoteViews, int viewId, int[] appWidgetIds) { if (remoteViews == null) { throw new IllegalArgumentException("RemoteViews must not be null."); } if (appWidgetIds == null) { throw new IllegalArgumentException("appWidgetIds must not be null."); } if (deferred) { throw new IllegalStateException("Fit cannot be used with RemoteViews."); } if (placeholderDrawable != null || errorDrawable != null) { throw new IllegalArgumentException( "Cannot use placeholder or error drawables with remote views."); } Request finalData = picasso.transformRequest(data.build()); String key = createKey(finalData); RemoteViewsAction action = new AppWidgetAction( picasso, finalData, remoteViews, viewId, appWidgetIds, skipMemoryCache, errorResId, key); performRemoteViewInto(action); }
/** * Asynchronously fulfills the request into the specified {@link ImageView} and invokes the target * {@link Callback} if it's not {@code null}. * * <p><em>Note:</em> The {@link Callback} param is a strong reference and will prevent your {@link * android.app.Activity} or {@link android.app.Fragment} from being garbage collected. If you use * this method, it is <b>strongly</b> recommended you invoke an adjacent {@link * Picasso#cancelRequest(android.widget.ImageView)} call to prevent temporary leaking. */ public void into(ImageView target, Callback callback) { if (target == null) { throw new IllegalArgumentException("Target must not be null."); } if (!data.hasImage()) { picasso.cancelRequest(target); PicassoDrawable.setPlaceholder(target, placeholderResId, placeholderDrawable); return; } if (deferred) { if (data.hasSize()) { throw new IllegalStateException("Fit cannot be used with resize."); } int measuredWidth = target.getMeasuredWidth(); int measuredHeight = target.getMeasuredHeight(); if (measuredWidth == 0 || measuredHeight == 0) { PicassoDrawable.setPlaceholder(target, placeholderResId, placeholderDrawable); picasso.defer(target, new DeferredRequestCreator(this, target, callback)); return; } data.resize(measuredWidth, measuredHeight); } Request finalData = picasso.transformRequest(data.build()); String requestKey = createKey(finalData); if (!skipMemoryCache) { Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey); if (bitmap != null) { picasso.cancelRequest(target); PicassoDrawable.setBitmap( target, picasso.context, bitmap, MEMORY, noFade, picasso.indicatorsEnabled); if (callback != null) { callback.onSuccess(); } return; } } PicassoDrawable.setPlaceholder(target, placeholderResId, placeholderDrawable); Action action = new ImageViewAction( picasso, target, finalData, skipMemoryCache, noFade, errorResId, errorDrawable, requestKey, callback); picasso.enqueueAndSubmit(action); }
/** * Asynchronously fulfills the request without a {@link ImageView} or {@link Target}. This is * useful when you want to warm up the cache with an image. */ public void fetch() { if (deferred) { throw new IllegalStateException("Fit cannot be used with fetch."); } if (data.hasImage()) { Request finalData = picasso.transformRequest(data.build()); String key = createKey(finalData); Action action = new FetchAction(picasso, finalData, skipMemoryCache, key); picasso.enqueueAndSubmit(action); } }
/** * Asynchronously fulfills the request into the specified {@link Target}. In most cases, you * should use this when you are dealing with a custom {@link android.view.View View} or view * holder which should implement the {@link Target} interface. * * <p>Implementing on a {@link android.view.View View}: * * <blockquote> * * <pre> * public class ProfileView extends FrameLayout implements Target { * {@literal @}Override public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) { * setBackgroundDrawable(new BitmapDrawable(bitmap)); * } * * {@literal @}Override public void onBitmapFailed() { * setBackgroundResource(R.drawable.profile_error); * } * * {@literal @}Override public void onPrepareLoad(Drawable placeHolderDrawable) { * frame.setBackgroundDrawable(placeHolderDrawable); * } * } * </pre> * * </blockquote> * * Implementing on a view holder object for use inside of an adapter: * * <blockquote> * * <pre> * public class ViewHolder implements Target { * public FrameLayout frame; * public TextView name; * * {@literal @}Override public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) { * frame.setBackgroundDrawable(new BitmapDrawable(bitmap)); * } * * {@literal @}Override public void onBitmapFailed() { * frame.setBackgroundResource(R.drawable.profile_error); * } * * {@literal @}Override public void onPrepareLoad(Drawable placeHolderDrawable) { * frame.setBackgroundDrawable(placeHolderDrawable); * } * } * </pre> * * </blockquote> * * <p><em>Note:</em> This method keeps a weak reference to the {@link Target} instance and will be * garbage collected if you do not keep a strong reference to it. To receive callbacks when an * image is loaded use {@link #into(android.widget.ImageView, Callback)}. */ public void into(Target target) { if (target == null) { throw new IllegalArgumentException("Target must not be null."); } if (deferred) { throw new IllegalStateException("Fit cannot be used with a Target."); } Drawable drawable = placeholderResId != 0 ? picasso.context.getResources().getDrawable(placeholderResId) : placeholderDrawable; if (!data.hasImage()) { picasso.cancelRequest(target); target.onPrepareLoad(drawable); return; } Request finalData = picasso.transformRequest(data.build()); String requestKey = createKey(finalData); if (!skipMemoryCache) { Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey); if (bitmap != null) { picasso.cancelRequest(target); target.onBitmapLoaded(bitmap, MEMORY); return; } } target.onPrepareLoad(drawable); Action action = new TargetAction( picasso, target, finalData, skipMemoryCache, errorResId, errorDrawable, requestKey); picasso.enqueueAndSubmit(action); }
/** * Synchronously fulfill this request. Must not be called from the main thread. * * <p><em>Note</em>: The result of this operation is not cached in memory because the underlying * {@link Cache} implementation is not guaranteed to be thread-safe. */ public Bitmap get() throws IOException { checkNotMain(); if (deferred) { throw new IllegalStateException("Fit cannot be used with get."); } if (!data.hasImage()) { return null; } Request finalData = picasso.transformRequest(data.build()); String key = createKey(finalData, new StringBuilder()); Action action = new GetAction(picasso, finalData, skipMemoryCache, key); return forRequest( picasso.context, picasso, picasso.dispatcher, picasso.cache, picasso.stats, action, picasso.dispatcher.downloader) .hunt(); }
@Test public void onGlobalLayoutSubmitsRequestAndCleansUp() throws Exception { 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(); verify(observer).removeOnPreDrawListener(request); verify(picasso).enqueueAndSubmit(actionCaptor.capture()); Action value = actionCaptor.getValue(); assertThat(value).isInstanceOf(ImageViewAction.class); assertThat(value.getRequest().targetWidth).isEqualTo(100); assertThat(value.getRequest().targetHeight).isEqualTo(100); }