开发者

Android: showing default image in gallery's ImageView while actual image is downloaded

开发者 https://www.devze.com 2023-01-05 17:28 出处:网络
I believe this is pretty trivial but I can\'t get it to work. I want to display a default image in gallery elements (ImageViews) while their actual image is being fetched from the net.

I believe this is pretty trivial but I can't get it to work.

I want to display a default image in gallery elements (ImageViews) while their actual image is being fetched from the net.

Right now, nothing is shown for an ImageView which its image has yet to arrive. Once it arrives it is immediately shown.

What I tried is right after the instantiation of the ImageView to call its setImageResource function like so:

final ImageView i = new ImageView(mContext);

i.setImageResource(R.drawable.loading);

But it doesn't seem to work. Below is the full getView() function.

Any help is appreciated.

Thanks.

public View getView(int position, View convertView, ViewGroup parent) {

     final ImageView i = new ImageView(mContext);
     i.setImageResource(R.drawable.loading);


     // if the drawbale is in the buffer - fetch it from there
     Drawable bufferedImage = DataManager.getInstance().getImagesBuffer()[position];
     if (bufferedImage != null){
         i.setImageDrawable(bufferedImage);

         BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
         drawable.setAntiAlias(true);
     }
     // if drawable is not in buffer - fetch it from the net via AsyncImageLoader
     else
     {

         String imageUrl = DataManager.getInstance().getImageBufferInstance().getImageUrl(position);
         Drawable downloadedImage = AsyncImageLoader.getInstance().loadDrawable(imageUrl, new ImageCallback() {
         public void imageLoaded(Drawable imageDrawable, String imageUrl) {

                if (imageDrawable == null)
                {
                    imageDrawable = getResources().getDrawable(R.drawable.icon);
                }
                i.setImageDrawable(imageDrawable);

                BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
                drawable.setAntiAlias(true);    

                }
            });

         i.setImageDrawable(downloadedImage);
     }

     i.setLayoutParams(new CoverFlow.LayoutParams(Utils.getInst开发者_StackOverflow中文版ance().getScreenWidth() / 2,
             Utils.getInstance().getScreenHeight() / 2));
     i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 


     return i;


 }


Fix your indentation... If I'm reading that correctly, you're only setting the temporary drawable icon in the imageLoaded callback of your (not shown) AsyncImageLoader, which I assume means it's then only being set after the image downloads and is then immediately overwritten with the downloaded image. Try moving the placeholder-setting code into your else block outside the callback.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号