Android筆記之屬性動畫

前言、動畫分類java

例如如下圖所看到的,Android的動畫主要分爲三種:android

 

 

如下首先說說 屬性動畫ide

所謂屬性動畫—— 就是指對象的屬性值發生了變化,如控件位置和透明度等。動畫

舉例,現在要實現一個按鍵先下移。再右移的動畫。this

 

1)編寫動畫xmlspa

因爲新建androidproject的時候,在res如下並無專門放置動畫xml的目錄。所以,咱們新建一個animator名稱的目錄。建議不要起別的名字,因爲ADTres的目錄命名有檢索功能,如起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的說明例如如下:  

  •  android:ordering說明一系列動畫動做的運行順序,有兩個選擇。sequentially together,順序運行仍是一塊兒運行;
  • objectAnimator 是設定動畫實施的對象,duration是該動畫動做運行從開始到結束所用的時間,屬性是y(後面在Activity會講到,這是自定義的屬性,用來指button的在屏幕中的y座標值。),屬性值是500,類型是整型。
 

2Activity代碼

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動畫設定類中就需要調用對象的屬性設定方法。經過動畫來改變對應屬性。

 

以上僅僅是簡單地演示了屬性動畫,實際上這個是從Android 3.0後纔出來的。3.0以前有一個很是著名的動畫開源項目,名字叫作 Nine Old Androids ,該項目開發了很是多屬性動畫, 例如如下圖所看到的,很差意思,不會作gif動態圖。你們可以直接在博文後面的連接下。裏面有APK和源碼。值得一讀。
 
 
 
相關文章
相關標籤/搜索