0

大家好,我被图像缓存困住了。我需要使用来自网络的图像填充线性布局。

我正在搜索如何在 android 中缓存图像的示例,并找到了这些示例:

我尝试为我的代码实现它。

    String picPath = "https://www.google.lt/logos/classicplus.png";

    try {
        /*
        View v = null;//new View(this);

        ImageLoader loader = new ImageLoader(this);
        ImageView im = (ImageView)this.findViewById(R.id.image);

        loader.DisplayImage(picPath, im);
        addImage( im);*/

          LayoutInflater inflater =(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           View convertView = inflater.inflate(R.layout.item, null);

        ImageView image = (ImageView)convertView.findViewById(R.id.image);
        ImageDownloader down = new ImageDownloader();
        down.download(picPath, image);
        addImage(image);

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    public void addImage( View view){

        LinearLayout pubLayout = (LinearLayout)findViewById( R.id.scrollerLinearlayout);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                 LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        //setting layout_margin
        params.setMargins(15, 30, 15, 30);
        pubLayout.addView(view,params);

}

然而这并没有奏效。我相信是因为我将视图添加到线性布局并且下载图像时无法重新绘制它?

有一些示例如何使用 ListView 进行图像缓存。但是如果我不使用列表视图该怎么办。

我会编写自己的缓存,但是如何对下载的图像进行回调,因为我想首先将图像添加到布局并在回调时更新它?

谢谢。

4

1 回答 1

0

你必须在父视图上调用无效

    pubLayout.getParent().invalidate();
于 2012-05-26T18:38:16.577 回答