android 動畫AnimationSet 和 AnimatorSet

一、前言

        在Android 中, 製作混合型動畫,肯定少不了AnimationSet和 AnimatorSet了,接下來我會簡單說一下這兩個的區別

      1. AnimationSet 可以讓許多個動畫在同一時間開始(也就是動畫的疊加)

      2.AnimatorSet 就比 AnimationSet 功能強大很多了, AnimatorSet  可以使用 playSequentially、playTogether兩個方法,來讓一些列的動畫串行和並行

二、AnimationSet的簡單使用 

 ImageView01 = (ImageView) findViewById(R.id.ImageView01);
 AnimationSet set = new AnimationSet(true);
 Animation temAnimation01 = new TranslateAnimation(0,120 , 0, -300);
 temAnimation01.setDuration(1000);
 set.addAnimation(temAnimation01);

 temAnimation01 = new ScaleAnimation(2, 1, 2, 1);
 temAnimation01.setDuration(1000);
   
 set.addAnimation(temAnimation01);
ImageView01.startAnimation(set);//這樣ImageView01就可以同時平移和縮放了

三、AnimatorSet的簡單使用

   溫馨提示,下面的代碼不可以直接運行 

   其中。 balls 是一個球的數組 。 getHeight() 可以獲取屏幕的高度

// ===============================================
// 第1個球球的動畫效果:用ObjectAnimator
// 用工廠方法構造對象:用ofFloat()因爲屬性值是float類型
// 第1個參數爲目標對象:balls.get(0)
// 第2個參數爲屬性名:y 這裏要求目標對象要有“set屬性名()”的方法。
// 後面是可變參數,表明屬性目標值,一個參數表明是終止值(對象要有get屬性方法)
// 可變參數的個數爲2時,表明第一個是起始值,第二個是終止值;更多個參數時,動畫屬性值會逐個經過這些值

ObjectAnimator anim1 = ObjectAnimator.ofFloat(balls.get(0),
"y", 0f, getHeight() - balls.get(0).getHeight())
.setDuration(500);//三個參數第一個分別是 要運行動畫的控件(通常是一個p_w_picpathView或者 view的子類) 、屬性名、從哪裏運動到哪裏(    ofFloat(Object target, String propertyName, float... values)   )

// ===============================================
// 第二個球球的動畫效果:clone動畫效果1,但是重新設置目標物體
ObjectAnimator anim2 = anim1.clone();
anim2.setTarget(balls.get(1));
anim1.addUpdateListener(this);
// 因爲前兩個動畫完全相同,所以設置刷新監聽的時候就只設置了一個(它們刷新的是同一個View)

// ===============================================
// 第三個球球的動畫效果:先加速下落,再減速上升
ShapeHolder ball2 = balls.get(2);
// 動畫效果:落下效果
ObjectAnimator animDown = ObjectAnimator.ofFloat(ball2, "y",
0f, getHeight() - ball2.getHeight()).setDuration(500);
// 落下效果改變了Interpolator,設置爲加速
animDown.setInterpolator(new AccelerateInterpolator());
// 動畫效果:上升效果
ObjectAnimator animUp = ObjectAnimator.ofFloat(ball2, "y",
getHeight() - ball2.getHeight(), 0f).setDuration(500);
// 上升效果設置爲減速上升
animUp.setInterpolator(new DecelerateInterpolator());

// 用一個AnimatorSet對象將下落效果和上升效果順序播放
AnimatorSet s1 = new AnimatorSet();
s1.playSequentially(animDown, animUp);// 順序播放效果,參數個數可變

// 下落動畫刷新View
animDown.addUpdateListener(this);
// 上升動畫刷新View
animUp.addUpdateListener(this);
// ===============================================
// 第四個球球的動畫效果
// 另一個AnimatorSet克隆了上一個set,更換了對象
AnimatorSet s2 = (AnimatorSet) s1.clone();
s2.setTarget(balls.get(3));

// ===============================================
// 第五個球球的動畫效果:使用ValueAnimator
final ShapeHolder ball5 = balls.get(4);
ValueAnimator valueAnimator5 = ValueAnimator.ofFloat(0f,
getHeight() - ball5.getHeight());
valueAnimator5.setDuration(500);
valueAnimator5.addUpdateListener(new AnimatorUpdateListener() {

// ValueAnimator需要自己在監聽處理中設置對象參數
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// 用animation.getAnimatedValue()得到當前的屬性值,設置進動畫對象中
ball5.setY((Float) animation.getAnimatedValue());

// 記得要刷新View否則不會調用重新繪製
invalidate();
}
});

// =============================================================
// 用一個總的AnimatorSet對象管理以上所有動畫
animation = new AnimatorSet();
animation.playTogether(anim1, anim2, s1);// 並行
animation.playSequentially(s1, s2, valueAnimator5);// 串行
animation.start();// 播放動畫時,我們只要使用 animation.start(),你就可以看到動畫效果了

 

 

原文來自:http://blog.csdn.net/u012972188 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章