【问题标题】:Animate selector/state transitions动画选择器/状态转换
【发布时间】:2012-03-02 13:17:16
【问题描述】:

我的 ListView 有一个简单的选择器

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

    <item android:drawable="@drawable/yellow_arc" android:state_activated="true"/>
    <item android:drawable="@drawable/yellow_nonarc" android:state_activated="false"/>

</selector>

当视图的状态从激活变为未激活时,我想为这些可绘制对象之间的过渡设置动画,反之亦然。

如果您在 API 演示中运行 example,您将看到一个明显的淡入/淡出动画,同时更改视图的激活状态。

所以我想要的是在视图状态发生变化时自定义动画。我认为应该通过 xml 来完成,但我找不到方法。

提前致谢。

编辑:

我想我发现了一些有用的东西,activated_background.xml 中有一个 \Android\android-sdk\platforms\android-API_VERSION\data\res\drawable,其中包括

<selector xmlns:android="http://schemas.android.com/apk/res/android"
        android:exitFadeDuration="@android:integer/config_mediumAnimTime">
    <item android:state_activated="true" android:drawable="@android:drawable/list_selector_background_selected" />
    <item android:drawable="@color/transparent" />
</selector>

因此,API-demos 中的示例通过声明exitFadeDuration 来实现此淡出动画。但是,这不是我想要的。我想声明 自定义动画 用于状态可绘制对象之间的转换,因为淡入/淡出动画不看起来很适合我的可绘制对象。

【问题讨论】:

    标签: android animation android-listview transition


    【解决方案1】:

    在 api 21“StateListAnimator”中添加

    http://developer.android.com/reference/android/animation/StateListAnimator.html

    我知道这是一个老问题,但这可能会帮助未来希望这样做的人。

    【讨论】:

      【解决方案2】:

      我猜TransitionDrawable 可以帮助您完成这项工作。

      您可以在这里查看答案: Animate change of view background color on Android

      【讨论】:

        【解决方案3】:

        现代方式(自 api 21 起)以示例为例:

         <?xml version="1.0" encoding="utf-8"?>
        <animated-selector xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
            <item
                android:id="@+id/checked"
                android:drawable="@drawable/check_box_on"
                app:check="true" />  <!-- this is custom state. You can use android:state_checked="true"-->
            <item
                android:id="@+id/unchecked"
                android:drawable="@drawable/check_box_off"
                app:check="false" />
            <transition
                android:drawable="@drawable/toggle_checkbox_unchecked_checked"
                android:fromId="@+id/unchecked"
                android:toId="@+id/checked" />
            <transition
                android:drawable="@drawable/toggle_checkbox_checked_unchecked"
                android:fromId="@+id/checked"
                android:toId="@+id/unchecked" />
        </animated-selector>
        

        动画选择器的文档:link

        transition drawable 例如是这样的:

        <?xml version="1.0" encoding="utf-8"?>
        <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:aapt="http://schemas.android.com/aapt"
            android:drawable="@drawable/check_box_on">
            <target android:name="check_box_on_path">
                <aapt:attr name="android:animation">
                    <objectAnimator
                        android:duration="@android:integer/config_shortAnimTime"
                        android:interpolator="@android:interpolator/decelerate_cubic"
                        android:propertyName="trimPathEnd"
                        android:valueFrom="1"
                        android:valueTo="0"
                        android:valueType="floatType" />
                </aapt:attr>
            </target>
        </animated-vector>
        

        动画矢量文档:link

        【讨论】:

          【解决方案4】:

          这是你想要的渐变吗?

          我想这与 textSwitcher 的工作方式相同,也许您想将其更改为 ViewSwitcher,淡入淡出是按程序完成的


          Animation in = AnimationUtils.loadAnimation(this,
                          android.R.anim.fade_in);
                  Animation out = AnimationUtils.loadAnimation(this,
                          android.R.anim.fade_out);
          
                  mSwitcher1.setInAnimation(in);
                  mSwitcher1.setOutAnimation(out); 
          

          【讨论】:

          • A TextSwitcher 不是一个好的解决方案。如果我使用它,我也必须处理 ListView 内的视图回收机制,因为每个列表项都会有一个 separate 切换器。所以这将是一个矫枉过正。我尝试过类似的东西,很难处理。我认为最好使用选择器,因为它们是可用的,并且可以自己处理选择/未选择机制。
          猜你喜欢
          • 2015-12-05
          • 1970-01-01
          • 2015-04-26
          • 2013-01-09
          • 2021-07-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多