如何通过包含的网址到图像源imageadapter网格视图数组? [英] How to pass an array containing urls to an image source to imageadapter of the grid view?

查看:93
本文介绍了如何通过包含的网址到图像源imageadapter网格视图数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何传递包含URL作为图像源,以网格视图的imageadapter数组? 我有一个工作形象适配器。但问题是适配器的getView方法返回只有一个imageview的。有没有人告诉如何通过包含的网址到GridView的图像适配器的阵列?该getview方法是这里

 公开查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
    ImageView的ImageView的= NULL;

        如果(convertView == NULL){//如果它不回收,初始化一些属性
            ImageView的=新ImageView的(mContext);
            imageView.setLayoutParams(新GridView.LayoutParams(85,85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8,8,8,8);
        } 其他 {
            ImageView的=(ImageView的)convertView;
        }

    // BMP位= loadBitmap(URL [I]);
        BMP位= loadBitmap(http://www.fgj.com/aero/planes/boeing/boeingf15.jpg);
        的System.out.println(适配器:+网址[I]);
        imageView.setImageBitmap(BMP);

    //imageView.setImageResource(mThumbIds[position]);
    // BMP位= loadBitmap(http://www.fgj.com/aero/planes/boeing/boeingf15.jpg);
    返回ImageView的;
}
 

解决方案

不幸的是,这是的的难度比它需要。你需要做的概述是:

  1. 收集你需要获取的URL。
  2. 创建一个AsyncTask的抓取每一个反过来的的使用AsyncTasks池的线程池的方式来服务的URL。某处你需要跟踪这些网址属于哪个位置,你的适配器视图这样你就可以选择无效行作为自己的形象进来了。
  3. 缓存你回到SD卡或内存中的图片。
  4. 无效行作为自己的形象进来,使他们从缓存中重新载入。

我个人认为,框架应该提供一个WebImage小工具,将使这是一个更容易一些。我想有太多的移动部件,这是有道理的,每个应用程序来实现这个自己。

修改 - 除非的使用图书馆,让别人做肮脏的工作。显然, DroidFu 有针对此问题的解决方案:的 ImageLoader的。我没有用它,但它可能不容易出错比滚动自己。

how to pass an array containing urls as an image source to imageadapter of the grid view? I have a working Image adapter. but the problem is the getView method of the adapter returns only one imageview. Can some one tell how to pass an array containing urls to the image adapter of the gridview ? the getview method is here

    public View getView(final int position, View convertView, ViewGroup parent) {
    ImageView imageView = null;

        if (convertView == null) {  // if it's not recycled, initialise some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8,8,8,8);
        } else {
            imageView = (ImageView) convertView;
        }

    //  Bitmap bmp  = loadBitmap(url[i]);
        Bitmap bmp  = loadBitmap("http://www.fgj.com/aero/planes/boeing/boeingf15.jpg");
        System.out.println("in adapter :" + url[i]);
        imageView.setImageBitmap(bmp);

    //imageView.setImageResource(mThumbIds[position]);
    //Bitmap bmp  = loadBitmap("http://www.fgj.com/aero/planes/boeing/boeingf15.jpg");
    return imageView;
}

解决方案

Unfortunately this is much harder than it needs to be. The overview of what you need to do is:

  1. Collect the urls you need to fetch.
  2. Create an AsyncTask to fetch each one in turn or use a pool of AsyncTasks in a thread-pool fashion to service the urls. Somewhere you need to keep track of which url belongs to which position in your AdapterView so you can selectively invalidate rows as their images come in.
  3. Cache the images you get back to the SD card or in memory.
  4. Invalidate rows as their images come in, causing them to reload from the cache.

Personally I think the framework should supply a "WebImage" widget that would make this a bit easier. I guess there are just too many moving parts that it makes sense for each app to implement this on its own.

Edit - unless you use a library and let someone else do the dirty work. Apparently DroidFu has a solution for this problem: ImageLoader. I haven't used it, but it's probably less error prone than rolling one's own.

这篇关于如何通过包含的网址到图像源imageadapter网格视图数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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