參考官方文檔:https://developer.android.com/guide/topics/graphics/prop-animation#api-overview
概述
筆者近期接觸到android動畫,將諸多概念都稍微整理了一下。
一方面做一個知識的記錄,另一方面也給剛接觸android動畫的初學者一個參考。
主要內容
- ValueAnimator
- ObjectAnimator(包括AnimatorSet,PropertyValuesHolder)
- AnimatorSet 和 PropertyValuesHolder
- 插值器(Interpolator)
valueAnimator
valueAnimator可以控制某個值的變化,通過在值變化的同時刷新View來實現動畫。
下面的例子是實現TextView中值的刷新:
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 1000);
valueAnimator.setDuration(5000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
tv1.setText(animation.getAnimatedValue() + "");
}
});
valueAnimator.start();
ObjectAnimator
ObjectAnimator可以對某個View執行屬性動畫,常見的屬性動畫有旋轉,平移,縮放等。
ObjectAnimator一般是配合着AnimatorSet或者PropertyValuesHolder使用,demo如下,以下兩塊代碼實現的是同一種效果:“btn先放大後縮小”。
屬性動畫有以下幾種:(來自官方文檔)
- translationX,translationY:從當前位置開始平移
- rotation, rotationX, rotationY:旋轉
- scaleX , scaleY:縮放
- pivotX , pivotY:旋轉和縮放的參照點,默認是View的中心
- x , y:平移到當前容器的某個絕對位置
- alpha:透明度
/*
ObjectAnimator + PropertyValuesHolder
*/
PropertyValuesHolder xHolder = PropertyValuesHolder.ofFloat("scaleX", 1, 1.5f, 0.8f, 1);
PropertyValuesHolder yHolder = PropertyValuesHolder.ofFloat("scaleY", 1, 1.5f, 0.8f, 1);
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(btn0, xHolder, yHolder);
objectAnimator.setDuration(3000);
objectAnimator.start();
/*
* ObjectAnimator + AnimatorSet
*/
ObjectAnimator anim1 = ObjectAnimator.ofFloat(btn1, "scaleX", 1, 1.5f, 0.8f, 1);
ObjectAnimator anim2 = ObjectAnimator.ofFloat(btn1, "scaleY", 1, 1.5f, 0.8f, 1);
final AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(anim1);
animatorSet.play(anim2).with(anim1);
animatorSet.setDuration(3000);
animatorSet.start();
AnimatorSet 和 PropertyValuesHolder
PropertyValuesHolder:
適合在實現“單個View連續動畫”的情況下使用,使用AnimatorSet在一些情況下會比PropertyValuesHolder複雜一些。
比如要實現View的循環播放,使用PropertyValuesHolder實現的代碼如下:
objectAnimator.setRepeatCount(-1);
使用AnimatorSet實現的代碼如下:
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
if (animatorSet != null) {
animatorSet.start();
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
AnimatorSet
適合在動畫比較複雜的情況下使用,比如有多個View的動畫需要同時進行或者交替進行,這種情況使用PropertyValuesHolder是很難實現的。
再比如,對於同一個View的動畫不連續的情況,PropertyValuesHolder也比較難實現,或者說實現更加複雜。
demo中,View執行完平移之後再執行縮放邏輯。
ObjectAnimator anim11 = ObjectAnimator.ofFloat(tv2, "translationX", 200);
ObjectAnimator anim22 = ObjectAnimator.ofFloat(tv2, "scaleX", 1, 1.5f, 1f);
ObjectAnimator anim33 = ObjectAnimator.ofFloat(tv2, "scaleY", 1, 1.5f, 1f);
final AnimatorSet animatorSet2 = new AnimatorSet();
animatorSet2.play(anim11);
animatorSet2.play(anim22).after(anim11);
animatorSet2.play(anim33).with(anim22);
animatorSet2.setDuration(3000);
animatorSet2.start();
插值器(Interpolator)
插值器可以控制動畫變化的速率,設置非常簡單,代碼如下:
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 1000);
valueAnimator.setDuration(5000);
valueAnimator.setInterpolator(new DecelerateInterpolator());//設置插值器
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
tv1.setText(animation.getAnimatedValue() + "");
}
});
valueAnimator.start();
插值器類型:(來自官方文檔)
- AccelerateDecelerateInterpolator:開始和結束的時候慢,中間快
- AccelerateInterpolator:開始的時候慢,然後加速
- AnticipateInterpolator:開始先後退,然後向前
- AnticipateOvershootInterpolator: 開始先後退,然向前到超標,最後回到最終值
- BounceInterpolator :最後會反彈
- CycleInterpolator:動畫會重複一定的週期數
- DecelerateInterpolator:開始快,然後減速
- LinearInterpolator:變化勻速
- OvershootInterpolator:到達最終值後超標,再回到最終值
- TimeInterpolator:用來自定義插值器
demo代碼:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);
// test View
final TextView tv1 = new TextView(this);
tv1.setText(0 + "");
tv1.setTextSize(30);
linearLayout.addView(tv1);
final Button btn0 = new Button(this);
btn0.setText("btn0");
linearLayout.addView(btn0);
final Button btn1 = new Button(this);
btn1.setText("btn1");
linearLayout.addView(btn1);
final TextView tv2 = new TextView(this);
tv2.setText("tv2");
tv2.setTextSize(30);
linearLayout.addView(tv2);
final Button btn2 = new Button(this);
btn2.setText("btn2");
linearLayout.addView(btn2);
final Button btn3 = new Button(this);
btn3.setText("btn3");
linearLayout.addView(btn3);
/*
* ValueAnimator +時間插值器演示
*/
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 1000);
valueAnimator.setDuration(5000);
valueAnimator.setInterpolator(new DecelerateInterpolator());//設置插值器
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
tv1.setText(animation.getAnimatedValue() + "");
}
});
valueAnimator.start();
/*
ObjectAnimator + PropertyValuesHolder
*/
PropertyValuesHolder xHolder = PropertyValuesHolder.ofFloat("scaleX", 1, 1.5f, 0.8f, 1);
PropertyValuesHolder yHolder = PropertyValuesHolder.ofFloat("scaleY", 1, 1.5f, 0.8f, 1);
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(btn0, xHolder, yHolder);
objectAnimator.setDuration(3000);
// objectAnimator.setRepeatCount(-1);
objectAnimator.start();
/*
* ObjectAnimator + AnimatorSet
*/
ObjectAnimator anim1 = ObjectAnimator.ofFloat(btn1, "scaleX", 1, 1.5f, 0.8f, 1);
ObjectAnimator anim2 = ObjectAnimator.ofFloat(btn1, "scaleY", 1, 1.5f, 0.8f, 1);
final AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(anim1);
animatorSet.play(anim2).with(anim1);
animatorSet.setDuration(3000);
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
if (animatorSet != null) {
animatorSet.start();
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animatorSet.start();
/*
* ObjectAnimator + AnimatorSet
*/
ObjectAnimator anim11 = ObjectAnimator.ofFloat(tv2, "translationX", 200);
ObjectAnimator anim22 = ObjectAnimator.ofFloat(tv2, "scaleX", 1, 1.5f, 1f);
ObjectAnimator anim33 = ObjectAnimator.ofFloat(tv2, "scaleY", 1, 1.5f, 1f);
final AnimatorSet animatorSet2 = new AnimatorSet();
animatorSet2.play(anim11);
animatorSet2.play(anim22).after(anim11);
animatorSet2.play(anim33).with(anim22);
animatorSet2.setDuration(3000);
animatorSet2.start();
}
}