/** * 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); } }
private void performRemoteViewInto(RemoteViewsAction action) { if (!skipMemoryCache) { Bitmap bitmap = picasso.quickMemoryCacheCheck(action.getKey()); if (bitmap != null) { action.complete(bitmap, MEMORY); return; } } if (placeholderResId != 0) { action.setImageResource(placeholderResId); } 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); }