Android ImageView ScaleType *FIT_TOP* -
i trying implement imageview holds both landscape or portrait images. images should fit width of imageview (if landscape) or height (if portrait) in case must aligned top of view no margin or padding.
what achieve android:scaletype="fitstart"
centered in case of portrait images or aligned top in case of landscape images.
added:
now using such ugly code, seems work, not sure best solution:
<com.custom.layout.myimageview android:id="@+id/detail_view_image" android:src="@drawable/logo" android:background="#fff" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerhorizontal="true" android:croptopadding="false" android:adjustviewbounds="false" />
and in class, extends imageview do:
@override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { int imgwidth = getdrawable().getintrinsicwidth(); int imgheight = getdrawable().getintrinsicheight(); if(imgwidth>imgheight) setscaletype(scaletype.fit_start); else setscaletype(scaletype.fit_center); int width = measurewidth(widthmeasurespec); int height = measureheight(heightmeasurespec); setmeasureddimension(width, height); }
instead of overriding native views place snippet in oncreate() method changes scaletype of imageview according orientation of phone.
some example snippet(i not sure works idea), in oncreate method:
imageview imgview = findviewbyid(r.id.myimage); //portrairt orientation if(getresources().getconfiguration().orientation == configuration.orientation_portrait) { imgview.setscaletype(scaletype.fit_center); } else { //landscape orientation imgview.setscaletype(scaletype.fit_start); }
so when orientation change happening oncreate called again , change view's scale type , if overriding onconfigurationchanged() should add above snippet again wherever want. try , let me know if worked
Comments
Post a Comment