StateListAnimator 介紹

StateListAnimator 是在 Android 5.1 版本引入的。在這之前,處理 View 的點擊狀態一般都是使用 StateListDrawable 來完成的。

啥? 您沒用過 StateListDrawable ?

下面的文件 (res/drawable/foreground_selector.xml) 內容,您一定很熟悉吧!

<?xmlversion="1.0" encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
  <itemandroid:color="@color/transparentAccent"
    android:state_pressed="true">
    <shape>
      <solidandroid:color="@color/transparentAccent"/>
    </shape>
  </item>
 
  <item>
    <shape>
      <solidandroid:color="@android:color/transparent"/>
    </shape>
  </item>
</selector>

這就是一個 StateListDrawable 對象,當設置為 View 的背景的時候,不同的狀態可以使用不同的背景圖片表示。

效果如下圖:

您可能會問, 使用 StateListDrawable 很好啊, 不同的點擊狀態使用不同的背景來表示,為啥還要搞個新的 StateListAnimator 呢?

原因是在 Android 5.0系統開始引入了新的 Material Design(紙墨設計) 規范,而在 紙墨設計規范中動畫是非常重要的,通過各種動畫來指導用戶操作以及凸顯重要的內容。 StateListDrawable 只是簡單的狀態切換,并沒有動畫所以不太符合 紙墨設計 規范的要求,因此從新設計了一個 StateListAnimator。

既然是一個 Animator ,就說明該類可以對 View 的屬性做動畫。

比如:(res/animator/selector_animator.xml)

<?xmlversion="1.0" encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
  <itemandroid:state_pressed="true">
    <objectAnimator
      android:duration="@android:integer/config_shortAnimTime"
      android:propertyName="translationZ"
      android:valueTo="4dp"
      android:valueType="floatType"/>
  </item>
 
  <item>
    <objectAnimator
      android:duration="@android:integer/config_shortAnimTime"
      android:propertyName="translationZ"
      android:valueTo="0dp"
      android:valueType="floatType"/>
  </item>
</selector>

根元素依然為 selector, 只不過該文件是在 animator 目錄中的。每個 item 為一個 objectAnimator 對象 用來對 View 的屬性做動畫。 可以把這個文件設置到 View 的 stateListAnimator 屬性上去:

android:stateListAnimator=”@animator/selector_animator”

效果如下:

另外值得說明的是,在 item 中不僅可以使用 objectAnimator 還可以使用多個 objectAnimator 來實現復雜的動畫,多個 objectAnimator 放到一個 set 中:

<?xmlversion="1.0" encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
  <itemandroid:state_pressed="true">
    <set>
      <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="scaleX"
        android:valueTo="1.025"
        android:valueType="floatType"/>
      <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="scaleY"
        android:valueTo="1.025"
        android:valueType="floatType"/>
      <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="translationZ"
        android:valueTo="4dp"
        android:valueType="floatType"/>
    </set>
  </item>
 
  <item>
    <set>
      <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="scaleX"
        android:valueTo="1.0"
        android:valueType="floatType"/>
      <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="scaleY"
        android:valueTo="1.0"
        android:valueType="floatType"/>
      <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="translationZ"
        android:valueTo="0dp"
        android:valueType="floatType"/>
    </set>
  </item>
 
</selector>

效果:

本文示例中的代碼位于 github 。 原文位于 stylingandroid

 

來自:http://blog.chengyunfeng.com/?p=1014

 

 本文由用戶 tianzhidao 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!