Android以编程方式平铺图像作为背景

hee*_*ero 8 android background image

我有一个想要水平平铺的图像,我需要以编程方式完成.我尝试了两种方法,两种方法都不起作用.

button_inner_shadow是图像navigation_background是用于平铺上面图像的xml

1:直接平铺图像

BitmapDrawable navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource(
                getResources(), R.drawable.button_inner_shadow));
navigationBackground.setTileModeX(Shader.TileMode.REPEAT);
navigationTextViews[id].setBackgroundDrawable(navigationBackground);
Run Code Online (Sandbox Code Playgroud)

2:使用xml来平铺图像

navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource(
                getResources(), R.drawable.navigation_background));
navigationTextViews[id].setBackgroundDrawable(navigationBackground);
Run Code Online (Sandbox Code Playgroud)

navigation_background

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true"
    android:src="@drawable/button_inner_shadow"
    android:tileMode="repeat" />
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

我还setBackgroundResource用来设置改变程序另一部分的背景颜色,并认为这是一个问题.我添加了navigationTextViews[id].setBackgroundResource(0);,它应该删除后台资源,这不适用于我使用的上述解决方案.

编辑:navigationTextViews []是一个TextViews数组

hee*_*ero 4

好吧,由于某种原因,第一个选项适用于不同的图像,所以我猜测问题是我的第一个图像(具有一定透明度的渐变)太透明。