Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center a bitmap and repeat the edge pixel

I am trying to use an image as background in my android app. If the image doesn't fit the screen I want the image to be centered horizontally and topped vertically. The remaining screen area should be filled by repeating the edges.

My layout xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:background="@drawable/background_image"
>
    <ScrollView
        android:layout_width="fill_parent" android:layout_height="fill_parent"
    >
    ...
    </ScrollView>
</LinearLayout>    

I tried to mark the left and right column as well as the top row of pixels as stretchable using draw9patch. That seems to work on smaller devices, but not on Galaxy Tab 10.1 and Motorola Xoom. The edge is repeated vertically but not horizontally.

I also tried to create an xml-drawable with tileMode="clamp"

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:filter="true"
    android:gravity="center"
    android:tileMode="clamp"
    android:src="@drawable/background"
/>

but that doesn't allow me to center the image horizontally. How can I combine clamp and center_horizontal?

like image 956
Michael Kowhan Avatar asked Jun 16 '11 12:06

Michael Kowhan


1 Answers

Per the documentation "Gravity is ignored when the tile mode is enabled."

A custom drawable can be used to do this. Here is an example of clamping on the left and right with a horizontally centered image.

like image 140
Jim Baca Avatar answered Oct 12 '22 15:10

Jim Baca