如何在Recycler视图中使用Fresco预处理2-3张图像

时间:2016-11-26 14:05:31

标签: android fresco

我使用以下代码使用Fresco在Recycler View中加载图像。 目前,一旦用户滚动到任何项目,图像加载就开始,并且Fresco加载图像并将其缓存以供稍后使用,并且下次从缓存加载图像。有没有办法预先处理图像,以便即使第一次也可以快速加载图像。

public class ProductFeedsRecyclerAdapterV2 extends RecyclerView.Adapter<ProductFeedsRecyclerAdapterV2.ViewHolder> {

    private static final String TAG = ProductFeedsRecyclerAdapterV2.class.getSimpleName();
    private List<ProductFeed> mItems;
    private float ratio;

    Context context;
    String user_id;

    public ProductFeedsRecyclerAdapterV2(Context context, List<ProductFeed> items) {
        mItems = items;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_brand_feed, viewGroup, false);

        return new ViewHolder(v);
    }


    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {

        final ProductFeed feed = mItems.get(i);

        CircleProgressBarDrawable drawable = new CircleProgressBarDrawable();
        drawable.setBackgroundColor(context.getResources().getColor(R.color.gray));
        drawable.setColor(context.getResources().getColor(R.color.colorAccent));

        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(100)
                .setProgressBarImage(drawable)
                .build();

        hierarchy.setActualImageScaleType(ScalingUtils.ScaleType.FOCUS_CROP);
        hierarchy.setActualImageFocusPoint(new PointF(0.5f,0f));

        Uri uri = Uri.parse(feed.image);
        viewHolder.ivBrandImage.setImageURI(uri);
        viewHolder.ivBrandImage.setHierarchy(hierarchy);
        viewHolder.ivBrandImage.setAspectRatio(1.15f);

    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.ivBrandImage) SimpleDraweeView ivBrandImage;
        ViewHolder(View v) {
            super(v);
            ButterKnife.bind(this, v);
        }
    }

}

1 个答案:

答案 0 :(得分:2)

Fresco支持预取,请参阅http://frescolib.org/docs/using-image-pipeline.html

您只需致电

即可
imagePipeline.prefetchToDiskCache(imageRequest, callerContext);

imagePipeline.prefetchToBitmapCache(imageRequest, callerContext);

预取到磁盘或位图缓存。