1. 程式人生 > >Android Animation、ObjectAnimator與ValueAnimator實現檢視動畫和屬性動畫

Android Animation、ObjectAnimator與ValueAnimator實現檢視動畫和屬性動畫

一、檢視動畫

檢視動畫比較簡單,它提供了AlphaAnimation、RotateAnimation、TranslateAnimation、ScaleAnimation四種動畫方式。

檢視動畫一個非常大的缺陷就是不具有互動性,當一個View發生檢視動畫後,其相應事件的位置依然在顯示動畫前的位置。但是優點也非常明顯,即效率比較高且使用方便。

1、透明度動畫

AlphaAnimation aa = new AlphaAnimation(0f, 1f);
aa.setDuration(1000);
view.startAnimation(aa);

2、旋轉動畫

RotateAnimation ra = new
RotateAnimation(0f, 359f//旋轉的角度,負值代表逆時針的 , Animation.RELATIVE_TO_SELF, 0.5f//自己的X中心 , Animation.RELATIVE_TO_SELF, 0.5f);//自己的Y中心 ra.setDuration(1000); view.startAnimation(ra);

3、縮放動畫

ScaleAnimation sa = new ScaleAnimation(0f, 2f, 0f, 2f//0倍到2倍
                , Animation.RELATIVE_TO_SELF, 0.5f
//中心縮放 , Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(1000); view.startAnimation(sa);

4、位移動畫

TranslateAnimation ta = new TranslateAnimation(0f, 200f, 0f, 400f);
ta.setDuration(1000);
view.startAnimation(ta);



效果展示
檢視動畫

5、動畫集合

AnimationSet as = new AnimationSet(true);
as.setDuration(1000
); //從完全透明到完全不透明 AlphaAnimation aa = new AlphaAnimation(0f, 1f); aa.setDuration(1000); as.addAnimation(aa); //從0到原大小的縮放(放大)效果 ScaleAnimation sa = new ScaleAnimation(0f, 1f, 0f, 1f// , Animation.RELATIVE_TO_SELF, 0.5f// , Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(1000); as.addAnimation(sa); view.startAnimation(as);

6、載入佈局中動畫

Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.scale_animation);
view.startAnimation(anim);

7、動畫的監聽事件

監聽動畫的開始、重複和結束

animation.setAnimationListener(new AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
    }
});

8、實現不間斷旋轉

RotateAnimation ra = new RotateAnimation(0f, 359f//
                , Animation.RELATIVE_TO_SELF, 0.5f//
                , Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(400);
ra.setInterpolator(new LinearInterpolator());//勻速變化
ra.setRepeatCount(-1);//無限重複
view.startAnimation(ra);

二、屬性動畫

1、ObjectAnimator

由於Android3.0之前的動畫框架Animation存在一些侷限——動畫改變的只是顯示的內容,並不能響應事件。因此,Android3.0之後,Google提出了屬性動畫這樣一個動畫框架,幫助開發者實現更加豐富的動畫效果。

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

示例:

ObjectAnimator animator= ObjectAnimator.ofFloat(button, "translationX", 0, 200);
animator.setDuration(1000);
animator.start();

效果:
ObjectAnimator

與檢視動畫一樣,也可以給屬性動畫設定時長插值器等屬性,這些引數與在檢視動畫中的設定方法類似。
但是,在設定ObjectAnimator的時候,有一點非常重要,那就是要操縱的屬性必須有get、set方法,不然ObjectAnimator就無法起效。下面就是一些常用的可以使用的動畫的屬性值:

  • translationX,translationY:這兩個屬性作為一種增量來控制View物件佈局的左上角座標偏移的位置。
  • rotation, rotationX,rotationY:這三個屬性控制View物件圍繞他的支點進行2D,3D旋轉。
  • scaleX,scaleY:這兩個屬性控制圍繞他的支點進行2D縮放。
  • pivotX,pivotY:這兩個屬性控制View物件的支點位置。預設為該物件的中心。
  • x,y:控制View物件在它的容器的最終的位置,他是最初的左上角的座標和translateX和translateY的總和。
  • alpha:控制View的透明度,1可見,0不可見。

如果一個屬性沒有getset方法,那麼可通過兩種方法來解決。一個是通過自定義一個屬性類或者包裝類,間接給這個屬性設定get和set方法;另一個是通過ValueAnimator來實現。

包裝類實現如下:
包裝類:

private static class WrapperView{
    private View mTarget;

    public WrapperView(View target) {
        this.mTarget= target;
    }

    public int getWidth() {
        return mTarget.getLayoutParams().width;
    }

    public void setWidth(int width){
        mTarget.getLayoutParams().width = width;
        mTarget.requestLayout();
    }
}

使用方式:

ObjectAnimator animator= ObjectAnimator.ofInt(new WrapperView(button), "width",0, 500);
animator.setDuration(1000);
animator.start();

效果展示:
ObjectAnimator操作無get/set方法的屬性

2、動畫的監聽事件

animator.addListener(new AnimatorListener() {

    @Override
    public void onAnimationStart(Animator animation) {
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
    }

    @Override
    public void onAnimationEnd(Animator animation) {
    }

    @Override
    public void onAnimationCancel(Animator animation) {
    }
});

或者使用AnimatorListenerAdapter只對必要的事件進行監聽:

animator.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
    }
});

3、PropertyValuesHolder

類似檢視中的AnimationSet,在屬性動畫中,如果要同時操作同一個物件的多個屬性,要同時作用多種動畫,可以使用PropertyValuesHolder來實現。如下:

PropertyValuesHolder ovh1 = PropertyValuesHolder.ofFloat("translationX",0,300);
PropertyValuesHolder ovh2 = PropertyValuesHolder.ofFloat("translationY",0,150);
PropertyValuesHolder ovh3 = PropertyValuesHolder.ofFloat("scaleX", 1f, 2f, 1f);
PropertyValuesHolder ovh4 = PropertyValuesHolder.ofFloat("scaleY", 1f, 2f, 1f);
ObjectAnimator.ofPropertyValuesHolder(button, ovh1, ovh2, ovh3, ovh4).setDuration(1000).start();

效果展示:
PropertyValuesHolder

4、AnimatorSet

使用AnimatorSet同樣可以同時操作一個檢視的多個屬性,而且,AnimatorSet能夠實現更為精確的順序控制。
使用AnimatorSet實現上面效果的寫法為:

ObjectAnimator animator1 = ObjectAnimator.ofFloat(button, "translationX",0f,300f);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(button, "translationY",0f,150f);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(button, "scaleX",1f, 2f, 1f);
ObjectAnimator animator4 = ObjectAnimator.ofFloat(button, "scaleY",1f, 2f, 1f);
AnimatorSet as = new AnimatorSet();
as.setDuration(1000);
as.playTogether(animator1, animator2, animator3, animator4);
as.start();




Thank you for your support.
THE END