具有淡入/淡出持续时间的Android选择器最初不可见

Axa*_*dax 11 android android-drawable

我试图实现ActionBar中的图标不会离散地改变状态,而是通过淡化动画.当我添加android:enterFadeDurationandroid:exitFadeDuration在选择标签,我绘制最初是不可见的 -当我点击它,它改变状态state_pressed(正确地进入衰落持续时间),当我释放它,它返回其正常可见未选中状态.

我必须遗漏一些明显的东西,或者这是某种错误?

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="150" android:exitFadeDuration="150">
    <item android:drawable="@drawable/filters_toggle_icon_selected" android:state_focused="true"/>
    <item android:drawable="@drawable/filters_toggle_icon_selected" android:state_pressed="true"/>
    <item android:drawable="@drawable/filters_toggle_icon" android:state_focused="false" android:state_pressed="false"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

Kab*_*5CZ 5

我有类似的问题,我的代码看起来像这样:

<selector xmlns:android="http://schemas.android.com/apk/res/android" 
          android:enterFadeDuration="@android:integer/config_mediumAnimTime"
          android:exitFadeDuration="@android:integer/config_mediumAnimTime" >
    <item android:state_pressed="true" android:drawable="@color/pressed" />
    <item android:drawable="@color/default" />
</selector>
Run Code Online (Sandbox Code Playgroud)

起初,我发现了一个提示摆脱,enterFadeDuration只能使用exitFadeDuration.这解决了初始隐身的问题,但在第一次交互期间,视图仍然逐渐变为隐形.

然后,我修改了我的结构如下:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/default" />
    <item>
        <selector android:enterFadeDuration="@android:integer/config_mediumAnimTime"
                  android:exitFadeDuration="@android:integer/config_mediumAnimTime" >
            <item android:state_pressed="true" android:drawable="@color/pressed" />
        </selector>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

基本上,我只是将默认的drawable推出选择器.这是一种解决方法,它也适用于具有多个状态的选择器,但有一些明显的局限性:

  • 默认情况下绘制的始终可见作为底层.它适用于不透明的颜色,但透明度可能会导致不良结果.
  • 如果视图以选择器测试的状态之一开始,则仍显示为默认值,因为选择器仍然以不可见的方式启动.

可能不适用于原始问题,但是要克服选择器的这种行为需要考虑.


小智 3

使用android:enterFadeDuration="@android:integer/config_mediumAnimTime"android:exitFadeDuration="@android:integer/config_mediumAnimTime".