2014-06-27 15 views
15

Z docs:Jak korzystać z StateListAnimator?

Nowa klasa StateListAnimator pozwala zdefiniować animatorów, które działają gdy stan z widoku zmian. Poniższy przykład pokazuje, jak zdefiniować StateListAnimator jako zasób XML:

<!-- animate the translationZ property of a view when pressed --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true"> 
    <set> 
     <objectAnimator android:propertyName="translationZ" 
     android:duration="100" 
     android:valueTo="2" 
     android:valueType="floatType"/> 
     <!-- you could have other objectAnimator elements 
      here for "x" and "y", or other properties --> 
    </set> 
    </item> 
    <item android:state_enabled="true" 
    android:state_pressed="false" 
    android:state_focused="true"> 
    <set> 
     <objectAnimator android:propertyName="translationZ" 
     android:duration="100" 
     android:valueTo="2" 
     android:valueType="floatType"/> 
    </set> 
    </item> 
</selector> 

jednak nie mówi nic o tym, jak faktycznie użycie ten plik xml. Wydaje się, że klasa Resources nie ma metody uzyskiwania klasy StateListAnimator, a klasa StateListAnimator również nie dostarcza żadnych informacji.

Jak możemy tego użyć?

Odpowiedz

17

W Android L nowy atrybut XML został dodany widokiem:

android:stateListAnimator : Sets the state-based animator for the View. 

Dodatkowo dla instancji StateListAnimator obiektowi programowo nowy sposób:

loadStateListAnimator(Context context, int id) 

został dodany do AnimatorInflater.

Można je znaleźć na pakiecie dokumentacji podglądu dla deweloperów w wersji demonstracyjnej: Android L.

+8

Dla fragmentu kodu, wypróbuj StateListAnimator sla = AnimatorInflater.loadStateListAnimator (context, R.anim.my_anim); View.setStateListAnimator (sla); – Justin