毕加索第一次运行不调用onBitmapLoaded for循环 [英] Picasso first run not call onBitmapLoaded in for loop

查看:116
本文介绍了毕加索第一次运行不调用onBitmapLoaded for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取位图在循环中使用Picasso时遇到问题.

I have problem with get bitmap use Picasso in for loop.

首次运行中未将其称为onBitmapLoaded.

it is not called onBitmapLoaded in first run.

第二次运行,它称为

for (int i = 0; i < 3; i++) { 
            final int k=i;
            Picasso.with(this)
                    .load(ListA.get(i).getImage()) //image
                    .resize(100, 100)
                    .transform(new ImageTrans_CircleTransform())
                    .into(new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {                         
                            Log.i("load", "Ok " + k);   
                           //use bitmap for add marker to map                
                        }

                        @Override
                        public void onBitmapFailed(Drawable errorDrawable) {
                        }

                        @Override
                        public void onPrepareLoad(Drawable placeHolderDrawable) {
                            Log.i("load", "first " + k);
                        }
                    });  
        }

日志

11-04 16:42:22.222  11677-11677/com.tenten I/load﹕ first___0 
11-04 16:42:22.222  11677-11677/com.tenten I/load﹕ first___1 
11-04 16:42:22.232  11677-11677/com.tenten I/load﹕ first___2 

我使用毕加索从列表图像中获取位图.

I use picasso to get bitmap from list image.

我需要位图而不是imageview.

谢谢. :D

推荐答案

使用毕加索的目标"的一个常见问题是人们没有对其进行严格的引用.这导致目标是随机工作的,因为有时它们在完成之前就被GC收集了,有时它们的寿命足够长,可以调用回调函数.

A common issue with using Picasso's Targets is that people don't keep strong references to them. This results in the targets being randomly working, because sometimes they get collected by the GC before being finished, and sometimes they live long enough to get the callback called.

您要做的是将这些回调存储在某个地方,直到完成.这是一个示例:

What you have to do is to store those callbacks somewhere until they are finished. Here's an example:

final List<Target> targets = new ArrayList<Target>();
for (int i = 0; i < 3; i++) { 
    final int k=i;
    Target target = new Target() {

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {                         
            Log.i("Targets", "Loaded: " + k);   
            targets.remove(this);                
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            targets.remove(this);
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            Log.i("Targets", "Preparing: " + k);
        }
    }
    targets.add(target);
    Picasso.with(this)
        .load(ListA.get(i).getImage()) // Start loading the current target
        .resize(100, 100)
        .into(target);  
}

要确保列表也不会得到GC,请将targets设置为全局变量.

To make sure the list does not get GC'd too, make targets a global variable.

这篇关于毕加索第一次运行不调用onBitmapLoaded for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆