android - TransitionDrawable 第一个元素不消失

标签 android android-transitions

我正在尝试使用 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"
        />

play on top of pause after transition

我查看了 android 示例代码和互联网上的一些指南,每个人都这样做。

谢谢你的帮助,

罗伊

最佳答案

我遇到了完全相同的问题,只出现在透明图像(32 位 PNG、GIF)上。 尝试使用 setCrossFadeEnabled :

TransitionDrawable drawable = (TransitionDrawable) mPlayBtn.getDrawable();
drawable.setCrossFadeEnabled(true);

关于android - TransitionDrawable 第一个元素不消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23538095/

相关文章:

android - 使用 drawableLeft 将动画应用于 Drawable 集

android - 在 Android 中 fragment 发生变化时如何使用 Material Design Transition?

android - 为什么我的 Android 转换会忽略 TransitionListener?

返回 2 个屏幕时,Android Activity 返回过渡刹车

java - BufferedReader vs Scanner,FileInputStream vs FileReader?

android - 如何在 Google Maps Android API 实用程序库中刷新热图图层的点?

java - 如何设置从数据库获取数据的超时?

java - fragment 转换 : shared elements

android - 替换 View 之间的共享元素转换

android - 下载 hdpi、mdpi 和 ldpi 可绘制对象并使用它们