Fersco:使用simpledraweview集成Asynctask

时间:2016-03-11 08:53:57

标签: android performance listview android-asynctask fresco

我正在使用baseadapter的自定义列表视图。每行包含SimpleDraweeView。我正在使用本地图像映射simpleDraweeView。如果图像在本地路径中不可用,那么我将根据我的约束创建一个位图并将其存储到该路径中。

我正在AsyncTask中创建位图创建部分。如果我快速滚动,那么Asynctask将多次调用,所以我想通过使用下面的函数来避免这种情况

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;
}

将BitmapworkerTask设置为SimpleDraweeView代码:

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类:

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;
   }
}

1 个答案:

答案 0 :(得分:0)

你的问题到底是什么?你在问这个代码是否合适?不是。 Fresco的重点是避免编写此代码。

您不需要AsyncTask或其中任何一个。您只需在SimpleDraweeView构造函数中设置层次结构,然后调用.setImageURI