AnimatedStateListDrawable 显示不正确的动画

AnimatedStateListDrawable shows incorrect animation

我想制作一个在 selected/unselected 或不在时动画的动画可绘制对象。 "selected" -> "not selected" 和 "not selected" -> "selected" 有不同的动画 我的问题是当我 select 我的视图时,它触发了正确的动画,但是当我 unselect 它会反向触发相同的动画而不是正确的动画。有任何想法吗?

<?xml version="1.0" encoding="utf-8"?>
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- provide a different drawable for each state-->
<item
    android:id="@+id/not_selected"
    android:state_selected="false"
    android:drawable="@drawable/vector_bars" />

<item
    android:id="@+id/selected"
    android:state_selected="true"
    android:drawable="@drawable/vector_arrow" />



<!-- specify a transition -->
<transition
    android:fromId="@id/selected"
    android:toId="@id/not_selected"
    android:drawable="@drawable/anim_arrow_to_bars"
    android:reversible="false"/>

<transition
    android:fromId="@id/not_selected"
    android:toId="@id/selected"
    android:drawable="@drawable/anim_bars_to_arrow"
    android:reversible="false"/>

</animated-selector>

好的,经过更广泛的搜索,我找到了答案。我添加了 2 个额外的状态并稍微更改了转换,现在它按预期工作了。

<?xml version="1.0" encoding="utf-8"?>
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- provide a different drawable for each state-->
<item
    android:id="@+id/not_selected_pressed"
    android:state_selected="false"
    android:state_pressed="true"
    android:drawable="@drawable/vector_bars" />

<item
    android:id="@+id/selected_pressed"
    android:state_selected="true"
    android:state_pressed="true"
    android:drawable="@drawable/vector_arrow" />

<item
    android:id="@+id/not_selected"
    android:state_selected="false"
    android:drawable="@drawable/vector_bars" />

<item
    android:id="@+id/selected"
    android:state_selected="true"
    android:drawable="@drawable/vector_arrow" />



<!-- specify a transition -->
<transition
    android:fromId="@id/selected_pressed"
    android:toId="@id/not_selected"
    android:drawable="@drawable/anim_arrow_to_bars"
    android:reversible="false"/>

<transition
    android:fromId="@id/selected_pressed"
    android:toId="@id/selected"
    android:drawable="@drawable/anim_bars_to_arrow"
    android:reversible="false"/>

</animated-selector>