我是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
感谢
答案 0 :(得分:1)
你需要在这里改变两件事:
1)BitmapFactory.decodeFile(categoryImagesList[position]), 100, 100)
至BitmapFactory.decodeResource(getResources(),categoryImagesList[position]);
2)将holder.iv.setImageResource(thumbImage);
更改为holder.iv.setImageBitmap(thumbImage);