Picasso onBitmapLoaded从未调用过

时间:2014-09-22 13:11:05

标签: android android-drawable picasso

我遇到了同样的问题,我想使用Picasso为图像缓存目的生成的drawable,但我无法得到相同的结果。这是我用来访问Bitmap Drawables的代码:

Target targetBitmap = new Target() {

            @Override
            public void onPrepareLoad(Drawable arg0) {

            }

            @Override
            public void onBitmapLoaded(Bitmap arg0, Picasso.LoadedFrom arg1) {
                mBitmap = arg0;
                BitmapDrawable d = new BitmapDrawable(context.getResources(), arg0);

                int margin = 2;
                int border = 0;

                Rect r = new Rect(margin, margin, width - margin, height - margin);

                int imageWidth = r.width();
                int imageHeight = r.height();

                if (imageHeight > r.height() - (border * 2)) {
                    imageHeight = r.height() - (border * 2);
                }

                r.left += ((r.width() - imageWidth) / 2) - border;
                r.right = r.left + imageWidth + border + border;
                r.top += ((r.height() - imageHeight) / 2) + border;
                r.bottom = r.top + imageHeight + border + border;

                Paint p = new Paint();
                p.setColor(0xFFFFFF);

                c.drawRect(r, p);
                r.left += border;
                r.right -= border;
                r.top += border;
                r.bottom -= border;
                d.setBounds(r);
                d.draw(c);
            }

            @Override
            public void onBitmapFailed(Drawable arg0) {

            }
        };

        Picasso.with(context).load(app.getListBookDetails().get(img_num).getImage_150x225()).into(targetBitmap);

但是这里永远不会调用onBitmapLoaded方法。我该如何访问此方法?如果可以的话请帮忙。我已经抓住了一切,但一切都徒劳无功。

由于 萨纳特

2 个答案:

答案 0 :(得分:6)

以下是您希望阻止对象对象进行垃圾回收的示例代码:

 final Target mTarget = new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
                            Log.d("DEBUG", "onBitmapLoaded");
                            BitmapDrawable mBitmapDrawable = new BitmapDrawable(getResources(), bitmap);
                            navigationMenuItem.setIcon(mBitmapDrawable);
                        }

                        @Override
                        public void onBitmapFailed(Drawable drawable) {
                            Log.d("DEBUG", "onBitmapFailed");
                        }

                        @Override
                        public void onPrepareLoad(Drawable drawable) {
                            Log.d("DEBUG", "onPrepareLoad");
                        }
                    };


                    Picasso.with(this).load(tempUrl).into(mTarget);

答案 1 :(得分:0)

为了使用RxJava 2接下来使用Bitmap:

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)
    }
}

检索位图:

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