自定义GridView滞后使用多个位图

时间:2013-10-27 00:09:20

标签: android android-arrayadapter android-gridview bitmapfactory

我使用多个项目填充GridView,每个项目包含ImageView和TextView,大约有100多个这样的项目。 问题是一切正常,但滚动时活动滞后, 我试图通过限制图像化来load bitmap efficiently,但没有任何改进。

还要注意我的图片来源是Local Filesystem,而Category.getCategoryUrl()包含filepath。

以下是我的ArrayAdapter实施:

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);
    }

}

1 个答案:

答案 0 :(得分:1)