【问题标题】:Image caching not in List View android图像缓存不在列表视图android中
【发布时间】:2012-05-22 15:13:16
【问题描述】:

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

我正在搜索如何在 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 进行图像缓存。但是如果我不使用列表视图该怎么办。

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

谢谢。

【问题讨论】:

    标签: java android image caching


    【解决方案1】:

    你必须在父视图上调用 invalidate

        pubLayout.getParent().invalidate();
    

    【讨论】:

    • 我什么时候应该调用这个?而我的线性布局的 publyaout 没有 invalidate();只是 invalidateChild(child, r) 和 invalidateChildInParent(location, r)
    • 这听起来很奇怪。我的 LinearLayouts 确实无效。 :\您使用的是哪个版本的Android...
    • 这不是我想要的,因此接受答案是错误的,因为其他人不会觉得它有用。我猜
    • 感谢您的帮助。抱歉耽误您的时间
    猜你喜欢
    • 1970-01-01
    • 2013-05-14
    • 2021-09-20
    • 2011-10-13
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    相关资源
    最近更新 更多