使用Picasso通过Bitmap获取回调

Ste*_*e M 81 android bitmap picasso

我正在使用Picasso为我的应用下载图像.

我遇到的情况是我需要Bitmap在加载到第一个之前访问第一个ImageView.Downloader.Response该类的存在似乎表明这是可能的,但我找不到任何使用示例.我不想写一堆更多的代码来异步处理这个特殊情况,如果它可以与Picasso一起使用的话.

谁能告诉我怎么做?

Ste*_*e M 171

如果有人想知道,在github上找到答案:

private Target target = new Target() {
      @Override
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
      }

      @Override
      public void onBitmapFailed(Drawable errorDrawable) {
      }

      @Override
      public void onPrepareLoad(Drawable placeHolderDrawable) {
      }
}

private void someMethod() {
   Picasso.with(this).load("url").into(target);
}

@Override 
public void onDestroy() {  // could be in onPause or onStop
   Picasso.with(this).cancelRequest(target);
   super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)

帖子建议不要使用匿名回调,而是使用实例变量作为目标.

  • 理想情况下,您可以直接在视图或视图持有者对象上实现`Target`.如果您不这样做,则需要在某处保持对实例的强引用,否则将收集垃圾. (29认同)
  • onPitmapLoaded在onPrepareLoad之后第一次没有调用 (8认同)

Abd*_*ؤمن 62

取自这里:

Picasso.with(this)
    .load(url)
    .into(new Target() {
        @Override
        public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
            /* Save the bitmap or do something with it here */

            //Set it in the ImageView
            theView.setImageBitmap(bitmap); 
        }
});
Run Code Online (Sandbox Code Playgroud)

更新(2016年5月4日):

            Picasso.with(this)
                .load(youUrl)
                .into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                });
Run Code Online (Sandbox Code Playgroud)

更新(2016年11月22日)

或使用强引用,Target以便它不会被垃圾收集

Target target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        };


void foo() {
        Picasso.with(getContext()).load(getUrl()).into(target);
}
Run Code Online (Sandbox Code Playgroud)

科特林

object: com.squareup.picasso.Target {
                  override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) {
                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                }

                  override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                  }

                  override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {

                  }
                }
Run Code Online (Sandbox Code Playgroud)

  • 匿名目标可能是垃圾收集 (5认同)

Ole*_*ich 6

什么比下一个容易:

val url: String = "https://...."
val bitmap: Bitmap = Picasso.with(context).load(url).get()
Run Code Online (Sandbox Code Playgroud)

应该从不是主线程调用!

或使用RxJava 2:

fun getBitmapSingle(picasso: Picasso, url: String): Single<Bitmap> = Single.create {
    try {
        if (!it.isDisposed) {
            val bitmap: Bitmap = picasso.load(url).get()
            it.onSuccess(bitmap)
        }
    } catch (e: Throwable) {
        it.onError(e)
    }
}
Run Code Online (Sandbox Code Playgroud)

检索位图:

getBitmapSingle(Picasso.with(context), "https:/...")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ bitmap ->
                // val drawable = BitmapDrawable(context, bitmap)
                }, Throwable::printStackTrace)
Run Code Online (Sandbox Code Playgroud)

我使用了毕加索v.2.5.2