我正在尝试使用 TransitionDrawable 来在播放/暂停图标之间切换。当我第一次点击播放按钮时,播放图标保持不变,暂停按钮出现在播放图标的顶部。当我再次单击时,暂停图标消失,播放按钮在那里。
代码:onClick运行这个方法
private void startOrPause() {
TransitionDrawable drawable = (TransitionDrawable) mPlayBtn.getDrawable();
if (!isPlaying) {
drawable.startTransition(300);
} else {
drawable.reverseTransition(300);
}
isPlaying = !isPlaying;
}
XML:转换
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_play"/>
<item android:drawable="@drawable/ic_pause"/>
</transition>
图像按钮:
<ImageButton android:id="@+id/audio_layout_play_imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@drawable/play_pause_transition"
android:background="@android:color/transparent"
android:layout_marginLeft="4dp"
/>
我查看了 android 示例代码和互联网上的一些指南,每个人都这样做。
谢谢你的帮助,
罗伊
最佳答案
我遇到了完全相同的问题,只出现在透明图像(32 位 PNG、GIF)上。 尝试使用 setCrossFadeEnabled :
TransitionDrawable drawable = (TransitionDrawable) mPlayBtn.getDrawable();
drawable.setCrossFadeEnabled(true);
关于android - TransitionDrawable 第一个元素不消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23538095/