Tuesday, 15 April 2014

android - Getting size from view that places inside a ViewGroup with GONE visibility (programatically it will be set as VISIBLE) -



android - Getting size from view that places inside a ViewGroup with GONE visibility (programatically it will be set as VISIBLE) -

i'm trying image photographic camera device, save it, , show preview in imageview.

my imageview (named imagepreview) defined within next layout:

<linearlayout android:id="@+id/ll_selected_image" android:layout_width="match_parent" android:layout_height="300dp" android:orientation="vertical" android:layout_gravity="center_horizontal" android:visibility="gone"> <!-- @ first gone --> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent"> <imageview android:id="@+id/imagepreview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerinparent="true"/> </relativelayout> </linearlayout>

when image acquired camera, seek scale image next this document, code is:

private void setpic() { // dimensions of view int targetw = mimageview.getwidth(); int targeth = mimageview.getheight(); // dimensions of bitmap bitmapfactory.options bmoptions = new bitmapfactory.options(); bmoptions.injustdecodebounds = true; bitmapfactory.decodefile(mcurrentphotopath, bmoptions); int photow = bmoptions.outwidth; int photoh = bmoptions.outheight; // determine how much scale downwards 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(mcurrentphotopath, bmoptions); mimageview.setimagebitmap(bitmap); }

the problem obtain "division zero" mimageview.getwidth(); , mimageview.getheight(); homecoming zero.

note visibility of parent linearlayout setted gone. then, application sets programmatically visible.

if remove gone, , add together visible instead, works good!

the unusual thing have tried add together in setpic(), first code lines, next code:

if(ivselectedimage.getvisibility() == view.visible) log.i(tag,"is visible!"); else if(ivselectedimage.getvisibility() == view.gone) log.i(tag,"is gone!"); else if(ivselectedimage.getvisibility() == view.invisible) log.i(tag,"is invisible!");

and prints in console "is visible"!

briefly: if set gone in xml, , set visible (programmatically) getheight() , getwidth() can't right value (also if when phone call these methods view visible). while, if set visible in xml, methods getheight() , getwidth() homecoming me right values.

you don't need utilize view size @ runtime example. based on code, while view visibility set gone main container, linearlayout, fixed height of 300dp. result, know 1 of dimensions of view since @ best android:layout_height="match_parent" can equal 300dp.

to scale need screen width - since main view (and nested relativelayout , imageview set match_parent). if you're in activity utilize this

display display = getwindowmanager().getdefaultdisplay(); point size = new point(); display.getsize(size); int width = size.x;

otherwise can display using

windowmanager wm = (windowmanager) context.getsystemservice(context.window_service); display display = wm.getdefaultdisplay();

just run calculation before need set image. store results sharedpreferences or , utilize ratio 300dp height , screen width scale image.

android android-layout imageview android-linearlayout android-bitmap

No comments:

Post a Comment