我有一个简单的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来完成它,但是我找不到一种方法。
提前谢谢。
编辑:
我想我找到了一些有用的东西-- activated_background.xml
in \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演示中的示例通过声明一个exitFadeDuration
来实现这个淡出动画.但是,,这不是我想要的,..。我想声明自定义动画用于状态绘图之间的转换,因为淡入/淡出动画对我的绘图不太好。
发布于 2015-11-06 00:35:52
在api 21 "StateListAnimator“中添加
http://developer.android.com/reference/android/animation/StateListAnimator.html
我知道这是个老生常谈的问题,但这可能会帮助未来的人们去做这件事。
发布于 2014-05-23 09:04:58
我想TransitionDrawable可以帮助你完成这个任务。
您可以在这里查看答案:Android上视图背景色的动态变化
发布于 2020-12-23 14:54:42
现代的方法(从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>
动画选择器文档:链接
例如,在可转换绘图的情况下:
<?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>
动画文档-向量:链接
https://stackoverflow.com/questions/9221535
复制相似问题