Android background with gradient and tiled image

2020-06-12 03:24发布

For a linearLayout, I want to have a gradient along with a tiled (repeated) image in the background. I already have a shape xml set as the background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:angle="90"
        android:endColor="@color/color1"
        android:startColor="@:color/color2">
    </gradient>
</shape>

How do I add a tiled bg image?

1条回答
Lonely孤独者°
2楼-- · 2020-06-12 03:44

Check out LayerLists.

The following is an XML drawable, called myBackground.xml, placed in res/drawable. Set it as the background of the View you want to set a gradient and tiled background for.

In the example below, the tiled image will be on top of the gradient, because it is specified later in the LayerList - obviously if it's on top, you'll need some transparency on the png image tile (you can set this in an image editing application, like GIMP or Photoshop).

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:type="radial" android:gradientRadius="500"
                android:startColor="#17568A"
                android:endColor="#494C4F" />  

        </shape>
    </item>
    <item>
        <bitmap
            android:src="@drawable/tile_classy_fabric"
            android:tileMode="repeat" />
    </item>
</layer-list>

"tile_classy_fabric" refers to a file called "tile_classy_fabric.png" in my res/drawable folder (250px square, as it's tileable - we don't need it super large).

查看更多
登录 后发表回答