Search code examples
androidimageviewandroid-imageview

Keep the drawable smaller than the imageview


I have a vector drawable image, that its size is 24dp. I would like to show the image inside an Imageview that is larger but keep the drawable at the center at size 24dp. The following makes the drawable bigger:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/the_image_drawable"
        />

</LinearLayout>

What am I doing wrong?


Solution

  • ImageView has some tricky param: ScaleType. by default it is set to FIT_CENTER, so in fact Matrix.ScaleToFit.CENTER. from doc:

    Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.

    try to exchange this attr to CENTER or CENTER_INSIDE, these aren't using scaling at all

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/the_image_drawable"
        android:scaleType="center"/>