參考官方文檔:https://developer.android.com/guide/topics/graphics/prop-animation#api-overview
主要內(nèi)容
- 1.ValueAnimator
- 2.ObjectAnimator(包括AnimatorSet矾兜,PropertyValuesHolder)
- 3.AnimatorSet 和 PropertyValuesHolder
- 4.插值器(Interpolator)
valueAnimator
valueAnimator可以控制某個值的變化,通過在值變化的同時刷新View來實現(xiàn)動畫患久。
下面的例子是實現(xiàn)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執(zhí)行屬性動畫椅寺,常見的屬性動畫有旋轉(zhuǎn)浑槽,平移,縮放等返帕。
ObjectAnimator一般是配合著AnimatorSet或者PropertyValuesHolder使用桐玻,demo如下,以下兩塊代碼實現(xiàn)的是同一種效果:“btn先放大后縮小”荆萤。
屬性動畫有以下幾種:(來自官方文檔)
- translationX,translationY:從當(dāng)前位置開始平移
- rotation, rotationX, rotationY:旋轉(zhuǎn)
- scaleX , scaleY:縮放
- pivotX , pivotY:旋轉(zhuǎn)和縮放的參照點镊靴,默認是View的中心
- x , y:平移到當(dāng)前容器的某個絕對位置
- 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:
適合在實現(xiàn)“單個View連續(xù)動畫”的情況下使用,使用AnimatorSet在一些情況下會比PropertyValuesHolder復(fù)雜一些链韭。
比如要實現(xiàn)View的循環(huán)播放偏竟,使用PropertyValuesHolder實現(xiàn)的代碼如下:
objectAnimator.setRepeatCount(-1);
使用AnimatorSet實現(xiàn)的代碼如下:
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
適合在動畫比較復(fù)雜的情況下使用,比如有多個View的動畫需要同時進行或者交替進行敞峭,這種情況使用PropertyValuesHolder是很難實現(xiàn)的踊谋。
再比如,對于同一個View的動畫不連續(xù)的情況旋讹,PropertyValuesHolder也比較難實現(xiàn)褪子,或者說實現(xiàn)更加復(fù)雜。
demo中骗村,View執(zhí)行完平移之后再執(zhí)行縮放邏輯嫌褪。
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)
插值器可以控制動畫變化的速率,設(shè)置非常簡單胚股,代碼如下:
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 1000);
valueAnimator.setDuration(5000);
valueAnimator.setInterpolator(new DecelerateInterpolator());//設(shè)置插值器
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
tv1.setText(animation.getAnimatedValue() + "");
}
});
valueAnimator.start();
插值器類型:(來自官方文檔)
- AccelerateDecelerateInterpolator:開始和結(jié)束的時候慢笼痛,中間快
- AccelerateInterpolator:開始的時候慢,然后加速
- AnticipateInterpolator:開始先后退琅拌,然后向前
- AnticipateOvershootInterpolator: 開始先后退缨伊,然向前到超標(biāo),最后回到最終值
- BounceInterpolator :最后會反彈
- CycleInterpolator:動畫會重復(fù)一定的周期數(shù)
- DecelerateInterpolator:開始快进宝,然后減速
- LinearInterpolator:變化勻速
- OvershootInterpolator:到達最終值后超標(biāo)刻坊,再回到最終值
- 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());//設(shè)置插值器
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();
}
}