前言、動畫分類java
例如如下圖所看到的,Android的動畫主要分爲三種:android
如下首先說說 屬性動畫ide
所謂屬性動畫—— 就是指對象的屬性值發生了變化,如控件位置和透明度等。動畫
舉例,現在要實現一個按鍵先下移。再右移的動畫。this
(1)編寫動畫xmlspa
因爲新建androidproject的時候,在res如下並無專門放置動畫xml的目錄。所以,咱們新建一個animator名稱的目錄。建議不要起別的名字,因爲ADT對res的目錄命名有檢索功能,如起animator這個名字的時候。ADT就能依據名稱識別出這個是動畫xml目錄,在你新建xml的時候。會給對應的根元素選擇。3d
例如如下圖所看到的:xml
動畫XML的代碼例如如下——對象
<?xml version="1.0" encoding="utf-8"?
> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially" > <objectAnimator android:duration="2000" android:propertyName="y" android:valueTo="500" android:valueType="intType"></objectAnimator> <objectAnimator android:duration="2000" android:propertyName="a" android:valueTo="300" android:valueType="intType"></objectAnimator> </set>blog
對動畫xml的說明例如如下:
(2)Activity代碼
public class PropertyActivity extends Activity { public final static String TAG = "PropertyActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_property); final Button moveButton = (Button)findViewById(R.id.move_btn); final Move move = new Move(moveButton); moveButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 裝載屬性動畫資源 AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(PropertyActivity.this, R.animator.property_anim); // 設置要控制的對象 set.setTarget(move); // 開始動畫 set.start(); Log.d(TAG, "getWidth:"+moveButton.getWidth()); Log.d(TAG, "Top:"+moveButton.getTop()); Log.d(TAG, "getMeasuredWidth:"+moveButton.getMeasuredWidth()); Log.d(TAG, "getBottom:"+moveButton.getBottom()); } }); } private class Move{ private int a; private int y; private View view; public Move(View view) { this.view = view; } public int getY() { return y; } public void setY(int y) { this.y = y; view.layout(view.getLeft(), y, view.getRight(), y + view.getMeasuredHeight()); } public int getA() { return a; } public void setA(int a) { this.a = a; Log.d(TAG, "End_getWidth:"+view.getWidth()); Log.d(TAG, "End_Top:"+view.getTop()); Log.d(TAG, "End_getMeasuredWidth:"+view.getMeasuredWidth()); Log.d(TAG, "End_getBottom:"+view.getBottom()); view.layout(a, view.getTop(), a + view.getMeasuredWidth(), view.getBottom()); } } }
咱們在程序中Logcat打印出對button位置的詳細值。
Log.d(TAG,"getWidth:"+moveButton.getWidth());
Log.d(TAG,"Top:"+moveButton.getTop());
Log.d(TAG,"getMeasuredWidth:"+moveButton.getMeasuredWidth());
Log.d(TAG,"getBottom:"+moveButton.getBottom());
結果例如如下:
上下高度確實是從0變化到500,實現了移動。
上面代碼中的屬性x,y都是本身隨意取的變量值,在set方法中設置了詳細view的高度和寬度,所以,變量名稱是什麼不重要,僅僅要xml與這裏java代碼相符合便可。
get()方法不必的,而set方法是必須的,因爲
AnimatorSet動畫設定類中就需要調用對象的屬性設定方法。經過動畫來改變對應屬性。