屬性動畫一

Animation 與 Animator

動畫我們一般分爲傳統動畫Animation和屬性動畫Animator

Animation動畫的原理是不斷調用onDraw方法不斷的重新繪製控件,可以實現 旋轉,位移,縮放,透明度。

屬性動畫顧名思義,是不斷調用控件屬性的get set方法去真實地改變控件的屬性

相對於屬性動畫,Animation動畫只能改變控件的顯示,不能真正地改變控件的屬性,對於需要交互的控件是不適用的

所以 Android3.0引入了屬性動畫


Animator簡單使用

1.簡單的平移示例

translationX表示控件在X方向上的偏移量,初始值一般爲0,這樣動畫起始不會突兀

float可以是多個參數,用,隔開,動畫分多段進行

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


常用的Animator屬性還有

translationX    translationY  rotation rotationX  rotationY  scaleX  scaleY  alpha
rotationX是繞X軸旋轉,(還有那麼點立體的感覺)

2.多個動畫同時start,其實是一個異步的過程

ObjectAnimator.ofFloat(imageView,"rotation",0f,360f).setDuration(1000).start();
ObjectAnimator.ofFloat(imageView,"translationX",0f,200f).setDuration(1000).start();
ObjectAnimator.ofFloat(imageView,"translationY",0f,200f).setDuration(1000).start();
這三個動畫效果是同時進行的

3.也可以使用PropertyValuesHolder先將要同時進行的動畫屬性名與屬性值保存起來,使用ofPropertyValuesHolder方法展示動畫,

效果與以上相同,並且,該方法在性能上是有所優化的

PropertyValuesHolder p1=PropertyValuesHolder.ofFloat("rotation",0f,360f);
PropertyValuesHolder p2=PropertyValuesHolder.ofFloat("translationX",0f,200f);
PropertyValuesHolder p3=PropertyValuesHolder.ofFloat("translationY",0f,200f);
ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2,p3).setDuration(1000).start();


4.還可以使用AnimatorSet,與上述兩種方法的效果是一致的
ObjectAnimator animator1=ObjectAnimator.ofFloat(imageView,"rotation",0f,360f);
ObjectAnimator animator2=ObjectAnimator.ofFloat(imageView,"translationX",0f,200f);
ObjectAnimator animator3=ObjectAnimator.ofFloat(imageView,"translationY",0f,200f);
AnimatorSet set=new AnimatorSet();
set.playTogether(animator1,animator2,animator3);
set.setDuration(1000);
set.start();
但是AnimatorSet有豐富的功能,例如,替換playTogether方法爲

set.playSequentially(animator1,animator2,animator3);
這三個動畫就會按先後順序一一執行,每個動畫執行時間都是1秒


也可以使用play, with, after 來進行單獨的執行順序的控制

with表示同時,即先同時執行2,3,然後執行1,每步都是1s

set.play(animator2).with(animator3);
set.play(animator1).after(animator2);

Animator動畫監聽事件

調用addListener方法設定ObjectAnimator的監聽事件,
ObjectAnimator animator=ObjectAnimator.ofFloat(imageView,"rotation",0f,360f);
animator.setDuration(1000);
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) {

                        }
                    });
animator.start();

大部分時候也許只需要監聽onAnimationEnd,可以通過添加AnimatorListenAdapter,只監聽需要監聽的事件

animator.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        //方法體
    }
});


可以使用插值器,給動畫增加更多 多變的效果

安卓內置了很多插值器,基本能滿足使用需求

AccelerateDecelerateInterpolator, AccelerateInterpolator, AnticipateInterpolator, AnticipateOvershootInterpolator, BaseInterpolator, BounceInterpolator, CycleInterpolator, DecelerateInterpolator, FastOutLinearInInterpolator, FastOutSlowInInterpolator,  LinearInterpolator, LinearOutSlowInInterpolator, OvershootInterpolator, PathInterpolator


animator.setInterpolator(new BounceInterpolator());

以下是常用插值器的效果一覽圖,橫軸是時間,縱軸是變化量


以上內容整理自慕課網  



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