Android - 即使已加载图像,也不会显示图像

时间:2013-04-05 01:22:51

标签: android android-layout android-imageview

我设置了我的imageview layout width to a fix valuemy layout height to wrapcontent。 imageview的图像通过url加载。发生的事情是,当我的视图被绘制时,由于图像尚未加载,它只会绘制layout_width。但是当图像已经加载时,不再绘制高度,因此不会显示图像。只有当我来回滚动时才会显示它,因为我认为这是重绘视图的时间。有没有办法可以解决这个问题?使用invalidate()无效。也许我做错了吗?

我只会发布我的代码片段: 这是扩展BaseAdapter的 MyAdapter getView

 public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder holder = null;

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    Typeface typeFaceMyriadReg = Typeface.createFromAsset(context.getAssets(), MYRIAD_REG);


    if(convertView == null){
        convertView = inflater.inflate(R.layout.shop_list_layout, null);
        holder = new ViewHolder();
        holder.txtShopName = (TextView) convertView.findViewById(R.id.name);
        holder.txtShopName.setTypeface(typeFaceMyriadReg);
        holder.txtShopDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtShopDesc.setTypeface(typeFaceMyriadReg);
        holder.imageView = (ImageView) convertView.findViewById(R.id.prof_pic);
        holder.progBar = (ProgressBar) convertView.findViewById(R.id.img_progress);



        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
        imageLoader=new ImageLoader(context.getApplicationContext(), holder.progBar);

    ShopRowItem shopItem = (ShopRowItem) getItem(position);

    holder.txtShopName.setText(shopItem.getShopName());
    holder.txtShopDesc.setText(shopItem.getShopDesc());
    imageLoader.DisplayImage(shopItem.getImageUrl(), holder.imageView);

    return convertView;
}

然后是ImageLoder的一部分:

  public void DisplayImage(String url, ImageView imageView)
{
    imageViews.put(imageView, url);
    Bitmap bitmap=memoryCache.get(url);
    if(bitmap!=null){
        imageView.setImageBitmap(bitmap);
        progressBar.setVisibility(View.VISIBLE);
    }
    else
    {
        queuePhoto(url, imageView);
        progressBar.setVisibility(View.VISIBLE);

    }
} 

// ...
// ...

    //Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable
{
    Bitmap bitmap;
    PhotoToLoad photoToLoad;
    public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
    public void run()
    {
        if(imageViewReused(photoToLoad))
            return;
        if(bitmap!=null){

            //should i do recalculations and adjustments here? ? ?

            photoToLoad.imageView.setImageBitmap(bitmap);
            progressBar.setVisibility(View.GONE);
        }   
        else{
                photoToLoad.imageView.setImageResource(stub_id);
                progressBar.setVisibility(View.VISIBLE);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以通过在imageview中放置一些加载图片来解决此问题,直到从服务器获取图像数据为止。

public void DisplayImage(String url, ImageView imageView)
{
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);
if(bitmap!=null){
    imageView.setImageBitmap(bitmap);
    progressBar.setVisibility(View.VISIBLE);
}
else
{
    queuePhoto(url, imageView);
//add this line of code here.....
    imageView.setImageResource(R.drawable.your_image_placeholder);
    progressBar.setVisibility(View.VISIBLE);

}
}