我有一个图像,我想水平平铺,我需要以编程方式完成它。两种方法都试过了,都不行。
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);
2:使用xml平铺图片
navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource(
getResources(), R.drawable.navigation_background));
navigationTextViews[id].setBackgroundDrawable(navigationBackground);
导航背景
<?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" />
我做错了什么?
我还在程序的另一部分使用 setBackgroundResource
设置更改背景颜色,我认为这是一个问题。我添加了 navigationTextViews[id].setBackgroundResource(0);
,它应该会删除背景资源,但它不适用于我使用的上述解决方案。
编辑:navigationTextViews[] 是一个 TextViews 数组
最佳答案
嗯,出于某种原因,第一个选项使用不同的图像,所以我猜问题是我的第一个图像(具有一定透明度的渐变)太透明了。
关于Android以编程方式平铺图像作为背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11199064/