从可绘制资源创建动态缩略图图像并设置为imageView

时间:2017-08-30 08:25:58

标签: android dynamic imageview thumbnails android-drawable

我是android的新手,我正在使用一些可绘制的资源图像来显示活动。但我想将图像缩放到缩略图大小。

我的代码是:

public class CategoryImages extends BaseAdapter {
    ...
    ...
    Integer[] categoryImagesList;
    public CategoryImages(Context mainActivity, Integer[] categories_images, String selectedCat) {
        categoryImagesList = {R.drawable.suit_7, R.drawable.bkg_4, R.drawable.misc_18};
    }
    public class Holder {
        ImageView iv;
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder = new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.category_image_list, null);
        holder.iv = (ImageView) rowView.findViewById(R.id.category_img);
        holder.iv.setImageResource(categoryImagesList[position]);
    }
    ...
    ...
}

这是具有实际图像大小的工作文件,但现在我将其更改为

public class CategoryImages extends BaseAdapter {
    ...
    ...
    Integer[] categoryImagesList;
    public CategoryImages(Context mainActivity, Integer[] categories_images, String selectedCat) {
        categoryImagesList = {R.drawable.suit_7, R.drawable.bkg_4, R.drawable.misc_18};
    }
    public class Holder {
        ImageView iv;
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder = new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.category_image_list, null);
        holder.iv = (ImageView) rowView.findViewById(R.id.category_img);

        Bitmap thumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(categoryImagesList[position]), 100, 100);
        holder.iv.setImageResource(thumbImage);
    }
    ...
    ...
}

然后ThumbnailUtils.extractThumbnail显示categoryImagesList[position]参数错误且holder.iv.setImageResource不接受thumbImage参数,它说thumbImage类型为Bitmap,setImageResource需要int

感谢

1 个答案:

答案 0 :(得分:1)

你需要在这里改变两件事:

1)BitmapFactory.decodeFile(categoryImagesList[position]), 100, 100)BitmapFactory.decodeResource(getResources(),categoryImagesList[position]);

2)将holder.iv.setImageResource(thumbImage);更改为holder.iv.setImageBitmap(thumbImage);