概述
屬性動畫,API3.0之後提出的動畫模式
- 優點:
- 不在侷限於View 對象,無對象也可以進行動畫處理
- 不再侷限於4種基本變換:平移、旋轉、縮放 、透明度
- 可以靈活的操作任意對象屬性,根據自己業務來實現自己想要的結果
- 核心:
- ObjectAnimator 對象動畫(重點)
- ValueAnimator 值動畫(重點)
- PropertyValueHolder 用於多個動畫同時執行
- TypeEvaluator 估值器
- Interpolator 插值器
- AnimatorSet 動畫集合
屬性動畫要求動畫作用的對象提供該屬性的set方法,屬性動畫根據傳遞的該屬性的初始值和最終值,以動畫的效果多次去調用set方法。每次傳給set方法的值都不一樣,確切來說是隨着時間的推移,所傳遞的值越來越接近最終值。
使用和效果顯示
ObjectAnimator
/**
* 操作對象屬性,不侷限於對象
* 完成透明動畫
*/
public void startObjectAnimatorAnim(View v) {
//1.設置參數
//2.執行動畫
ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(v, "alpha", 1.0f, 0.3f, 1.0f);
alphaAnim.setDuration(1000);//執行時間
alphaAnim.setStartDelay(300);//延遲
alphaAnim.start();
}
ValueAnimator
public void startValueAnimatorAnim(final View v) {
//組合使用300
ObjectAnimator animator = ObjectAnimator.ofFloat(v, "hehe", 0f, 100f, 50f);
animator.setDuration(300);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// animation.getAnimatedFraction();//百分比
float value = (float) animation.getAnimatedValue();//百分比的所對應的值
Log.d("hongx", value + "");
v.setScaleX(0.5f + value / 200);
v.setScaleY(0.5f + value / 200);
}
});
animator.start();
// animator.setRepeatCount(2);//重複次數
animator.setRepeatCount(ValueAnimator.INFINITE);//無限次數
}
PropertyValueHolder
public void startPropertyValueHolderAnim(View v) {
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();
}
AnimatorSet
public void startAnimatorSet(View v) {
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).after(animator1);//鏈式調用順序
//animatorSet.play(animator3).with(animator2).before(animator1);//animator1
//animatorSet.playTogether(animator1,animator2,animator3);//一起執行
animatorSet.playSequentially(animator2, animator1, animator3);//順序執行
animatorSet.start();
}
TypeEvaluator
/**
* 估值器
* 自由落體效果實現
* 核心目的:自定義變換規則
*/
public void startEvaluator(final View v) {
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("hongx", p.x + "===" + p.y);
v.setX(p.x);
v.setY(p.y);
}
});
animator.start();
}
Interpolator
/**
* 插值器
* 由API提供的一組算法,用來操作動畫執行是的變換規則,省去了一些自己寫算法的麻煩,大致分爲九種
*/
public void startInterpolatorApply(final View v) {
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) (加速度參數. f越大,起始速度越慢,但是速度越來越快)
// animator.setInterpolator(new AccelerateInterpolator(10));
// 減速插值器公式: y=1-(1-t)^(2f) (描述: 加速度參數. f越大,起始速度越快,但是速度越來越慢)
// animator.setInterpolator(new DecelerateInterpolator());
// 先加速後減速插值器 y=cos((t+1)π)/2+0.5
// animator.setInterpolator(new AccelerateDecelerateInterpolator());
// 張力值, 默認爲2,T越大,初始的偏移越大,而且速度越快 公式:y=(T+1)×t3–T×t2
// animator.setInterpolator(new AnticipateInterpolator());
// 張力值tension,默認爲2,張力越大,起始和結束時的偏移越大,
// 而且速度越快;額外張力值extraTension,默認爲1.5。公式中T的值爲tension*extraTension
// animator.setInterpolator(new AnticipateOvershootInterpolator());
// 彈跳插值器
animator.setInterpolator(new BounceInterpolator());
// 週期插值器 y=sin(2π×C×t) 週期值,默認爲1;2表示動畫會執行兩次
// 週期插值器 y=sin(2π×C×t) 週期值,默認爲1;2表示動畫會執行兩次
// animator.setInterpolator(new CycleInterpolator(2));
// 線性插值器,勻速公式:Y=T
// animator.setInterpolator(new LinearInterpolator());
// 公式: y=(T+1)x(t1)3+T×(t1)2 +1
// 描述: 張力值,默認爲2,T越大,結束時的偏移越大,而且速度越快
// animator.setInterpolator(new OvershootInterpolator());
animator.start();
}