如何等待 Picasso 完成加载 Android 中的图像?

How to wait Picasso to finish load Images in Android?

所以,我有一个 activity,比方说一个 PetDetailActivity,它显示了一个带有一些位图的旋转木马(我使用 com.synnapps:carouselview:0.1.5 来处理我的旋转木马)。问题是 PetDetailActivity 加载了 0 大小的旋转木马,这可能是图像仍在由线程处理。如何等待 Picasso 处理完 URLs,然后在新的 Activity 中显示出来?

这是PetDetail的代码Activity:

import ...
public class PetDetailActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pet_detail);

        Intent i = getIntent();
        Pet targetPet = (Pet)i.getSerializableExtra("PetObject");
        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null) actionBar.setDisplayHomeAsUpEnabled(true);

        //Creating a BitmapHandler object to download image from URL to Bitmap object using picasso.
        BitmapHandler bitmapHandler = new BitmapHandler(targetPet.getCarouselImageUrl());
        final ArrayList<Bitmap> petCarouselBitmaps = bitmapHandler.getProcessedBitmap();

        //The bitmap is being downloaded in other thread, so the activity is up and 
        //CarouselView is still empty (petCarouselBitmaps.size() == 0)
        //So how to wait the bitmaps is processed, like show a loading screen on the UI?

        CarouselView petCarousel = findViewById(R.id.petCarousel);
        petCarousel.setPageCount(petCarouselBitmaps.size());
        petCarousel.setImageListener(new ImageListener() {
            @Override
            public void setImageForPosition(int position, ImageView imageView) {
                imageView.setImageBitmap(petCarouselBitmaps.get(position));
            }
        });
    }
...
}

这里是 BitmapHandler Class,它使用 picasso 从 URL 下载图像到位图:

public class BitmapHandler extends Thread {
    ArrayList<String> urlList;
    private ArrayList<Bitmap> loadedBitmap;

    public BitmapHandler(ArrayList<String> list){
        this.urlList = list;
        this.loadedBitmap = new ArrayList<>();
    }

    public ArrayList<Bitmap> getProcessedBitmap(){
        this.run();
        //Returning the loaded bitmap as a ArrayList<Bitmap> Object.
        return loadedBitmap;
    }

    @Override
    public void run() {
        Target target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                loadedBitmap.add(bitmap);
            }
            @Override
            public void onBitmapFailed(Exception e, Drawable errorDrawable) {}
            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {}
        };

        for (String url : urlList) {
            Picasso.get().load(url).into(target);
        }
    }
}

感谢您的帮助!

我认为您可以使用占位符,然后加载图像,它将显示在图像视图中。 如果你想延迟使用可以使用 Thread.sleep(5000).

问题:。如何等待 Picasso 完成 URL 处理

解决方案:

我认为你可以使用 Target 回调:

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

加载图片时,需要写:

Picasso.with(this).load(myURL).into(target);

仅供参考:

  • onBitmapLoaded()主要用于在我们实际加载到视图之前执行图像操作。
  • Picasso.LoadedFrom 描述图像的加载位置,无论是 MEMORY、DISK 还是 NETWORK。