Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: layout_width and height crop my image

i have this little layout i defined to inserted in every page of my app:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageButton
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="10dp"
    android:background="@null"
    android:src="@drawable/sa_info"
    android:layout_alignParentRight="true"/>

i've sa_info.png that is 118x118 but i want it to look much smaller on the screen. if i try to set width and height of the imagebutton to the desired size (lets say 48dp) the resulting image when i run the app is cropped to the center: original image cropped image

so the only way i have to get the desired result is scale the image itself to the desired resolution. what i'm doing wrong, is there a better way to do this?

like image 562
jack_the_beast Avatar asked Apr 27 '26 09:04

jack_the_beast


1 Answers

Try adding

android:scaleType="fitCenter" -> http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

android:adjustViewBounds="true" -> Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

like image 199
Alex Avatar answered Apr 28 '26 21:04

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!