How to adjust how much an ImageView is cropped by when using scaletype centerCrop on Android?

778 Views Asked by At

I have an ImageView (PhotoView) within a LinearLayout and I am applying scaletype center crop but is cropped a bit too much and appears too zoomed in. How can I resolve this?

       <LinearLayout
           android:id="@+id/photoViewContainer"
           android:layout_width="wrap_content"
           android:layout_height="@dimen/flexible_space_image_height"
           android:background="@android:color/holo_purple"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintVertical_bias="0.0">
               <com.github.chrisbanes.photoview.PhotoView
                   android:id="@+id/mainImageView"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:scaleType="centerCrop"
                   android:adjustViewBounds="true"
                   android:background="@android:color/black" />
       </LinearLayout>
1

There are 1 best solutions below

0
ansh sachdeva On

I am not sure if you can control the cropping, but using scaletype="fitXY" and providing some padding worked for me.

<com.github.chrisbanes.photoview.PhotoView
                   android:id="@+id/mainImageView"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:scaleType="fitXY"
                   android:padding="16dp"
                   android:adjustViewBounds="true"
                   android:background="@android:color/black" />

In this, the scaletype="fitXY" would squeeze the image to the size of screen and padding it would fit it in center.
Also there is a hack that i have heard of, in which you replace the padding by negative padding, which would zoom out the image, but i have not tried it.