Android programmatically tile image as background
I have an image that I want to tile horizontally and I need it done programmatically. I have tried two ways, and both do not work.
button_inner_shadow is the image navigation_background is the xml that suppose to tile the above image
1: tile image directly
BitmapDrawable navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource(
getResources(), R.drawable.button_inner_shadow));
navigationBackground.setTileModeX(Shader.TileMode.REPEAT);
navigationTextViews[id].setBackgroundDrawable(navigationBackground);
2: use xml to tile image
navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource(
getResources(), R.drawable.navigation_background));
navigationTextViews[id].setBackgroundDrawable(navigationBackground);
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" />
What am I doing wrong?
I also use setBackgroundResource
to set change the background color in another part of the program and thought that was a problem. I have added navigationTextViews[id].setBackgroundResource(0);
, which should remove the background resource, and that doesn't work with the above solutions I used.
EDIT: navigationTextViews[] is an array of TextViews
那么,出于某种原因,第一个选项使用了不同的图像,所以我猜测问题是我的第一个图像(这是一个具有某种透明度的渐变)太透明了。
链接地址: http://www.djcxy.com/p/10936.html