2014-06-27 15 views
15

Dal docs:Come utilizzare StateListAnimator?

La nuova classe StateListAnimator consente di definire animatori che corrono quando lo stato di una visione cambia. L'esempio seguente mostra come definire uno StateListAnimator come una risorsa 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> 

Tuttavia, non dice nulla su come effettivamente uso questo file XML. Non sembra esserci alcun metodo sulla classe Resources per ottenere un StateListAnimator e la classe StateListAnimator non fornisce alcuna informazione.

Come possiamo usare questo?

risposta

17

In L Android un nuovo attributo XML è stato aggiunto per Vista:

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

Inoltre per istanziare StateListAnimator oggetto di programmazione un nuovo metodo:

loadStateListAnimator(Context context, int id) 

è stata aggiunta a AnimatorInflater.

Questi possono essere trovati su Android L pacchetto di documentazione di anteprima dello sviluppatore.

+8

Per uno snippet di codice, provare StateListAnimator sla = AnimatorInflater.loadStateListAnimator (context, R.anim.my_anim); View.setStateListAnimator (SLA); – Justin