详解Android Material Design自定义动画的编写

2019-12-10 18:30:06于海丽

在上例中,为一个View添加视图状态动画,定义了一个使用selector元素的xml资源,并赋给view的android:stateListAnimator属性。如要在代码中为View指定视图状态动画,可使用AnimationInflater.loadStateListAnimator()加载xml资源,并使用View.setStateListAnimator()将其指定给View。
当你的主题继承了Material主题,按钮默认拥有了z动画。为了避免这种行为在你的按钮,设置android:stateListAnimator属性值为null。
AnimatedStateListDrawable类允许您创建图片以显示关联View的状态改变动画。一些系统的Widget,在5.0上默认使用这些动画。下面的例子显示了如何定义一个AnimatedStateListDrawable作为XML资源:

<!-- res/drawable/myanimstatedrawable.xml -->
<animated-selector
  xmlns:android="http://www.easck.com/apk/res/android">

  <!-- provide a different drawable for each state-->
  <item android:id="@+id/pressed" android:drawable="@drawable/drawableP"
    android:state_pressed="true"/>
  <item android:id="@+id/focused" android:drawable="@drawable/drawableF"
    android:state_focused="true"/>
  <item android:id="@id/default"
    android:drawable="@drawable/drawableD"/>

  <!-- specify a transition -->
  <transition android:fromId="@+id/default" android:toId="@+id/pressed">
    <animation-list>
      <item android:duration="15" android:drawable="@drawable/dt1"/>
      <item android:duration="15" android:drawable="@drawable/dt2"/>
      ...
    </animation-list>
  </transition>
  ...
</animated-selector>

Animate Vector Drawables  矢量图片动画
矢量图片是可伸缩而不失真的。AnimatedVectorDrawable类让你能使一个矢量图动起来。
通常在三种xml定义动态的矢量图:

  • 使用<vector>元素的矢量图,在res/drawable/
  • 一个动态矢量图,使用<animated-vector>元素,在res/drawable/
  • 一个或多个object animator,使用<objectAnimator>元素,在res/animator/