2017-08-30 130 views
0

我是新來的android,我正在使用一些可繪製的資源圖像顯示在活動中。但我想縮放該圖像縮略圖大小。從可繪製資源創建動態縮略圖圖像並設置爲imageView

我的代碼是:

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 param,它表示thumbImage是Bitmap類型的,並且setImageResource要求int

感謝

+0

變化'holder.iv.setImageResource(thumbImage);'到'holder.iv.setImageBitmap(thumbImage);' – Piyush

回答

1

這裏需要改變兩件事情:

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

2)改變holder.iv.setImageResource(thumbImage);holder.iv.setImageBitmap(thumbImage);