2

I am using custom listview with baseadapter. Each row contain SimpleDraweeView. I am mapping simpleDraweeView with my local images. If image is not available in local path then i will create a bitmap based on my constraint and store it into that path.

I am doing bitmap creation part in AsyncTask. If i scrolling fast then Asynctask will call multiple times so i want to avoid this by using below function

private boolean cancelPotentialWork(Object data, Object view) {
    SimpleDraweeView imageView = (SimpleDraweeView) view;
    final BitmapWorkerTask bitmapWorkerTask =getBitmapWorkerTask(imageView);
    if (bitmapWorkerTask != null) {
        final Object bitmapData = bitmapWorkerTask.mPath;
        if (bitmapData == null || !bitmapData.equals(data)) {
            bitmapWorkerTask.cancel(true);
        } else {
            return false;
        }
    }
    return true;
}

Set BitmapworkerTask to SimpleDraweeView Code :

Drawable drawable = new AsyncColorDrawable(task, mPath); BitmapWorkerTask task = new BitmapWorkerTask(mPath, mImageView, object, optionView, mViewMode, type); mImageView.setVisibility(View.VISIBLE); mImageView.setImageDrawable(drawable); task.execute();

AsyncColorDrawable Class :

private class AsyncColorDrawableForNote extends ColorDrawable {
    private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
    private String mPath;

    public AsyncColorDrawableForNote(BitmapWorkerTask bitmapWorkerTask, String path) {
        super(mContext.getResources().getColor(R.color.application_container_background_color));
        bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
        mPath = path;
    }

    public BitmapWorkerTask getBitmapWorkerTask() {
        return bitmapWorkerTaskReference.get();
    }

    public String getPath() {
        return mPath;
    }
}

BitmapWorkerTask :

private class BitmapWorkerTask extends AsyncTask<Void, Void, Bitmap> {
    private final WeakReference<SimpleDraweeView> imageViewReference;
    private String mPath;
    private Object mObject;
    private View mOPtionView;
    private int mViewMode;
    private int mType;
    private SimpleDraweeView mDraweeView;

    public BitmapWorkerTask(String path, Object view, Object obj,
                            View optionView, int viewMode, int type) {
        mObject = obj;
        mPath = path;
        mType = type;
        mOPtionView = optionView;
        mViewMode = viewMode;
        mDraweeView = (SimpleDraweeView) view;
        imageViewReference = new WeakReference<>((SimpleDraweeView) view);
    }


    @Override
    protected Bitmap doInBackground(Void... params) {            
        return createBitmapFromNote(mObject, mViewMode, mPath)
    }

    @Override
    protected void onPostExecute(Bitmap value) {
        SimpleDraweeView mImageView = getAttachedImageView();
        if (value != null && mImageView != null) {
            mImageView.setVisibility(View.VISIBLE);            
            GenericDraweeHierarchy hierarchy = setHierarchyForDraweeView(mImageView, 0);
            hierarchy.setFailureImage(new BitmapDrawable(mContext.getResources(), value));
            hierarchy.setPlaceholderImage(new BitmapDrawable(mContext.getResources(), value));
            mImageView.setImageURI(Uri.fromFile(new File(mPath)));
        } 
    }

    private SimpleDraweeView getAttachedImageView() {
        final SimpleDraweeView imageView = imageViewReference.get();
        final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
        if (this == bitmapWorkerTask) {
            return imageView;
        }
        return null;
    }

private GenericDraweeHierarchy setHierarchyForDraweeView(SimpleDraweeView draweeView, int duration) {
    if (draweeView != null) {
        if (draweeView.getHierarchy() == null) {
            GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(mContext.getResources());
            GenericDraweeHierarchy hierarchy = builder
                    .setFadeDuration(duration)
                    .setPlaceholderImage(new AsyncColorDrawable(mContext.getResources()))
                    .setFailureImage(mContext.getResources().getDrawable(R.drawable.broken_image_black))
                    .build();
            draweeView.setHierarchy(hierarchy);
        } else {
            GenericDraweeHierarchy hierarchy = draweeView.getHierarchy();
            hierarchy.setFadeDuration(duration);
            return hierarchy;
        }
    }
    return null;
   }
}
Arun
  • 510
  • 1
  • 4
  • 21

1 Answers1

0

What is your question, exactly? Are you asking if this code is okay? It's not. The whole point of Fresco is to avoid having to write this code.

You don't need an AsyncTask or any of that. You just need to set your hierarchy in the SimpleDraweeView constructor and then call .setImageURI.

tyronen
  • 2,558
  • 1
  • 11
  • 15
  • I am creating bitmap in asynctask. Once my bitmap is ready then i will assign that bitmap to simpledraweeview. I am using grid view. so each item having simpledraweeview. so i will start multiple asynctask. – Arun Nov 25 '16 at 14:50