我想在Android中创建一个带文本和背景图片的按钮。背景图片应该每隔X时间交替淡入淡出。
我已经使用TransitionDrawable和两张图片使其工作。
但是我无法让它使用超过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);
我在tile.xml文件中定义了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个的可绘制对象,但我无法让其正常工作。
我也尝试了不使用任何XML,而是使用纯JAVA,但是结果完全相同 :-(