android - How to dynamically load images in GridView or how to optimazi it's work? -


my application has gridview control display photos directory. if directory consist of 1000 or 2000 photos, gridview slow, because of loading photos @ once.

введите сюда описание изображения

is possible implement gradual loading of photos, example, after view 100 photos, loaded 100? or there more ways optimize speed of gridview? grateful advice.

my code

activity_main.xml

the piece of code demonstrate gridview  <gridview     android:id="@+id/gridview"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:columnwidth="90dp"     android:numcolumns="auto_fit"     android:verticalspacing="10dp"     android:horizontalspacing="10dp"     android:stretchmode="columnwidth"     android:gravity="center"/> 

mainactivity.java

public class mainactivity extends activity {  imageadapter myimageadapter;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      gridview gridview = (gridview) findviewbyid(r.id.gridview);     myimageadapter = new imageadapter(this);     gridview.setadapter(myimageadapter);      string externalstoragedirectorypath = environment             .getexternalstoragedirectory()             .getabsolutepath();      string targetpath = externalstoragedirectorypath + "/dcim/100andro";      toast.maketext(getapplicationcontext(), targetpath, toast.length_long).show();     file targetdirector = new file(targetpath);      file[] files = targetdirector.listfiles();     (file file : files){         myimageadapter.add(file.getabsolutepath());     } }  public class imageadapter extends baseadapter {      private context mcontext;     arraylist<string> itemlist = new arraylist<string>();      public imageadapter(context c) {         mcontext = c;     }      void add(string path){         itemlist.add(path);     }      @override     public int getcount() {         return itemlist.size();     }      @override     public object getitem(int arg0) {         return null;     }      @override     public long getitemid(int position) {         return 0;     }      @override     public view getview(int position, view convertview, viewgroup parent) {         imageview imageview;         if (convertview == null) {             imageview = new imageview(mcontext);             imageview.setlayoutparams(new gridview.layoutparams(220, 220));             imageview.setscaletype(imageview.scaletype.center_crop);             imageview.setpadding(8, 8, 8, 8);         } else {             imageview = (imageview) convertview;         }          bitmap bm = decodesampledbitmapfromuri(itemlist.get(position), 220, 220);          imageview.setimagebitmap(bm);         return imageview;     }      public bitmap decodesampledbitmapfromuri(string path, int reqwidth, int reqheight) {          bitmap bm = null;          final bitmapfactory.options options = new bitmapfactory.options();         options.injustdecodebounds = true;         bitmapfactory.decodefile(path, options);          options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight);          options.injustdecodebounds = false;         bm = bitmapfactory.decodefile(path, options);          return bm;     }      public int calculateinsamplesize(              bitmapfactory.options options, int reqwidth, int reqheight) {         final int height = options.outheight;         final int width = options.outwidth;         int insamplesize = 1;          if (height > reqheight || width > reqwidth) {             if (width > height) {                 insamplesize = math.round((float)height / (float)reqheight);             } else {                 insamplesize = math.round((float)width / (float)reqwidth);             }         }          return insamplesize;     }  } 

}

you should use library load images, known ones glide (https://github.com/bumptech/glide) , picasso (http://square.github.io/picasso/). both of handle cache you, , lazy loading grid should work flawlessly.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -