【问题标题】:Android: showing default image in gallery's ImageView while actual image is downloadedAndroid:在下载实际图像时在画廊的 ImageView 中显示默认图像
【发布时间】:2010-07-06 22:57:43
【问题描述】:

我相信这很简单,但我无法让它发挥作用。
我想在画廊元素 (ImageViews) 中显示默认图像,同时从网络获取它们的实际图像。
现在,没有显示任何图像尚未到达的 ImageView。到达后立即显示。
我尝试的是在 ImageView 实例化之后调用它的 setImageResource 函数,如下所示:

最终的 ImageView i = new ImageView(mContext);
i.setImageResource(R.drawable.loading);

但它似乎不起作用。下面是完整的 getView() 函数。
任何帮助表示赞赏。
谢谢。

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.getInstance().getScreenWidth() / 2,
             Utils.getInstance().getScreenHeight() / 2));
     i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 


     return i;


 }

【问题讨论】:

    标签: android image-gallery


    【解决方案1】:

    修复您的缩进...如果我没看错,您只是在(未显示)AsyncImageLoader 的 imageLoaded 回调中设置临时可绘制图标,我认为这意味着它仅在图像之后设置下载,然后立即被下载的图像覆盖。尝试将占位符设置代码移动到回调之外的 else 块中。

    【讨论】:

    • Yoni,非常感谢您调查它(如果我的缩进使代码不清楚......)。您的洞察力帮助我理解了变量 updatedImage 为空,并且我在 i.setImageDrawable(downloadedImage) 中将 imageView i 设置为空。这就是为什么我在 imageView 中看不到任何东西的原因。一旦回调开始,它将 i 设置为实际图像并出现在屏幕上。而不是做 i.setImageDrawable(downloadedImage) 我应该问下载的图像是否为空。如果是这样,请将 i 设置为默认图像。如果没有,请使用下载的图像进行设置。再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多