Android中的自定义按钮动画 [英] Custom Button Animation in Android
问题描述
在我的应用程序中,当用户按下菜单中的按钮时,我希望按钮可以缩放到更大的尺寸,然后在释放按钮时,它需要缩放到其原始大小.在xml中,我创建了以下动画制作器:
In my application, when a user presses a button in the menu, I would like the button to scale to a larger size, and then when the button is released, it needs to scale back to its original size. In xml, I have created the following animator:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<objectAnimator
android:propertyName="scaleX"
android:duration="100"
android:valueFrom="1.0"
android:valueTo="1.3"
android:valueType="floatType"/>
<objectAnimator
android:propertyName="scaleY"
android:duration="100"
android:valueFrom="1.0"
android:valueTo="1.3"
android:valueType="floatType"/>
</set>
在Android开发人员页面上,您可以为自定义按钮创建状态列表,如下所示:
On the Android developer page, it says you can create a state list for a custom button, like so:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/button_focused"
android:state_focused="true" />
<item android:drawable="@drawable/button_default" />
</selector>
我有自定义背景,所以我的问题是:有没有办法将我的动画应用到这样的状态列表中的可绘制对象上?还是我必须将动画应用于Java代码中的每个按钮?我的应用程序有很多按钮,因此在Java中为每个动画分别设置动画会很长.如果必须在Java代码中应用动画,则仅在释放按钮时才调用按钮的onclick方法,如何检测何时首次按下按钮?
I have the custom background, so my question is: is there any way to apply my animation to the drawable in a state list like this? Or would I have to apply the animation to each button in my Java code? My application has a lot of buttons, so setting the animation for each one individually in Java would get very long. If I do have to apply the animation in the Java code, the button's onclick method is only called when the button is released, how would I detect when the button is first pressed down?
推荐答案
您可以执行以下操作:
- 创建文件
button_state_list_anim.xml
并将其放入anim
文件夹(如果没有,请在res
文件夹中创建)
-
将以下行添加到文件中:
- Create a file
button_state_list_anim.xml
and put it intoanim
folder (if you haven't got it, create it in yourres
folder) Add these lines to the file:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<set>
<objectAnimator
android:duration="100"
android:propertyName="scaleX"
android:valueTo="1.3"
android:valueType="floatType" />
<objectAnimator
android:duration="100"
android:propertyName="scaleY"
android:valueTo="1.3"
android:valueType="floatType" />
</set>
</item>
<!-- base state -->
<item android:state_enabled="true">
<set>
<objectAnimator
android:duration="100"
android:propertyName="scaleX"
android:startDelay="100"
android:valueTo="1"
android:valueType="floatType" />
<objectAnimator
android:duration="100"
android:propertyName="scaleY"
android:startDelay="100"
android:valueTo="1"
android:valueType="floatType" />
</set>
</item>
<item>
<set>
<objectAnimator
android:duration="0"
android:propertyName="scaleX"
android:valueTo="1"
android:valueType="floatType" />
<objectAnimator
android:duration="0"
android:propertyName="scaleY"
android:valueTo="1"
android:valueType="floatType" />
</set>
</item>
在使用按钮的布局文件中,添加android:stateListAnimator= "@anim/custom_button_state_list_anim_material"
,例如
In layout files where you use the button, add android:stateListAnimator= "@anim/custom_button_state_list_anim_material"
, for example
<Button
android:id="@+id/animated_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stateListAnimator="@anim/custom_button_state_list_anim_material"
android:text="Animated button"
/>
<Button
android:id="@+id/animated_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stateListAnimator="@anim/custom_button_state_list_anim_material"
android:text="Animated button"
/>
这篇关于Android中的自定义按钮动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!