【问题标题】:Android custom CursorAdapter with AsyncTask带有 AsyncTask 的 Android 自定义 CursorAdapter
【发布时间】:2013-10-06 15:44:27
【问题描述】:

我正在尝试使用从设备获取的图像和文本构建一个列表。事实证明,从手机的相机中获取图像是一项需要一段时间的任务,所以我试图让它尽可能快,这样用户体验就不会变慢。我从中得到的只是看起来所有图像都加载到一个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) {

  }
}

【问题讨论】:

    标签: android android-cursoradapter


    【解决方案1】:

    我认为这需要时间的可能原因是因为图像的大小至少为 1 mb,您可以更改为缩略图并检索它,如果仍然需要时间,您可以进行延迟下载,这在我们拍摄图像时完成从服务器(基本上它的作用是在我们获取图像时加载文本并显示图像)

    【讨论】:

    【解决方案2】:

    删除 ImageTask AsyncTask ..

    使用像 GlidePicasso 这样的库。对于几乎任何需要获取、调整大小和显示远程图像的情况都非常有效。

    我使用滑行从phone storage uri 加载图像

    使用上述任何一种,看看区别

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-29
      • 1970-01-01
      • 2013-05-09
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多