Android中的自定义按钮动画

Android中的自定义按钮动画,android,android-animation,android-button,Android,Android Animation,Android Button,在我的应用程序中,当用户按下菜单中的某个按钮时,我希望该按钮缩放到更大的尺寸,然后释放该按钮时,它需要缩放回其原始尺寸。在xml中,我创建了以下动画师: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="together"> <objectAnimator

在我的应用程序中,当用户按下菜单中的某个按钮时,我希望该按钮缩放到更大的尺寸,然后释放该按钮时,它需要缩放回其原始尺寸。在xml中,我创建了以下动画师:

<?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开发者页面上,它说您可以为自定义按钮创建一个状态列表,如下所示:

<?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方法,我如何检测按钮首次按下的时间?

添加onTouchListener:

 button.setOnTouchListener(new View.OnTouchListener()
 {
                @Override
                public boolean onTouch(View v, MotionEvent event) 
                {

                  if (event.getAction() == MotionEvent.ACTION_DOWN)
                   {
                      here the button is pressed down
                   }



                    return false;
                }
            });

您可以这样做:

  • 创建一个文件
    按钮\u state\u列表\u anim.xml
    并将其放入
    anim
    文件夹(如果您没有,请在
    res
    文件夹中创建)
  • 将以下行添加到文件中:

    <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\u button\u state\u list\u anim\u material”


  • 这正是我想要的,除了在XML文件中,它说“属性stateListAnimator仅在API 21或更高版本中使用。”我的应用程序的最低要求是API 15,那么在API 21之前的版本中我如何实现这一点呢?@Thomas Well,据我所知,没有用于状态更改的特殊侦听器,因此,您应该提供一些机制来检测此更改,然后手动启动动画。您好,它适用于我,但不是
    anim
    文件夹,错误表示
    selector
    anim
    中是不允许的。我在
    drawable
    中查看您的文件。然后将
    android:background
    stateListAnimator
    设置到此文件。