2016-11-26 25 views
0

我使用以下代碼使用Fresco在Recycler View中加載圖像。 目前,只要用戶滾動到任何項目,圖像加載就會開始,並且Fresco加載圖像並將其緩存以備後用,下次從緩存加載圖像時。是否有任何方法可以預先緩存圖像,以便即使是第一次也能快速加載圖像。如何在緩衝區中預覽2-3張使用壁畫的圖像

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

} 
+0

看着github頁面,Fresco已經緩存了位圖。 – rhari

+0

yes當用戶滾動到項目時,壁畫開始加載和緩存圖像,因此第一次加載圖像時會延遲加載圖像,因爲那時從服務器獲取圖像,下一次壁畫會從緩存中提取圖像。 – Sam

回答

2

壁畫支持預取,見http://frescolib.org/docs/using-image-pipeline.html

你可以簡單地調用

imagePipeline.prefetchToDiskCache(imageRequest, callerContext); 

imagePipeline.prefetchToBitmapCache(imageRequest, callerContext); 

預取到磁盤或位圖緩存。