简体   繁体   中英

Glide pre loaded image

i am using glide to load locally image. On first activity i loaded five large size images and same images loaded again on second activity but issue is that i already downloaded images on first activity and is available in cache but taking time on second activity when load. my first activity code

   Glide
                .with(getApplicationContext())
                .load(path)
                .asBitmap()
                .thumbnail(0.1f)
                 .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .priority(Priority.HIGH)
                .override(180, 180);

and second activity

    Glide
            .with(context)
            .load(path)
            .asBitmap()
            .thumbnail(0.1f)
            .diskCacheStrategy(DiskCacheStrategy.RESULT)
            .priority(Priority.HIGH)
            .override(180, 180).into(viewHolder.img_android);

note that all images size greater than 5 mb. when i open second activity first time, image take time to load then when again open its loaded so fast. so i want to preload all images on first activity. how is it possible. my first actvitiy code:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.third);

    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(R.drawable.iamge_1);
    list.add(R.drawable.iamge_2);
    list.add(R.drawable.iamge_3);
    list.add(R.drawable.image_5);
    list.add(R.drawable.iamge_4);
    list.add(R.drawable.image_6);
    loadImages(list);

    findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(First.this, Second.class);
            startActivity(i);
        }
    });
}


public void loadImages(ArrayList<Integer> list) {

    for ( int i = 0; i < list.size(); i++) {
        Glide
                .with(getApplicationContext())
                .load(list.get(i))
                .asBitmap()
                .thumbnail(0.1f)
                 .diskCacheStrategy(DiskCacheStrategy.ALL)
                .priority(Priority.HIGH)
                .override(133, 133);

    }
}

second activity code:

 private void initViews() {
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 2);
    recyclerView.setLayoutManager(layoutManager);

    ArrayList<Integer> androidVersions = prepareData();
    DataAdapter adapter = new DataAdapter(getApplicationContext(), androidVersions);
    recyclerView.setAdapter(adapter);

}

public ArrayList<Integer> prepareData() {
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(R.drawable.iamge_1);
    list.add(R.drawable.iamge_2);
    list.add(R.drawable.iamge_3);
    list.add(R.drawable.image_5);
    list.add(R.drawable.iamge_4);
    list.add(R.drawable.image_6);
    return list;
}

and adapter:

 @Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext())
      .inflate(R.layout.recycler_item, viewGroup, false);
    return new ViewHolder(view);
}

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

    Glide
            .with(context)
            .load(android.get(i))
            .asBitmap()
            .thumbnail(0.1f)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .priority(Priority.HIGH)
            .override(133, 133).into(viewHolder.img_android);
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    private TextView tv_android;
    private ImageView img_android;

    public ViewHolder(View view) {
        super(view);

        img_android = (ImageView) view.findViewById(R.id.img_android);
    }
}

You can use

 .placeholder(R.drawable.your_image) // for default image.

For your requirement read Using Cache Strategies

  • DiskCacheStrategy.NONE caches nothing, as discussed

  • DiskCacheStrategy.SOURCE caches only the original full-resolution image. In our example above that would be the 1000x1000 pixel one

  • DiskCacheStrategy.RESULT caches only the final image, after reducing the resolution (and possibly transformations) (default behavior)

  • DiskCacheStrategy.ALL caches all versions of the image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM