我试图有效地将我的本地drawables(从我的res> drawable文件夹)加载到我的列表项中。我希望在加载图像时尽可能少地使用这个内存。
我最初的资源名称(R.drawable.list_icon)在模型中定义为int
,并在我的适配器中调用setImageResource
来应用图标。
但是,在审核了API文档之后,它建议我使用setImageDrawable
或setImageBitmap
,因为它们不在UI线程上运行。我需要知道下面的代码是否是下面的代码,我将drawable转换为位图并使用setImageBitmap
是一个很好的方法,如果在AsyncTask
中加载本地图像值得合并(因为我也看过这种方法)。
public class ProductItem {
public int listIcon;
public String listTitle;
public String listDesc;
public ProductItem(int listIcon, String listTitle,
String listDesc) {
this.listIcon = listIcon;
this.listTitle = listTitle;
this.listDesc = listDesc;
}
public int getIconID() {
return listIcon;
}
}
public class ProductAdapter extends ArrayAdapter<ProductItem> {
Context context;
int layoutResourceID;
ProductItem data[];
ProductHolder viewHolder;
public ProductAdapter(Context context, int layoutResourceID, ProductItem[] data) {
super(context, layoutResourceID, data);
this.layoutResourceID = layoutResourceID;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layoutResourceID, parent, false);
viewHolder = new ProductHolder();
viewHolder.productType = (TextView) convertView.findViewById(R.id.itemTitle);
viewHolder.productDesc = (TextView) convertView.findViewById(R.id.itemSubtitle);
viewHolder.productImage = (ImageView) convertView.findViewById(R.id.itemIcon);
convertView.setTag(viewHolder);
} else {
viewHolder = (ProductHolder) convertView.getTag();
}
ProductItem objectItem = data[position];
if (objectItem != null) {
viewHolder.productType.setText(objectItem.listTitle);
viewHolder.productType.setTag(objectItem.listTitle);
viewHolder.productDesc.setText(objectItem.listDesc);
viewHolder.productDesc.setTag(objectItem.listDesc);
// Im currently using the following method for obtain the resource.
InputStream is = context.getResources().openRawResource(objectItem.getIconID());
final Bitmap imageBitmap = BitmapFactory.decodeStream(is);
viewHolder.productImage.setImageBitmap(imageBitmap);
// Im using 'setImageBitmap' since it doesnt run on the UI thread, hoping that this will in return load quicker.
// However i was previously using the following.
// viewHolder.productImage.setImageResource(objectItem.getIconID());
viewHolder.productImage.setTag(objectItem.listIcon);
}
return convertView;
}
static class ProductHolder {
public TextView productType;
public TextView productDesc;
public ImageView productImage;
}
}
listData[0] = new ProductItem(R.drawable.list_icon, getString(R.string.list_title), getString(R.string.list_description));
我的主要问题是加载局部绘图的最有效方法是什么,这些图像图标质量非常高,而不仅仅是简单的1色图标,它们是实际的产品图像。
答案 0 :(得分:1)
尝试使用UniversalImageLoader。它不仅适用于从网络加载图像。