我正在尝试建立一个列表,其中包含从设备获取的图像和文本。结果发现,从手机的摄像头拍摄图像需要一段时间,所以我正努力使它尽可能快,这样用户体验不会变慢。我从中得到的结果是,看起来所有的图像都加载在一个ImageView
中,而不是图像扩散到所有其他ImageViews
(我不完全确定我对ViewHolder
技术和自定义CursorAdapter
的实现是否正确)。
public class MyCustomCurserAdapter extends CursorAdapter {
static class ViewHolder {
public TextView nameText;
public ImageView imageThumbnail;
}
Cursor cursor;
public MyCustomCurserAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context arg1, Cursor cursor) {
ViewHolder holder = (ViewHolder)view.getTag();
int pathCol = cursor.getColumnIndex(NewPicSQLiteHelper.COLUMN_PATH);
String imageInSD = cursor.getString(pathCol);
File imgFile = new File(imageInSD);
if(imgFile.exists()){
int nameCol = cursor.getColumnIndex(NewPicSQLiteHelper.COLUMN_PIC_NAME);
String name = cursor.getString(nameCol);
if (name != null)
holder.nameText.setText(name);
ImageTask task = new ImageTask(holder.imageThumbnail);
task.execute(imgFile);
}
}
@Override
public View newView(Context arg0, Cursor cur, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.new_pic_item, parent, false);
ViewHolder holder = new ViewHolder();
holder = new ViewHolder();
holder.nameText = (TextView) view.findViewById(R.id.pic_name_entry);
holder.imageThumbnail = (ImageView) view.findViewById(R.id.pic_thumbnail);
// The tag can be any Object, this just happens to be the ViewHolder
view.setTag(holder);
return view;
}
private class ImageTask extends AsyncTask<File, Void, Bitmap>{
private final WeakReference <ImageView> imageViewReference;
public ImageTask(ImageView imageView) {
imageViewReference = new WeakReference <ImageView> (imageView);
}
@Override
protected Bitmap doInBackground(File... params) {
String path = params[0].getAbsolutePath();
return decodeSampledBitmapFromResource(path,75,75);
}
@Override
protected void onPostExecute(Bitmap result) {
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
if (result != null) {
imageView.setImageBitmap(result);
imageView.setVisibility(ImageView.VISIBLE);
} else {
imageView.setVisibility(ImageView.INVISIBLE);
}
}
}
}
private Bitmap decodeSampledBitmapFromResource(String orgImagePath, int reqWidth, int reqHeight) {
}
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
}
}
最佳答案
我认为它花费时间的原因可能是因为图像至少有1 MB的大小,你可以更改为缩略图并检索它,而且如果仍然需要时间,你可以放置延迟下载,这是在我们从服务器获取图像时完成的(基本上它所做的是加载获取图像时显示文本和图像)
关于android - 具有AsyncTask的Android自定义CursorAdapter,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19210671/