ChangUZ
ChangUZ

Reputation: 5438

How can I load asynchronous url image ? (libgdx)

    final Handler downloadHandler =  new Handler() {
        public void handleMessage(Message msg) {
            Log.d("Yoon", "Handler");
            MCImageDisposableView iconView = new MCImageDisposableView(profileSprite, tag);
            iconView.setScale(0.5f);
            iconView.setPosition(17, 145);
            addActor(iconView);
        }
    };

    Thread sb = new Thread(new Runnable() {
        @Override
        public void run() {
            Log.d("Yoon", "Thread");
            profileSprite = BitmapDownloader.downloadSprite(Utils.getImageURL(MyChoiceApp.shared.context, aid));
            downloadHandler.sendEmptyMessage(0);
        }
    });

    sb.start();

If use thread without handler, change actor(MCImageDisposableView has sprite) image.

But image is not correct. image is not loaded my url. image is loaded texture atlas

MCImageDisposableView class have donwload image function. It works good... if not use thread.

How can I load asynchronous image ?

Upvotes: 4

Views: 3065

Answers (2)

Nolesh
Nolesh

Reputation: 7028

I have created MyLazyImageList for this purpose. It's based on ScrollPane. Now it looks like below:

enter image description here

You can modify the code if you need. So the source code is here: http://pastebin.com/JPWTB9PF or here: http://pastebin.com/hi0v0nQy

Upvotes: 1

AndoAiron
AndoAiron

Reputation: 694

You need to implement lazyloading for this.

Read the below mentioned link for lazyloading :

Lazy load of images in ListView

Here is the basic idea for doing this :

addActor(){
   //if you need to download image from url
   downloadImageFromUrl(actorImageView, url);
}

downloadIamgeFromUrl(ImageView actorImageView, String url){
    //ToDo : create Thread to download image from server
    //ToDo : Once done, set this image to actorImageView  in UI thread as
    //runOnUiThread(new Runnable() {
        @Override
        public void run() {
            actorImageView  .setImageBitmap(bitmap);
        }
    });
}

Upvotes: 1

Related Questions