java - How to set parsed image as background instead of src -
i have customized list view parses images lists reads them in src
<imageview android:id="@+id/list_image" android:layout_height="420dp" android:layout_width="fill_parent" android:src="@drawable/posty" android:clickable="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparentright="true" android:layout_alignparentend="true"/>
is there anyway set android:src="@drawable/posty" can android:background="@drawable/posty" parsed image thanks.
the adapter:
public class lazyadapter extends baseadapter { private activity activity; private arraylist<hashmap<string, string>> data; private static layoutinflater inflater=null; public imageloader imageloader; public lazyadapter(activity a, arraylist<hashmap<string, string>> d) { activity = a; data=d; inflater = (layoutinflater)activity.getsystemservice(context.layout_inflater_service); imageloader=new imageloader(activity.getapplicationcontext()); } public int getcount() { return data.size(); } public object getitem(int position) { return position; } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { view vi=convertview; if(convertview==null) vi = inflater.inflate(r.layout.list_row, null); textview title = (textview)vi.findviewbyid(r.id.title); // title textview artist = (textview)vi.findviewbyid(r.id.author); // name textview duration = (textview)vi.findviewbyid(r.id.duration); // description textview id = (textview)vi.findviewbyid(r.id.id); // section imageview thumb_image=(imageview)vi.findviewbyid(r.id.list_image); // thumb image hashmap<string, string> song = new hashmap<string, string>(); song = data.get(position); // setting values in listview id.settext(song.get(customizedlistview.key_id)); artist.settext(song.get(customizedlistview.key_artist)); duration.settext(song.get(customizedlistview.key_duration)); imageloader.displayimage(song.get(customizedlistview.key_thumb_url), thumb_image); return vi; }
}
and image loader:
public class imageloader { memorycache memorycache=new memorycache(); filecache filecache; private map<imageview, string> imageviews=collections.synchronizedmap(new weakhashmap<imageview, string>()); executorservice executorservice; public imageloader(context context){ filecache=new filecache(context); executorservice=executors.newfixedthreadpool(5); } final int stub_id = r.drawable.no_image; public void displayimage(string url, imageview imageview) { imageviews.put(imageview, url); bitmap bitmap=memorycache.get(url); if(bitmap!=null) imageview.setimagebitmap(bitmap); else { queuephoto(url, imageview); imageview.setimageresource(stub_id); } } private void queuephoto(string url, imageview imageview) { phototoload p=new phototoload(url, imageview); executorservice.submit(new photosloader(p)); } private bitmap getbitmap(string url) { file f=filecache.getfile(url); //from sd cache bitmap b = decodefile(f); if(b!=null) return b; //from web try { bitmap bitmap=null; url imageurl = new url(url); httpurlconnection conn = (httpurlconnection)imageurl.openconnection(); conn.setconnecttimeout(30000); conn.setreadtimeout(30000); conn.setinstancefollowredirects(true); inputstream is=conn.getinputstream(); outputstream os = new fileoutputstream(f); utils.copystream(is, os); os.close(); bitmap = decodefile(f); return bitmap; } catch (exception ex){ ex.printstacktrace(); return null; } } //decodes image , scales reduce memory consumption //decodes image , scales reduce memory consumption private bitmap decodefile(file f){ try { return bitmapfactory.decodestream(new fileinputstream(f)); } catch (filenotfoundexception e) {} return null; } //task queue private class phototoload { public string url; public imageview imageview; public phototoload(string u, imageview i){ url=u; imageview=i; } } class photosloader implements runnable { phototoload phototoload; photosloader(phototoload phototoload){ this.phototoload=phototoload; } @override public void run() { if(imageviewreused(phototoload)) return; bitmap bmp=getbitmap(phototoload.url); memorycache.put(phototoload.url, bmp); if(imageviewreused(phototoload)) return; bitmapdisplayer bd=new bitmapdisplayer(bmp, phototoload); activity a=(activity)phototoload.imageview.getcontext(); a.runonuithread(bd); } } boolean imageviewreused(phototoload phototoload){ string tag=imageviews.get(phototoload.imageview); if(tag==null || !tag.equals(phototoload.url)) return true; return false; } //used display bitmap in ui thread class bitmapdisplayer implements runnable { bitmap bitmap; phototoload phototoload; public bitmapdisplayer(bitmap b, phototoload p){bitmap=b;phototoload=p;} public void run() { if(imageviewreused(phototoload)) return; if(bitmap!=null) phototoload.imageview.setimagebitmap(bitmap); else phototoload.imageview.setimageresource(stub_id); } } public void clearcache() { memorycache.clear(); filecache.clear(); }
you can use setbackgrounddrawable
. create bitmapdrawable
bitmap
:
bitmapdrawable bd = new bitmapdrawable(yourbitamp); imageview.setbackgrounddrawable(bd);
edit:
public void displayimage(string url, imageview imageview) { imageviews.put(imageview, url); bitmap bitmap=memorycache.get(url); if(bitmap!=null) { bitmapdrawable bd = new bitmapdrawable(bitmap); imageview.setbackgrounddrawable(bd); } else { queuephoto(url, imageview); imageview.setimageresource(stub_id); } }
Comments
Post a Comment