具有淡入/淡出持续时间的Android选择器最初不可见 [英] Android selector with fade in / fade out duration initially invisible

查看:110
本文介绍了具有淡入/淡出持续时间的Android选择器最初不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to achieve that an icon in ActionBar will not change states discretely, but by fading animation. When I add android:enterFadeDuration and android:exitFadeDuration to the selector tag, my drawable is initially invisible - when I tap it, it changes state to state_pressed (properly with enter fade duration) and when I release it, it turns back to its normal visible unselected state.

我肯定缺少明显的东西,或者这是某种错误吗?

I must be missing something obvious, or is this a bug of some kind?

<?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>

推荐答案

我遇到了一个类似问题,我的代码如下:

I had a similar problem, with my code looking like this:

<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>

起初,我发现了摆脱enterFadeDuration的提示,仅使用exitFadeDuration.最初的隐形解决了这个问题,但是在第一次曝光期间,视野仍然逐渐消失.

At first, I found a hint to get rid of enterFadeDuration and only use exitFadeDuration. That solved the problem with initial invisibility, but the view still faded into invisibility during the first interraction.

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

Then, I modified my structure as follows:

<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>

基本上,我只是将默认的drawable从选择器中推出.这是一种解决方法,它也适用于具有多个状态的选择器,但有一些值得注意的限制:

Basically, I just pushed the default drawable out of the selector. It's a workaround and it also works for selectors with multiple states, but has some notable limitations:

  • 默认可绘制对象作为始终可见作为底层.它适用于不透明的颜色,但是透明度可能会导致不良的结果.
  • 如果视图以选择器测试的状态之一开始,由于选择器仍以不可见的状态显示,因此默认情况下仍会显示为默认.
  • The default drawable is always visible as a bottom layer. It works for opaque colors, but transparency may cause undesirable results.
  • If the view starts in one of the states tested by selector, in still displays as default, because the selector still starts as invisible.

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

It might not be applicable to the original problem, but it's something to consider for overcoming this behaviour of selectors.

这篇关于具有淡入/淡出持续时间的Android选择器最初不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆