Scale an image ,keeping aspect ratio without being cropped-android app -
i take photo gallery , display on activity in image view . it's fine except when photo displayed. want photo displayed in exact same size image view keeping aspect ratio , not cropped. tried in xml(centercrop,fitxy...)this i'm trying,but not working.
showphotoactivity.class:
imageview showphoto = (imageview) findviewbyid(r.id.image); bundle extras = getintent().getextras(); uri uri = uri.parse(extras.getstring("imagepath")); if (showphoto != null) { showphoto.setadjustviewbounds(true); showphoto.setimageuri(uri); }
file.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.ga.photoeditor.showphotoactivity" android:background="#93212121"> <com.example.ga.photoeditor.scaleimageview android:id="@+id/image" android:layout_width="match_parent" android:layout_height="320dp" android:layout_centervertical="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" />
scaleimageview.java
public scaleimageview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } public scaleimageview(context context, attributeset attrs) { super(context, attrs); } public scaleimageview(context context) { super(context); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { // call super() resolveuri() called. super.onmeasure(widthmeasurespec, heightmeasurespec); // if there's no drawable can use result super. if (getdrawable() == null) return; final int widthspecmode = measurespec.getmode(widthmeasurespec); final int heightspecmode = measurespec.getmode(heightmeasurespec); int w = getdrawable().getintrinsicwidth(); int h = getdrawable().getintrinsicheight(); if (w <= 0) w = 1; if (h <= 0) h = 1; // desired aspect ratio of view's contents (not including padding) float desiredaspect = (float) w / (float) h; // allowed change view's width boolean resizewidth = widthspecmode != measurespec.exactly; // allowed change view's height boolean resizeheight = heightspecmode != measurespec.exactly; int pleft = getpaddingleft(); int pright = getpaddingright(); int ptop = getpaddingtop(); int pbottom = getpaddingbottom(); // sizes imageview decided on. int widthsize = getmeasuredwidth(); int heightsize = getmeasuredheight(); if (resizewidth && !resizeheight) { // resize width height, maintaining aspect ratio. int newwidth = (int) (desiredaspect * (heightsize - ptop - pbottom)) + pleft + pright; setmeasureddimension(newwidth, heightsize); } else if (resizeheight && !resizewidth) { int newheight = (int) ((widthsize - pleft - pright) / desiredaspect) + ptop + pbottom; setmeasureddimension(widthsize, newheight); } } }
are aware of available attribute, "scaletype?"
<com.example.ga.photoeditor.scaleimageview android:id="@+id/image" android:layout_width="match_parent" android:layout_height="320dp" android:layout_centervertical="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" />
experiment inserting:
android:scaletype= [center, centercrop, centerinside, fitcenter, fitend, fitstart, fitxy, or matrix]
it seems you're possibly trying change dimensions of imageview itself... different, apologize if i'm missing mark.
Comments
Post a Comment