毕加索第一次不调用onBitmapLoaded

时间:2018-11-02 08:37:07

标签: android picasso

毕加索第一次不打onBitmapLoaded,如果您知道,请告诉我

        txtView = (TextView) centerRelative.getChildAt(i);
                Picasso.with(getBaseContext()).load(file[i-4]).into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        txtView.setBackground(new BitmapDrawable(getResources(),bitmap));
//                      Not executing for the first time

                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {
//                      executing for the first time
                    }
                });

Target target = new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        txtView.setBackground(new BitmapDrawable(getResources(),bitmap));
                        logd("onBitmapLoaded");
                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                };
                Picasso.with(getBaseContext()).load(file[i-4]).into(target);

1 个答案:

答案 0 :(得分:1)

解决方案:您必须在此处进行一些更改:

代替写作:

new Target() {...}

在您的into(..)内,您必须创建一个Target类的全局对象。不要将其设为本地对象,因为它可能会被垃圾回收。所以,

第1步:

制作全局对象:

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) {
        ....
    }
};

最后,在您的into(...)中使用该目标:

Picasso.with(getBaseContext()).load(file[i-4]).into(target);

尝试一下,希望对您有所帮助。