Center a bitmap and repeat the edge pixel

为君一笑 提交于 2019-12-07 04:59:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6371540/center-a-bitmap-and-repeat-the-edge-pixel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!