My Gallery Lags a lot when i try to load images from sd card(Android Recyclerview) -


i trying implement gallery app using recycler view , sub sampling gallery. since image count around 850. when try load images gallery, gallery lags.

here recyclerview adapter:-

public class recyclerviewadapter extends recyclerview.adapter<recyclerviewadapter.recyclerviewholders> {  private arraylist<string> yeniliste; private context context;  public recyclerviewadapter(context context, arraylist<string> itemlist) {     this.yeniliste = itemlist;     this.context = context; }  @override public recyclerviewholders oncreateviewholder(viewgroup parent, int viewtype) {     view layoutview = layoutinflater.from(parent.getcontext()).inflate(r.layout.gallery_item, null);     recyclerviewholders rcv = new recyclerviewholders(layoutview);     return rcv; }  @override public void onbindviewholder(final recyclerviewholders holder, final int position) {     try {         bitmap bitmap = bitmapfactory.decodefile(yeniliste.get(position));         holder.countryphoto.setimage(imagesource.bitmap(bitmap).dimensions(50,50));     }catch (exception e){         e.printstacktrace();     }      holder.countryphoto.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             intent intent = new intent(v.getcontext(),galleryfullimage.class);             intent.putextra("realid",string.valueof(holder.getadapterposition()));             v.getcontext().startactivity(intent);         }     }); }  @override public int getitemcount() {     return this.yeniliste.size(); }   public static class recyclerviewholders extends recyclerview.viewholder implements view.onclicklistener{      public subsamplingscaleimageview countryphoto;      public recyclerviewholders(view itemview) {         super(itemview);         countryphoto = (subsamplingscaleimageview)itemview.findviewbyid(r.id.country_photo);     }      @override     public void onclick(view view) {         toast.maketext(view.getcontext(), "clicked country position = " + getadapterposition(), toast.length_short).show();      }  }  public void removeitem(int position) {     yeniliste.remove(position);     notifydatasetchanged(); }} 

bitmap bitmap = bitmapfactory.decodefile(yeniliste.get(position)); 

there problems in call:

first, not downscaling image when loading memory. not surprised if start lot of out of memory exceptions thrown out app.

second, loading image in ui thread, expensive operation , cause lag experiencing: app not able render new frame while it's loading image disk. need use background thread job. comon way use async task, 1 example described here.

but best option use library handle you. recommend glide. handle memory downscaling, background loading, cache fast reloading, plus has intuitive api.

this code:

bitmap bitmap = bitmapfactory.decodefile(yeniliste.get(position)); holder.countryphoto.setimage(imagesource.bitmap(bitmap).dimensions(50,50); 

will become:

glideapp.with(context).load(eniliste.get(position)).override(50,50).centercrop().into(new simpletarget<bitmap>(50, 50) {         @override         public void onresourceready(bitmap bitmap, glideanimation anim) {          holder.countryphoto.setimage(imagesource.bitmap(bitmap));         ); 

as not using simple imageview bitmap holder, need use custom target implementation.

i recomend reading docs or questions glide if having trouble implementing this.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -