我想在Android中创建一个带有文本和背景图像的按钮。背景图像应该每X次交叉淡入淡出。Android TransitionDrawable与多个项目
我有这个工作使用TransitionDrawable 2图像。
但我不能得到这与超过2图像工作。
我有什么:
在Java代码中创建一个按钮,并设置一个背景(这是在XML定义的TransitionDrawable)。我开始转型。
final Button b = new Button(getApplicationContext());
b.setTextColor(getResources().getColor(R.color.white));
b.setText("Some text");
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.tile));
StateListDrawable background = (StateListDrawable) b.getBackground();
TransitionDrawable td = (TransitionDrawable) background.getCurrent();
td.startTransition(2000);
在XML我tile.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#449def" />
</shape>
</item>
<item android:drawable="@drawable/transition">
<shape>
<solid
android:color="#0000ff" />
</shape>
</item>
</selector>
最后一个transition.xml定义
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/desert"/>
<item android:drawable="@drawable/hydrangeas" />
<item android:drawable="@drawable/jellyfish" />
</transition>
现在的效果是,当我启动应用程序显示在沙漠图像。此图像应该交叉淡化绣球图像。但水母图像从不显示。
在TransitionDrawables的文档中声明,您可以指定2个以上的drawable,但我无法使其工作。
我也试过这个没有任何的XML,但在纯JAVA但这恰好给了:-(
*在文档做它TransitionDrawables中指出,可以指定超过2绘项目* - 你可以提供一个链接到这个陈述? – Luksprog 2013-03-27 10:45:02
http://developer.android.com/reference/android/graphics/drawable/TransitionDrawable.html#TransitionDrawable(android.graphics.drawable.Drawable []) 这表明“至少2层是必需的这个drawable to好好工作。”。 正如我在原来的帖子中所说,我也尝试了一切纯Java代码(因此有效地使用这个构造函数),但这有完全相同的问题。 – Knarf 2013-03-27 11:17:47
单词的错误选择。我刚刚查看了“TransitionDrawable”的代码,它只在两个drawable之间淡出,其他每一层都被忽略。 – Luksprog 2013-03-27 11:47:51