我正在尝试显示带有商品的 ListView。 Goods 是一个包含标题、数量和图像 (BLOB) 的 SQLite 表。所以我的ListView不流畅。
我尝试了什么:
- 打开相机 Intent 、捕捉、获取 Extras 并插入到 SQLite。所以我只使用了一张位图。在 cargo 数量少于 50 之前,此方法运行良好。
我走的是另一条路:
- 打开相机 Intent ,捕捉并保存到文件。
- 检查
ExifInterface
并根据需要旋转图像。 - 使用 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 进行缩放大小为 300x300 并移动到我的文件夹。此图像文件用于全屏显示。
- 现在我需要一个缩略图以便将来将其放入 ListView 中。因此,我使用 http://developer.android.com/training/camera/photobasics.html 中的
setPic()
方法将文件 (3) 缩放到缩略图大小(例如 60x60) 并将这个小拇指保存到 SQLite 数据库 Goods 表中。
现在,当 Goods 数量超过 100 时,ListView 变得很难滚动。这不是问题的解决方案。
所以我需要一些不同的方法来用图像填充 ListView。
请以正确的方式指引我)))
谢谢!
附言我正在使用自定义 SimpleCursorAdapter 来显示数据:
public class GoodsCursorAdapter extends SimpleCursorAdapter {
public GoodsCursorAdapter(Context context) {
super(context, R.layout.cell_goods, null,
new String[] { "title", "countAvail" },
new int[] { R.id.lbTitle, R.id.lbCount }, 0);
}
private class ViewHolder {
ImageView imageView;
TextView lbTitle, lbCount;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.cell_goods, parent, false);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.ivThumb);
viewHolder.lbTitle = (TextView) convertView.findViewById(R.id.lbTitle);
viewHolder.lbCount = (TextView) convertView.findViewById(R.id.lbCount);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
mainCursor.moveToPosition(position);
byte[] byteArray = mainCursor.getBlob(mainCursor.getColumnIndex("photo"));
if (byteArray != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Log.d("INFO", mainCursor.getString(mainCursor.getColumnIndex("title"))+": "+String.valueOf(bitmap.getWidth())+"x"+String.valueOf(bitmap.getHeight())+"("+String.valueOf(bitmap.getByteCount()/1024)+"Kb)");
viewHolder.imageView.setImageBitmap(bitmap);
} else {
viewHolder.imageView.setImageResource(R.drawable.ic_action_picture);
}
viewHolder.lbTitle.setText(mainCursor.getString(mainCursor.getColumnIndex("title")));
viewHolder.lbCount.setText(String.format(getString(R.string.lbCount), mainCursor.getString(mainCursor.getColumnIndex("countAvail"))));
return convertView;
}
最佳答案
尝试从 UI 线程加载图像 - 使用 AsyncTask。
类似这样的内容(取自 here )
class BitmapWorkerTask extends AsyncTask<Cursor, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private Cursor mCursor;;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Cursor... params) {
mCursor = params[0];
byte[] byteArray = mCursor.getBlob(mCursor.getColumnIndex("photo"));
if (byteArray != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
return bitmap;
}
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
所以修改
if (byteArray != null) {
BitmapWorkerTask task = new BitmapWorkerTask(imageView);
task.execute(mainCursor);
}else{......}
关于Android - 带有图像的 ListView 滚动不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29794291/