如何在GridView中有效地加载网络图片吗? [英] how to load internet images in gridview efficiently?

查看:248
本文介绍了如何在GridView中有效地加载网络图片吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的例子在我的活动中显示网络图片。

I am using following example to display internet images in my activity.

http://developer.android.com/resources/tutorials/意见/ HELLO-gridview.html

在自定义图像适配器我直接载入从互联网上的图像,并将其分配给ImageView的。

In custom image adapter I'm directly loading images from internet and assigning it to imageview.

这显示图像GridView和每一件事工作正常,但它是没有效率的方法。

Which shows images in gridview and every thing works fine but it is not efficient way.

当我过GridView的滚动一次又一次加载图像,这就是为什么gridview的滚动很慢

When ever i scroll gridview it again and again loads images and thats why gridview scrolls very slow

是否有缓存或一些可用的,使其更快有用的技术?

Is there caching or some useful technique available to make it faster?

推荐答案

创建返回一个位图的全局和静态方法。该方法将参数:上下文 IMAGEURL imageName

Create a global and static method which returns a Bitmap. This method will take parameters: context,imageUrl, and imageName.

在方法:

  1. 检查文件是否已经存在于缓存中。如果是这样,则返回位图

  1. check if the file already exists in the cache. if it does, return the bitmap

    if(new File(context.getCacheDir(), imageName).exists())
        return BitmapFactory.decodeFile(new File(context.getCacheDir(), imageName).getPath());

  • 否则,您必须从网络上加载图像,并将其保存到缓存:

  • otherwise you must load the image from the web, and save it to the cache:

    image = BitmapFactory.decodeStream(HttpClient.fetchInputStream(imageUrl));
    
    
    
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(new File(context.getCacheDir(), imageName));
    }
    
    
    //this should never happen
    catch(FileNotFoundException e) {
        if(Constants.LOGGING)
            Log.e(TAG, e.toString(), e);
    }
    
    
    //if the file couldn't be saved
    if(!image.compress(Bitmap.CompressFormat.JPEG, 100, fos)) {
        Log.e(TAG, "The image could not be saved: " + imageName + " - " + imageUrl);
        image = BitmapFactory.decodeResource(context.getResources(), R.drawable.default_cached_image);
    }
    fos.flush();
    fos.close();
    
    
    return image;
    

  • preLOAD一个矢量< SoftReference<位图>> 所有使用上述方法中的位图对象的AsyncTask 类,另一种列表持有地图 imageUrls和imageNames(供以后访问的时候,你需要重新加载图像),然后将你的的GridView 适配器。

    preload a Vector<SoftReference<Bitmap>> object with all of the bitmaps using the method above in an AsyncTask class, and also another List holding a Map of imageUrls and imageNames(for later access when you need to reload an image), then set your GridView adapter.

    我推荐使用 SoftReferences 的阵列,以减少内存使用量。如果你有一个巨大的位图,你很可能会遇到内存问题的数组。

    i recommend using an array of SoftReferences to reduce the amount of memory used. if you have a huge array of bitmaps you're likely to run into memory problems.

    所以在你的 getView 方法,你可能有这样的事情(其中图标向量保持型 SoftReference&LT;位图&GT;

    so in your getView method, you may have something like(where icons is a Vector holding type SoftReference<Bitmap>:

    myImageView.setImageBitmap(icons.get(position).get());
    

    您需要做的检查:

    if(icons.get(position).get() == null) {
        myImageView.setImageBitmap(defaultBitmap);
        new ReloadImageTask(context).execute(position);
    }
    

    ReloadImageTask 的AsyncTask 类,只需拨打上面用正确的PARAMS创建的全局方法,那么 notifyDataSetChanged onPostExecute

    in the ReloadImageTask AsyncTask class, simply call the global method created from above with the correct params, then notifyDataSetChanged in onPostExecute

    一些额外的工作可能需要完成,以确保不启动时,它已经为特定项目运行此AsyncTask的

    some additional work may need to be done to ensure you don't start this AsyncTask when it is already running for a particular item

    这篇关于如何在GridView中有效地加载网络图片吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆