Android背景渐变和平铺图像

11

对于一个线性布局 (linearLayout),我想要在其背景上使用渐变和平铺(重复)的图像。我已经将一个形状 XML 设置为了背景。

<?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>

如何添加平铺背景图像?


请查看此链接以获取详细解释:http://androidblogger.blogspot.com/2009/01/how-to-have-tiled-background-cont.html - Xavi Gil
我知道如何分别做到它们,但我不知道如何将它们同时实现。 - Taranfx
1
好的,抱歉。那也许这个问题可以帮到你。我会像我之前贴出的第一个链接中所解释的一样,在位图XML文件中使用平铺图像,然后尝试在画布上添加渐变色。就像这个Stack Overflow的另一个问题那样。希望能帮到你。 - Xavi Gil
1个回答

26

查看LayerLists

下面是一个名为myBackground.xml的XML drawable,位于res/drawable中。 将其设置为您要设置渐变和平铺背景的View的背景。

在下面的示例中,平铺图像将位于渐变图像的上方,因为它稍后在LayerList中指定 - 显然,如果它在最上层,则需要在png图像块上设置一些透明度(可以在图像编辑应用程序中进行设置,如GIMP或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"是指我的res/drawable文件夹中名为"tile_classy_fabric.png"的文件(大小为250px,因为它是可平铺的 - 我们不需要特别大)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接