gpt4 book ai didi

带有 AsyncTask 的 Android 自定义 CursorAdapter

转载 作者:太空狗 更新时间:2023-10-29 15:08:34 25 4
gpt4 key购买 nike

我正在尝试使用从设备中获取的图像和文本构建一个列表。事实证明,从手机相机拍摄图像是一项需要一段时间的任务,所以我试图让它尽可能快,这样用户体验就不会变慢。我从中得到的只是看起来所有图像都加载到一个 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 大小,您可以更改为缩略图并检索它,而且如果仍然需要时间,您可以进行延迟下载,这是在我们拍摄图像时完成的来自服务器(基本上它所做的是加载文本并在我们获取图像时显示图像)

关于带有 AsyncTask 的 Android 自定义 CursorAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19210671/

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