0
votes

I have an imageview in a constraint layout and I have set all 4 of its constraints. Here's the xml:

    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/left_guideline_image_view"
        app:layout_constraintGuide_percent=".12077"
        android:orientation="vertical"/>

    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/right_guideline_image_view"
        app:layout_constraintGuide_percent=".87923"
        android:orientation="vertical"/>

    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/top_guideline_image_view"
        app:layout_constraintGuide_percent=".05580"
        android:orientation="horizontal"/>

    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bottom_guideline_image_view"
        app:layout_constraintGuide_percent=".31473"
        android:orientation="horizontal"/>



    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/image_view"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        app:layout_constraintLeft_toLeftOf="@+id/left_guideline_image_view"
        app:layout_constraintRight_toRightOf="@+id/right_guideline_image_view"
        app:layout_constraintTop_toTopOf="@+id/top_guideline_image_view"
        app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_image_view"/>

I have an image as a Bitmap object. I want to scale the image, while maintaining the aspect ratio, so that it fits the imageview (that is, one of its dimensions is equal to one of the dimensions of the imageview, and the other dimension is such that the aspect ratio is maintained). I tried following the instructions from here and I got a significant improvement over the orignal, but the image was still going off the top and bottom bounds of the imageview (it was touching the top of the screen) and the width was such that the aspect ratio was maintained.

Can this be done? If yes, how? If not, please tell me of a way to maintain this size of the image view for all screen sizes and also fit the image into the imageview.

1

1 Answers

2
votes

Your image view width and height is "wrap_content"

Have your tried changing it to "0dp"?