在gridview android上使用UIL时重复图像

时间:2015-04-09 11:47:51

标签: android gridview universal-image-loader

我正在使用gridview在本地加载图像并使用通用图像加载器,但是我有一个问题,并且没有任何解决方案使我受益,即使我的图像上没有重复的可绘制并且它们是独一无二的,图像加载器复制了一些图像。

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class customizedGrid extends BaseAdapter {
    private Context mContext;
    private final Integer[] Imageid;
    ImageLoader imageLoader = ImageLoader.getInstance();;
    DisplayImageOptions options;
    LayoutInflater inflater;

    public customizedGrid(Context c, Integer[] mImageIds) {
        mContext = c;
        options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.images).resetViewBeforeLoading(true)
                .cacheInMemory().cacheOnDisc()
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .delayBeforeLoading(1000)
                .displayer(new SimpleBitmapDisplayer()).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
        this.Imageid = mImageIds;
        inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Imageid.length;
    }

    @Override
    public Integer getItem(int position) {
        // TODO Auto-generated method stub
        return Imageid[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView imageView;

        if (convertView == null) {

            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(125, 125));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setScaleType(ScaleType.FIT_XY);
        imageLoader.displayImage("drawable://" + Imageid[position], imageView,
                options);
        // imageView.setImageResource(Imageid[position]);
        return imageView;
    }

}

我尝试使用Image Holder但性能问题仍然存在

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        grid = inflater.inflate(R.layout.unit, parent, false);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.image = (ImageView) grid.findViewById(R.id.grid_image);
        grid.setTag(viewHolder);
    } else {
        grid = (View) convertView;
    }
    ViewHolder holder = (ViewHolder) grid.getTag();
    holder.image.setImageResource(Imageid[position]);
    return grid;
    /*
     * imageLoader.displayImage("drawable://" //
     * +Imageid[Integer.parseInt(imageView
     * .getTag().toString())],imageView,options);
     * imageView.setImageResource(
     * Imageid[Integer.parseInt(imageView.getTag() .toString())]);
     */

}

static class ViewHolder {
    public ImageView image;
}

1 个答案:

答案 0 :(得分:0)

如果问题就像imageview首先显示现有图像,然后在一段时间后显示真实图像,尝试在行之前设置默认图像

imageView.setScaleType(ScaleType.FIT_XY);

添加

imageView.setImageResource(R.drawable.ic_launcher);  // a default image

如果这不是问题

@覆盖 public View getView(int position,View convertView,ViewGroup parent){

// TODO Auto-generated method stub
int currentposition=position;
ImageView imageView;

if (convertView == null) {

    imageView = new ImageView(mContext);
    imageView.setLayoutParams(new GridView.LayoutParams(125, 125));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setPadding(0, 0, 0, 0);
} else {
    imageView = (ImageView) convertView;
}
imageview.setTag(currentposition);
 imageView.setImageResource(R.drawable.ic_launcher); 
imageView.setScaleType(ScaleType.FIT_XY);
imageLoader.displayImage("drawable://" +   Imageid[Integer.parseInt(imageview.getTag().toString())], imageView,
        options);
// imageView.setImageResource(Imageid[position]);
return imageView;

}