首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >动画选择器/状态转换

动画选择器/状态转换
EN

Stack Overflow用户
提问于 2012-02-10 01:30:02
回答 4查看 22.8K关注 0票数 40

我有一个简单的ListView选择器

代码语言:javascript
运行
复制
<?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来完成它,但是我找不到一种方法。

提前谢谢。

编辑:

我想我找到了一些有用的东西-- activated_background.xml in \Android\android-sdk\platforms\android-API_VERSION\data\res\drawable,它包括

代码语言:javascript
运行
复制
<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来实现这个淡出动画.但是,,这不是我想要的,..。我想声明自定义动画用于状态绘图之间的转换,因为淡入/淡出动画对我的绘图不太好。

EN

回答 4

Stack Overflow用户

发布于 2015-11-06 00:35:52

在api 21 "StateListAnimator“中添加

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

我知道这是个老生常谈的问题,但这可能会帮助未来的人们去做这件事。

票数 7
EN

Stack Overflow用户

发布于 2014-05-23 09:04:58

我想TransitionDrawable可以帮助你完成这个任务。

您可以在这里查看答案:Android上视图背景色的动态变化

票数 3
EN

Stack Overflow用户

发布于 2020-12-23 14:54:42

现代的方法(从api 21开始)是这样做的,例如:

代码语言:javascript
运行
复制
 <?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>

动画选择器文档:链接

例如,在可转换绘图的情况下:

代码语言:javascript
运行
复制
<?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>

动画文档-向量:链接

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9221535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档