Exemplo n.º 1
0
  /**
   * 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) {
    long started = System.nanoTime();
    if (picasso.checkMainThreadsEnabled) {
      checkMain();
    }

    if (target == null) {
      throw new IllegalArgumentException("Target must not be null.");
    }
    if (deferred) {
      throw new IllegalStateException("Fit cannot be used with a Target.");
    }

    if (!data.hasImage()) {
      picasso.cancelRequest(target);
      target.onPrepareLoad(setPlaceholder ? getPlaceholderDrawable() : null);
      return;
    }

    Request request = createRequest(started);
    String requestKey = createKey(request);

    if (shouldReadFromMemoryCache(memoryPolicy)) {
      Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);
      if (bitmap != null) {
        picasso.cancelRequest(target);
        target.onBitmapLoaded(bitmap, MEMORY);
        return;
      }
    }

    target.onPrepareLoad(setPlaceholder ? getPlaceholderDrawable() : null);

    Action action =
        new TargetAction(
            picasso,
            target,
            request,
            memoryPolicy,
            networkPolicy,
            errorDrawable,
            requestKey,
            tag,
            errorResId);
    picasso.enqueueAndSubmit(action);
  }
    private void loadImage(Track track, ImageView view) {
      Context appContext = view.getContext().getApplicationContext();
      Picasso picasso = Picasso.with(appContext);
      picasso.cancelRequest(view);

      if (track.album != null && !track.album.images.isEmpty()) {
        String url = track.album.images.get(0).url;
        picasso
            .load(url)
            .fit()
            .centerCrop()
            .placeholder(R.drawable.ic_av_equalizer)
            .error(R.drawable.ic_av_equalizer)
            .into(view);
      } else {
        picasso.load(R.drawable.ic_av_equalizer).fit().centerCrop().noPlaceholder().into(view);
      }
    }
Exemplo n.º 3
0
  /**
   * 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) {
    long started = System.nanoTime();
    if (picasso.checkMainThreadsEnabled) {
      checkMain();
    }

    if (target == null) {
      throw new IllegalArgumentException("Target must not be null.");
    }

    if (!data.hasImage()) {
      picasso.cancelRequest(target);
      if (setPlaceholder) {
        setPlaceholder(target, getPlaceholderDrawable());
      }
      return;
    }

    if (deferred) {
      if (data.hasSize()) {
        throw new IllegalStateException("Fit cannot be used with resize.");
      }
      int width = target.getWidth();
      int height = target.getHeight();
      if (width == 0 || height == 0) {
        if (setPlaceholder) {
          setPlaceholder(target, getPlaceholderDrawable());
        }
        picasso.defer(target, new DeferredRequestCreator(this, target, callback));
        return;
      }
      data.resize(width, height);
    }

    Request request = createRequest(started);
    String requestKey = createKey(request);

    if (shouldReadFromMemoryCache(memoryPolicy)) {
      Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);
      if (bitmap != null) {
        picasso.cancelRequest(target);
        setBitmap(target, picasso.context, bitmap, MEMORY, noFade, picasso.indicatorsEnabled);
        if (picasso.loggingEnabled) {
          log(OWNER_MAIN, VERB_COMPLETED, request.plainId(), "from " + MEMORY);
        }
        if (callback != null) {
          callback.onSuccess();
        }
        return;
      }
    }

    if (setPlaceholder) {
      setPlaceholder(target, getPlaceholderDrawable());
    }

    Action action =
        new ImageViewAction(
            picasso,
            target,
            request,
            memoryPolicy,
            networkPolicy,
            errorResId,
            errorDrawable,
            requestKey,
            tag,
            callback,
            noFade);

    picasso.enqueueAndSubmit(action);
  }