Android 使用多个位图的自定义GridView

Android 使用多个位图的自定义GridView,android,android-arrayadapter,android-gridview,bitmapfactory,Android,Android Arrayadapter,Android Gridview,Bitmapfactory,我正在填充多个项目,每个都包含ImageView和TextView,大约有100多个这样的项目。 问题是一切都正常执行,但滚动时活动严重滞后, 我试图通过限制图像大小来减少图像质量,但没有任何改进 还要注意,我的图像源是本地文件系统和Category.getCategoryUrl()包含文件路径 以下是我的实现: public class RootCategoryAdapter extends ArrayAdapter<Category> { private int res

我正在填充多个项目,每个都包含ImageView和TextView,大约有100多个这样的项目。 问题是一切都正常执行,但滚动时活动严重滞后, 我试图通过限制图像大小来减少图像质量,但没有任何改进

还要注意,我的图像源是本地文件系统和Category.getCategoryUrl()包含文件路径

以下是我的实现:

public class RootCategoryAdapter extends ArrayAdapter<Category> {

    private int resource;
    private Context context;
    private ArrayList<Category> categoryList;
    private DisplayMetrics metrics;

    public RootCategoryAdapter(Context context, int resource,
            ArrayList<Category> categoryList, DisplayMetrics metrics) {
        super(context, resource, categoryList);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.resource = resource;
        this.categoryList = categoryList;
        this.metrics = metrics;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        CategoryHolder holder = null;

        if (convertView == null) {
            // LayoutInflater inflater =(LayoutInflater)
            // context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView = inflater.inflate(resource, parent, false);

            holder = new CategoryHolder();
            holder.title = (TextView) convertView
                    .findViewById(R.id.root_category_text);
            holder.image = (ImageView) convertView
                    .findViewById(R.id.root_category_image);

            convertView.setTag(holder);
        } else {
            holder = (CategoryHolder) convertView.getTag();
        }

        Category category = categoryList.get(position);

        holder.title.setText(category.getCategoryName());

        // I think problem starts here but not certain
        holder.options = new BitmapFactory.Options();
        holder.options.inJustDecodeBounds = true;
        holder.bitmap = BitmapFactory.decodeFile(category.getCategoryUrl(),
                holder.options);
        holder.options.inJustDecodeBounds = false;
        holder.options.inSampleSize = BasicChores.calculateInSampleSize(
                holder.options, 200, 200);
        holder.bitmap = BitmapFactory.decodeFile(category.getCategoryUrl(),
                holder.options);

        holder.image.setImageBitmap(holder.bitmap);

        return convertView;

    }

    static class CategoryHolder {
        Bitmap bitmap;
        BitmapFactory.Options options;
        TextView title;
        ImageView image;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

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

    @Override
    public Category getItem(int position) {
        // TODO Auto-generated method stub
        return categoryList.get(position);
    }

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

    @Override
    public int getPosition(Category item) {
        // TODO Auto-generated method stub
        return categoryList.indexOf(item);
    }

}
公共类RootCategoryAdapter扩展了ArrayAdapter{
私有int资源;
私人语境;
私有ArrayList类别列表;
私有显示度量;
公共RootCategoryAdapter(上下文上下文、int资源、,
ArrayList类别列表,DisplayMetrics){
super(上下文、资源、类别列表);
//TODO自动生成的构造函数存根
this.context=上下文;
这个资源=资源;
this.categoryList=categoryList;
这个。度量=度量;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
CategoryHolder holder=null;
if(convertView==null){
//LayoutInflater充气机=(LayoutInflater)
//context.getSystemService(Activity.LAYOUT\u INFLATER\u SERVICE);
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
convertView=充气机。充气(资源,父项,false);
holder=新类别文件夹();
holder.title=(TextView)convertView
.findviewbyd(R.id.root\u category\u text);
holder.image=(ImageView)convertView
.findviewbyd(R.id.root\u类别\u图像);
convertView.setTag(支架);
}否则{
holder=(CategoryHolder)convertView.getTag();
}
Category=categoryList.get(位置);
holder.title.setText(category.getCategoryName());
//我认为问题从这里开始,但不确定
holder.options=新的BitmapFactory.options();
holder.options.inJustDecodeBounds=true;
holder.bitmap=BitmapFactory.decodeFile(category.getCategoryUrl(),
持有人(期权);
holder.options.inJustDecodeBounds=false;
holder.options.inSampleSize=BASICHORES.calculateInSampleSize(
持有人:期权,200,200);
holder.bitmap=BitmapFactory.decodeFile(category.getCategoryUrl(),
持有人(期权);
holder.image.setImageBitmap(holder.bitmap);
返回视图;
}
静态类类别文件夹{
位图;
选项;
文本视图标题;
图像视图图像;
}
@凌驾
公共布尔表ID(){
//TODO自动生成的方法存根
返回true;
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回categoryList.get(position.getCategoryId();
}
@凌驾
公共类别getItem(内部位置){
//TODO自动生成的方法存根
返回categoryList.get(位置);
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回categoryList.size();
}
@凌驾
公共int getPosition(类别项){
//TODO自动生成的方法存根
返回(项目)的类别列表索引;
}
}

您可能应该缓存图像

看看这个:


在后台加载位图。google“lazy image loading”@SherifelKhatib“lazy image loading”对我没有任何帮助。如果可能的话,你能更准确地描述我吗“是的,但我的文件存储中已经有了图像,因此我希望能找到解决办法,而不是创建UI线程……我看到了GreenDroid的一个,但它只是下载图像。我使用Lrucache检查了图像,但由于超过了应用程序ram,它会释放出内存。您还可以在文件中将heapsize设置为大,从而放大heapsize。