Android應用層(View體系)二

1.Android屬性動畫

Animator框架中使用最多的就是AnimatorSet和ObjectAnimator配合,使用ObjectAnimator進行更精細化的控制,控制一個對象和一個屬性值,而使用多個ObjectAnimator組合到AnimatorSet形成一個動畫,

屬性動畫通過調用屬性get,set方法來真實地控制了一個View的屬性值,因此強大的屬性動畫框架,基本可以實現所有的動畫效果

## ObjectAnimator
- 創建一個ObjectAnimator只需通過他的靜態工廠類直接返還一個ObjectAnimator對象。
- 參數包括一個對象和對象的屬性名字,但這個屬性必須有get和set函數,內部會通過java反射機制來調用set函數修改對象的屬性值。

#### Step 1 : 實現平移的屬性動畫
“`
ObjectAnimator mObjectAnimator=ObjectAnimator.ofFloat(view,”translationX”,200);
mObjectAnimator.setDuration(300);
mObjectAnimator.start();

- 第一個對象是要**操作的View**
- 第二個參數則是要**操縱的屬性**
- 最後一個參數是一個**可變的數組參數**,需要傳進去一個該屬性變化的一個取值的過程

###### 常見的屬性動畫的屬性值
- translationX和translationY:這兩個屬性作爲增量控制View對象從他的佈局容器的左上角開始位置。
- rotation、rotationX、rotationY:這三個屬性控制View對象圍繞它的支點進行2D和3D旋轉。
- PrivotXPrivotY:控制View對象的支點位置,圍繞這個支點進行旋轉和縮放變換處理。默認該支點位置就是View對象的中心點。
- alpha:透明度,默認是1(不透明),0代表完全透明。
- x和y:描述View對象在它容器中的最終位置,它是最初的做上角座標和translationX,translationY值的累計的和。

#### Step add : 也可以通過自定義一個屬性類或包裝類間接的增加get和set方法
1. 自定義的屬性類

private static class MyView{
private View mTarget;
private MyView(View mTarget){
this.mTarget=mTarget;
}
public int getWidth(){
return mTarget.getLayoutParams().width;
}
public void setWidth(int width){
mTarget.getLayoutParams().width=width;
mTarget.requestLayout();
}
}

2. 使用

MyView mMyView=new MyView(mButton);
ObjectAnimator.ofInt(mMyView,”width”,500).setDuration(500).start();


## ValueAnimator
> ValueAnimator不提供任何動畫效果,它更像一個數值發生器,用來產生一定規律數字,從而讓調用者來控制動畫的實現過程。

#### Step 1 : 在ValueAnimator的AnimatorUpdateListener中監聽數值的變化,從而完成動畫的變換:

ValueAnimator mValueAnimator=ValueAnimator.ofFloat(0,100);
mValueAnimator.setTarget(view);
mValueAnimator.setDuration(1000).start();
mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float mFloat=(Float)animation.getAnimatedValue();
}
});
}


### 動畫監聽
完整的動畫具有start,Repeat,End,Cancel四個過程:

ObjectAnimator animator=ObjectAnimator.ofFloat(view,”alpha”,1.5f);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}


大部分時候我們只關心onAnimationEnd事件,android也提供了AnimatorListenterAdaper來讓我門選擇必要的事件進行監聽:

ObjectAnimator animator=ObjectAnimator.ofFloat(view,”alpha”,1.5f);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});


## 組合動畫-AnimatorSet
向這個方法中傳入一個Animator對象(ValueAnimator或ObjectAnimator)將會返回一個AnimatorSet.Builder的實例,AnimatorSet.Builder中包括以下四個方法:
- after(Animator anim) 將現有動畫插入到傳入的動畫之後執行
- after(long delay) 將現有動畫延遲指定毫秒後執行
- before(Animator anim) 將現有動畫插入到傳入的動畫之前執行
- with(Animator anim) 將現有動畫和傳入的動畫同時執行

ObjectAnimator animator1 = ObjectAnimator.ofFloat(mCustomView, “translationX”, 0.0f, 200.0f, 0f);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(mCustomView, “scaleX”, 1.0f, 2.0f);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(mCustomView, “rotationX”, 0.0f, 90.0f, 0.0F);
AnimatorSet set=new AnimatorSet();
set.setDuration(1000);
set.play(animator1).with(animator2).after(animator3);
set.start();


## xml中使用屬性動畫
和視圖動畫一樣,屬性動畫也可以直接寫在xml中:
在res文件中新建animator文件,在裏面新建一個scale.xml,裏面的內容如下:


程序中引用xml定義的屬性動畫:

Animator animator=AnimatorInflater.loadAnimator(this,R.animator.scale);
animator.setTarget(view);
animator.start();
“`

發佈了34 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章