how to use preloader in Grid View in android -
i want use preloader image in gridview images when loading form remote server. while loading server @ time want show preloader image this(same progress bar or progress bar).
i want show small progress bar there in gridview image item or preloader image dnt know can use me achieve this.
can please me how can thing in android.
i want make ios. image form ios.
here android layout xml file :
activity_image_grid.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <include android:id="@+id/title_bar" android:layout_alignparenttop="true" layout="@layout/activity_top_header_bar" /> <gridview android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/title_bar" android:gravity="center" android:horizontalspacing="4dip" android:numcolumns="4" android:padding="5dip" android:stretchmode="columnwidth" android:verticalspacing="4dip" /> </relativelayout>
this xml file used item each grid in gridview.
item_grid_image.xml
<?xml version="1.0" encoding="utf-8"?> <imageview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="75dp" android:adjustviewbounds="true" android:contentdescription="@string/descr_image" android:scaletype="centercrop" />
source code :
public class imagegridactivity extends baseactivity { private static final string tag = "[imagegridactivity]"; private displayimageoptions options; private pulltorefreshgridview mpullrefreshgridview; private gridview mgridview = null; arraylist<gallaryimage> mgridviewimageslist; private imageadapter mimageadapter = null; private string mimageurl = null; private string mgallarytitle = null; // private imageloader imageloader = imageloader.getinstance(); @override public void oncreate(final bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_image_grid); options = new displayimageoptions.builder() .showstubimage(r.drawable.photo_default) .showimageforemptyuri(r.drawable.photo_default) .showimageonfail(r.drawable.ic_error).cacheinmemory() .cacheondisc().bitmapconfig(bitmap.config.rgb_565).build(); final bundle bundle = getintent().getextras(); if (bundle != null) { mimageurl = bundle.getstring(constants.gallary_fetch_url); mgallarytitle = bundle.getstring(constants.gallary_type); if (mgallarytitle != null) { locale loc = locale.getdefault(); textview tvtitletext = (textview) findviewbyid(r.id.tv_title_bar_text); tvtitletext.settext(mgallarytitle.touppercase(loc)); } mpullrefreshgridview = (pulltorefreshgridview) findviewbyid(r.id.pull_refresh_grid); mpullrefreshgridview.setmode(mode.pull_from_start); mgridview = mpullrefreshgridview.getrefreshableview(); mgridviewimageslist = utility.getimageslist(mimageurl, imagegridactivity.this); if (mgridviewimageslist != null && !mgridviewimageslist.isempty()) { mimageadapter = new imageadapter(mgridviewimageslist); ((gridview) mgridview).setadapter(mimageadapter); } else { // did refresh after previous images loaded in // gridview. if (utility.checkconnection(imagegridactivity.this)) { log.i(tag, "wifi/internet connection found , have parse xml"); final fetchimagesasynctaskfeed asynctask = new fetchimagesasynctaskfeed(); asynctask.execute(mimageurl); } } mgridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(final adapterview<?> parent, final view view, final int position, final long id) { if (mgridviewimageslist != null && !mgridviewimageslist.isempty()) { startimagepageractivity(mgridviewimageslist, position); } else { log.d(tag, "there no image grid image"); } } }); // set listener invoked when list should refreshed. mpullrefreshgridview .setonrefreshlistener(new onrefreshlistener2<gridview>() { @override public void onpulldowntorefresh( pulltorefreshbase<gridview> refreshview) { if (mimageurl != null) { final fetchimagesasynctaskfeed asynctask = new fetchimagesasynctaskfeed(); asynctask.execute(mimageurl); } } @override public void onpulluptorefresh( pulltorefreshbase<gridview> refreshview) { } }); } } /** * @param position */ private void startimagepageractivity( final arraylist<gallaryimage> mimageattributeslist, final int position) { string[] urls = new string[mimageattributeslist.size()]; final intent intent = new intent(this, imagepageractivity.class); intent.putextra(constants.gallary_image_position_bundle_key, position); (int = 0; < mimageattributeslist.size(); i++) { urls[i] = mimageattributeslist.get(i).mimageurl; } intent.putextra(constants.gallary_images_image_bundle_key, urls); startactivity(intent); } public class imageadapter extends baseadapter { arraylist<gallaryimage> imagelist = null; public imageadapter(final arraylist<gallaryimage> imageattributeslist) { this.imagelist = imageattributeslist; } @override public int getcount() { return imagelist.size(); } @override public object getitem(final int position) { return imagelist.get(position); } @override public long getitemid(final int position) { return position; } @override public view getview(final int position, final view convertview, final viewgroup parent) { final imageview imageview; if (convertview == null) { imageview = (imageview) getlayoutinflater().inflate( r.layout.item_grid_image, parent, false); } else { imageview = (imageview) convertview; } imageloader.displayimage(imagelist.get(position).mimageurl, imageview, options); return imageview; } /** * @param updatedata */ public void updateddata(arraylist<gallaryimage> imglist) { this.imagelist = imglist; notifydatasetchanged(); } } private class fetchimagesasynctaskfeed extends asynctask<string, void, string> { @override protected void onpreexecute() { } @override protected string doinbackground(final string... urls) { try { thread.sleep(3000); final string imageurl = urls[0]; final gridviewimagesxmlhandler mgallaryxmlhandler = new gridviewimagesxmlhandler(); mgridviewimageslist = mgallaryxmlhandler.getimages(imageurl); if (mgridviewimageslist != null && !mgridviewimageslist.isempty()) { utility.setimagesinfromation(imageurl, mgridviewimageslist, imagegridactivity.this); } } catch (final exception e) { log.e(tag, "exception in fetch images url", e); } return null; } @override protected void onpostexecute(final string result) { if (mgridviewimageslist != null && !mgridviewimageslist.isempty()) { if (mimageadapter != null) { mimageadapter.updateddata(mgridviewimageslist); mpullrefreshgridview.onrefreshcomplete(); } else { mimageadapter = new imageadapter(mgridviewimageslist); ((gridview) mgridview).setadapter(mimageadapter); } } mpullrefreshgridview.onrefreshcomplete(); } } }
universal imageloader
https://github.com/nostra13/android-universal-image-loader
rowimage.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <imageview android:id="@+id/ivv" android:layout_gravity="center" android:layout_width="300dp" android:layout_height="300dp" /> <progressbar android:id="@+id/pb" android:layout_centerinparent="true" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </relativelayout>
in adapter constructor
imageloader imageloader; displayimageoptions options; file cachedir = storageutils.getowncachedirectory(a, "myraghu"); // singletone instance of imageloader imageloader = imageloader.getinstance(); // create configuration imageloader (all options optional) imageloaderconfiguration config = new imageloaderconfiguration.builder(a) // can pass own memory cache implementation .disccacheextraoptions(1024, 1024, compressformat.png, 100) .disccache(new unlimiteddisccache(cachedir)) // can pass own disc cache implementation .disccachefilenamegenerator(new hashcodefilenamegenerator()) .enablelogging() .build(); // initialize imageloader created configuration. once. imageloader.init(config); //imageloader.init(imageloaderconfiguration.createdefault(a)); // imageloader=new imageloader(activity.getapplicationcontext()); options = new displayimageoptions.builder() .showstubimage(r.drawable.ic_launcher) .cacheinmemory() .cacheondisc() .displayer(new roundedbitmapdisplayer(20)) .build();
in getview of custom adapter
public view getview(int position, view convertview, viewgroup parent) { view vi=convertview; if(convertview==null) vi = inflater.inflate(r.layout.rowimage, null); imageview image=(imageview)vi.findviewbyid(r.id.ivv); progressbar pb= (progressbar)vi.findviewbyid(r.id.pb); display(null, data.get(position).tostring(), pb); //imageloader.displayimage(data.get(position).tostring(), image,options); return vi; } public void display(imageview img, string url, final progressbar spinner) { imageloader.displayimage(url, img, options, new imageloadinglistener() { @override public void onloadingstarted(string imageuri, view view) { spinner.setvisibility(view.visible); } @override public void onloadingfailed(string imageuri, view view, failreason failreason) { spinner.setvisibility(view.gone); } @override public void onloadingcomplete(string imageuri, view view, bitmap loadedimage) { spinner.setvisibility(view.gone); } @override public void onloadingcancelled(string imageuri, view view) { } }); }
resulting snap shot have used listview should work gridview also.
first stub image displayed along progress bar. in case have used launcher icon looks stretched
once image downloaded progress bar dismissed , stub image replaced downloaded one. caches images.
Comments
Post a Comment