屬性動畫的原理:初始化動畫值,然后把該值設(shè)置到屬性中,接著注冊垂直同步信號,最后進行View重繪。這個過程是一直循環(huán)進行的。動畫對值進行了修改,但是沒有涉及到重繪,是因為垂直同步信號,在安卓中每隔16ms發(fā)送一次,對View進行了重新繪制測量和布局。
API3.0之后提出的動畫模式,優(yōu)點如下
- 不再局限于View對象,無對象也可以進行動畫處理。
- 不再局限于4種基本變換:平移,旋轉(zhuǎn),縮放,透明度。
- 可以靈活的操作任意對象屬性,根據(jù)自己業(yè)務(wù)來實現(xiàn)自己想要的結(jié)果。
核心API
- ObjectAnimator 對象動畫
- ValueAnimator 值動畫
- PropertyValuesHolder 用于多個動畫同時執(zhí)行
- TypeEvaluator 估值器
- Interpolator 插值器
- AnimatorSet 動畫集合
基礎(chǔ)使用
- ObjectAnimator 操作對象屬性,不局限于對象,并且動畫可以一起執(zhí)行。
ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(v, "alpha", 1.0f, 0.3f,1.0f);
alphaAnim.setDuration(1000);//執(zhí)行時間
alphaAnim.setStartDelay(300);//延遲
alphaAnim.start();
“alpha”是透明度,除此之外,還有平移,縮放,旋轉(zhuǎn)。
- AnimatorSet 動畫集合
ObjectAnimator除了單個執(zhí)行外,還可以使用AnimatorSet一起執(zhí)行動畫。
ObjectAnimator animator1 = ObjectAnimator.ofFloat(iv, "translationX", 0f, 500F);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(iv, "alpha", 0f, 1f);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(iv, "scaleX", 0f, 2f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(500);
//animatorSet.play(animator3).with(animator2).before(animator1);//鏈?zhǔn)秸{(diào)用順序
//animatorSet.play(animator3).with(animator2).after(animator1);//鏈?zhǔn)秸{(diào)用順序
//animatorSet.playTogether(animator1,animator2,animator3);//一起執(zhí)行
animatorSet.playSequentially(animator2, animator1, animator3);//順序執(zhí)行
animatorSet.start();
- ValueAnimator 值動畫
上面的ObjectAnimator設(shè)置的屬性的名字都是系統(tǒng)要求固定的,還可以使用其它任意的名字來進行操作,需要配置ValueAnimator:
ObjectAnimator animator = ObjectAnimator.ofFloat(v, "hello", 0f, 100f,50f);
animator.setDuration(300);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (float) animation.getAnimatedValue();//百分比的所對應(yīng)的值
Log.d("dn_alan", value + "");
v.setScaleX(0.5f + value / 200);
v.setScaleY(0.5f + value / 200);
}
});
animator.start();
//animator.setRepeatCount(2);//重復(fù)次數(shù)
//animator.setRepeatCount(ValueAnimator.INFINITE);//無限次數(shù)
- Interpolator 插值器
插值器是由API提供的一組算法,用來操作動畫執(zhí)行是的變換規(guī)則,省去了一些自己寫算法的麻煩,大致分為九種
ValueAnimator animator = new ValueAnimator();
animator.setDuration(3000);
animator.setObjectValues(new PointF(0, 0));
final PointF point = new PointF();
//估值
animator.setEvaluator(new TypeEvaluator() {
@Override
public Object evaluate(float fraction, Object startValue, Object endValue) {
point.x = 100f * (fraction * 5);
// y=vt=1/2*g*t*t(重力計算)
point.y = 0.5f * 98f * (fraction * 5) * (fraction * 5);
return point;
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
v.setX(point.x);
v.setY(point.y);
}
});
//加速插值器,公式: y=t^(2f) (加速度參數(shù). f越大,起始速度越慢,但是速度越來越快)
animator.setInterpolator(new AccelerateInterpolator(10));
//減速插值器公式: y=1-(1-t)^(2f) (描述: 加速度參數(shù). f越大,起始速度越快,但是速度越來越慢)
//animator.setInterpolator(new DecelerateInterpolator());
//先加速后減速插值器 y=cos((t+1)π)/2+0.5
//animator.setInterpolator(new AccelerateDecelerateInterpolator());
//張力值, 默認(rèn)為2,T越大,初始的偏移越大,而且速度越快 公式:y=(T+1)×t3–T×t2
//animator.setInterpolator(new AnticipateInterpolator());
//張力值tension,默認(rèn)為2,張力越大,起始和結(jié)束時的偏移越大,
//而且速度越快;額外張力值extraTension,默認(rèn)為1.5。公式中T的值為tension*extraTension
//animator.setInterpolator(new AnticipateOvershootInterpolator());
//彈跳插值器
//animator.setInterpolator(new BounceInterpolator());
//周期插值器 y=sin(2π×C×t) 周期值,默認(rèn)為1;2表示動畫會執(zhí)行兩次
//animator.setInterpolator(new CycleInterpolator(2));
//線性插值器,勻速公式:Y=T
//animator.setInterpolator(new LinearInterpolator());
//公式: y=(T+1)x(t1)3+T×(t1)2 +1
//描述: 張力值,默認(rèn)為2,T越大,結(jié)束時的偏移越大,而且速度越快
//animator.setInterpolator(new OvershootInterpolator());
animator.start();
- PropertyValuesHolder 用于多個動畫同時執(zhí)行
PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("alpha", 1f, 0.5f);
PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.5f);
PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.5f);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(v, holder1, holder2, holder3);
animator.setDuration(200);
animator.start();
- TypeEvaluator 估值器的核心目的:自定義變換規(guī)則
ValueAnimator animator = new ValueAnimator();
animator.setDuration(3000);
animator.setObjectValues(new PointF(0, 0));
final PointF point = new PointF();
//估值
animator.setEvaluator(new TypeEvaluator() {
@Override
public Object evaluate(float fraction, Object startValue, Object endValue) {
point.x = 100f * (fraction * 5);
// y=vt=1/2*g*t*t(重力計算)
point.y = 0.5f * 130f * (fraction * 5) * (fraction * 5);
return point;
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
PointF p = (PointF) animation.getAnimatedValue();
Log.d("dn_alan", p.x + "===" + p.y);
v.setX(p.x);
v.setY(p.y);
}
});
animator.start();
附有源碼兩篇文章:
屬性動畫源碼分析-ofFloat方法
屬性動畫源碼分析-start方法