java - Cannot take view size using view tree observer -
i tying width , height of view in adapter, while view not yet inflated, using viewtreeobserver, end 0 values. width , height going used in method resizes bitmap of imageview in listview in adapter. here code:
public view getview(int position, view convertview, viewgroup parent) { newview = convertview; viewholder holder; if (null == convertview) { holder = new viewholder(); newview = inflater.inflate(r.layout.selfie_list_view, null); holder.selfieview = (imageview) newview.findviewbyid(r.id.selfie_bitmap); holder.selfiename = (textview) newview.findviewbyid(r.id.selfie_name); newview.settag(holder); } else { holder = (viewholder) newview.gettag(); } selfierecord curr = list.get(position); string selfiename = curr.getbitmapname(); string selfiepath = curr.getbitmappath(); bitmap selfie = setpic(newview, selfiepath); holder.selfieview.setimagebitmap(selfie); holder.selfiename.settext(selfiename); return newview; } private bitmap setpic(final view view, string path) { viewtreeobserver vto = view.getviewtreeobserver(); if (vto.isalive()) { vto.addongloballayoutlistener(new ongloballayoutlistener() { @override public void ongloballayout() { view.getviewtreeobserver().removeongloballayoutlistener(this); targetw = view.getwidth(); targeth = view.getheight(); log.i(tag, "view_width = " + targetw + " view_height =" + targeth); } }); } // dimensions of bitmap bitmapfactory.options bmoptions = new bitmapfactory.options(); bmoptions.injustdecodebounds = true; bitmapfactory.decodefile(path, bmoptions); int photow = bmoptions.outwidth; int photoh = bmoptions.outheight; // determine how scale down image int scalefactor = math.min(photow/targetw, photoh/targeth); // decode image file bitmap sized fill view bmoptions.injustdecodebounds = false; bmoptions.insamplesize = scalefactor; bmoptions.inpurgeable = true; bitmap bitmap = bitmapfactory.decodefile(path, bmoptions); return bitmap; }
any ideas appreciated.
well solved using onpredrawlistener , putting bitmap decoding , imageview inside it. here's solution in case needs it. thank anyway:
public view getview(int position, view convertview, viewgroup parent) { newview = convertview; final viewholder holder; if (null == convertview) { holder = new viewholder(); newview = inflater.inflate(r.layout.selfie_list_view, null); holder.selfieview = (imageview) newview.findviewbyid(r.id.selfie_bitmap); holder.selfiename = (textview) newview.findviewbyid(r.id.selfie_name); newview.settag(holder); } else { holder = (viewholder) newview.gettag(); } selfierecord curr = list.get(position); string selfiename = curr.getbitmapname(); final string selfiepath = curr.getbitmappath(); //bitmap selfie = setpic(newview, selfiepath); viewtreeobserver vto = holder.selfieview.getviewtreeobserver(); vto.addonpredrawlistener(new onpredrawlistener() { @override public boolean onpredraw() { holder.selfieview.getviewtreeobserver().removeonpredrawlistener(this); int width = holder.selfieview.getmeasuredwidth(); int height = holder.selfieview.getmeasuredheight(); log.i(tag, "view_width = " + width + " view_height =" + height); bitmapfactory.options bmoptions = new bitmapfactory.options(); bmoptions.injustdecodebounds = true; bitmapfactory.decodefile(selfiepath, bmoptions); int photow = bmoptions.outwidth; int photoh = bmoptions.outheight; log.i(tag, "photo_width = " + photow + " view_height =" + photoh); // determine how scale down image int scalefactor = math.min(photow/width, photoh/height); // decode image file bitmap sized fill view bmoptions.injustdecodebounds = false; bmoptions.insamplesize = scalefactor; bmoptions.inpurgeable = true; bitmap bitmap = bitmapfactory.decodefile(selfiepath, bmoptions); holder.selfieview.setimagebitmap(bitmap); return true; } }); holder.selfiename.settext(selfiename); return newview; }
Comments
Post a Comment