tilex tilex - 4 years ago 223
Android Question

Scale image keeping its aspect ratio in background drawable

How do I make a background image fit the view but keep its aspect ratio when using

<bitmap />
as a background drawable xml?
None of
values gives the desired effect.

Answer Source

It is impossible to achieve manipulating background attribute within xml-files only. There are two options:

  1. You cut/scale the bitmap programmatically with Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) and set it as some View's background.

  2. You use ImageView instead of background placing it as the first layout's element and specify android:scaleType attribute for it:

        android:layout_height="fill_parent" >
            android:scaleType="centerCrop" />
        rest layout components here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download