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

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


这是 PetDetailActivity 的代码:


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

            }

        });

    }

...

}


守着一只汪
浏览 95回答 2
2回答

小唯快跑啊

问题: 。如何等待 Picasso 完成 URL 处理解决方案:我认为你可以使用 Target 回调:private Target target = new Target() {&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void onBitmapFailed() {&nbsp; &nbsp; &nbsp; }}在加载图像时,您需要编写:Picasso.with(this).load(myURL).into(target);仅供参考:onBitmapLoaded()主要用于在实际加载到视图之前执行图像操作。Picasso.LoadedFrom描述图像从何处加载,无论是内存、磁盘还是网络。

凤凰求蛊

我认为您可以使用占位符,然后加载图像,它将显示在图像视图中。而如果你想延迟使用可以使用Thread.sleep(5000).
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java