gpt4 book ai didi

java - 在本地加载 ListView 图标,寻找最高效的流程

转载 作者:行者123 更新时间:2023-12-01 12:29:26 25 4
gpt4 key购买 nike

我正在尝试将本地可绘制对象(从我的 res>可绘制文件夹)有效地加载到我的列表项中。我希望在加载图像时使用尽可能少的内存。

我最初将资源名称 (R.drawable.list_icon) 在模型中定义为 int ,并在适配器中调用 setImageResource 来应用图标。

但是在查看 API 文档后,它建议我使用 setImageDrawablesetImageBitmap 因为它们不是在 UI 线程上运行的。我需要知道下面的代码(我已将可绘制对象转换为位图并使用 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 色图标,它们是实际的产品图像。

最佳答案

尝试使用UniversalImageLoader。它不仅适用于从网络加载图像。

关于java - 在本地加载 ListView 图标,寻找最高效的流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26062486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com