Scale image keeping its aspect ratio in background drawable

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:

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    
            android:src="https://stackoverflow.com/questions/9891065/@drawable/backgrnd"
            android:scaleType="centerCrop" />
    
        ...
    
        rest layout components here
    
        ...
    
    </RelativeLayout>
    

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)