android - Gridview populating issue with asynctask -
i'm trying populate gridview thumbnail images i'm having issues decoding images within asynctask. when start app, images loaded 1 1 , images not displayed correctly ( once scroll shows top image , loads original later). here code :
public class imageadapter extends baseadapter { private context mcontext; private list<string> mlist; private int mheight; private int mwidth; private inputstream is; public imageadapter(context context, list<string> list, int height, int width) { mcontext = context; mlist = list; mheight = height; mwidth = width; } @override public int getcount() { return mlist.size(); } @override public object getitem(int position) { return mlist.get(position).tostring(); } @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); } else { imageview = (imageview) convertview; } inputstream is; try { = mcontext.getassets().open(mlist.get(position)); loadimage task = new loadimage(imageview , mheight , mwidth); task.execute(is); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return imageview ; } public class loadimage extends asynctask<inputstream, void, bitmap>{ private final weakreference<imageview> imageviewreference; private inputstream = null; private int width; public loadimage(imageview imageview, int mheight, int mwidth) { imageviewreference = new weakreference<imageview>(imageview); this.width=mwidth; // todo auto-generated constructor stub } @override protected bitmap doinbackground(inputstream... params) { = params[0]; if (is !=null) { bitmap bitmap = bitmapfactory.decodestream(is); bitmap nbitmap =bitmap.createscaledbitmap(bitmap,width/3 , width/3, false); return nbitmap; } return null; } @override protected void onpostexecute(bitmap bitmap) { if (imageviewreference != null && bitmap != null) { final imageview imageview = imageviewreference.get(); if (imageview != null) { imageview.setimagebitmap(bitmap); imageview.setscaletype(imageview.scaletype.center_crop); } } } }
i highly recommend universal image loader. has need: gridview can loaded thumbnails. has memory , disk cache features, along logging , features thread pool sizes. give try; works me , should fit needs.
Comments
Post a Comment