Search code examples
androidandroid-layoutimageviewandroid-imageviewcrop

Set a bitmap as background and crop it


I'm trying to set a bitmap image as background of a FrameLayout.

<FrameLayout
    android:id="@+id/game_list_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>

    <LinearLayout
        android:id="@+id/game_list_header_cnt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/game_list_header_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/game_list_header_sub_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </LinearLayout>
</FrameLayout>

The problem with this is that the height of the image I want to set as background is bigger than the height of the two TextView's so the height of game_list_header doesn't fit to to the TextView's height but the ImageView height.

I tryied different approaches, such as using the bitmap as a DrawableBitmap with gravity=clip_vertical|fill_horizontal but even in that way, the height of the image doesn't fit with the TextView's height, causing the FrameLayout is bigger than I want.


Solution

  • Have you tried to set image as a background of game_list_header_cnt?

    If that does not help, you can simply use ConstraintLayout and make imageView top_top of a top textView and bot_bot of bot textView