动画选择器/状态转换

40

我有一个简单的选择器用于我的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演示中运行示例,您将看到视图的激活状态发生变化时有一个明显的淡入淡出动画。
因此,我的要求是在视图状态变化时使用自定义动画。我认为可以通过XML实现,但我找不到方法。
提前感谢您的帮助。

编辑:

我猜我找到了一些有用的东西,即\Android\android-sdk\platforms\android-API_VERSION\data\res\drawable中的activated_background.xml包含

<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演示中的示例通过声明exitFadeDuration来实现淡出动画。 然而,这不是我想要的准确效果。 我希望能够为状态可绘制物之间的过渡声明自定义动画,因为对于我的可绘制物而言,淡入淡出动画看起来不好。
4个回答

7

3

2

现代的方式(自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>

animated-selector的文档:链接

其中,过渡绘图可举例如下:

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

animated-vector的文档:链接


0

你想要的是淡入淡出效果吗?

我猜这应该和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); 


一个 TextSwitcher 不是一个好的解决方案。如果我使用它,我也必须在 ListView 中处理视图回收机制,因为每个列表项都有一个单独的 switcher。所以这将会是一种浪费。我尝试过类似于此的东西,而且很难处理。我认为最好使用选择器,因为它们可用并且可以自己处理选中/未选中的机制。 - C.d.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接