Android Property Animation
Property Animation介紹:
出生在3.0,是利用了View所擁有的屬性,進行一系列的操作。比如一個View有什麼樣的setAbc的屬性,那麼理論上就可以設定它。
它不僅改變View的繪製,也改變了View的屬性;而Tween Animation 只改變View的繪製。
ValueAnimator 為動畫的基類,它有一個子類ObjectAnimator。需要Interpolator和TypeEvaluator來計算屬性值。
Interpolator 時間插入器,動畫的運動速率,參見上一篇Tween Animation
TypeEvaluator Animator的Factory 方法 of方法能建立屬性為任何值類型的對象。TypeEvaluator就是用來計算屬性值的
3.0以後新增了一些View的屬性:
1)translationX 和 translationY:這兩個屬性控制了View所處的位置,
它們的值是由layout容器設定的,是相對於座標原點(0,0左上方)的一個位移量。
2)rotation, rotationX 和 rotationY:控制View繞著軸點(pivotX和pivotY)旋轉。它的表現跟Tween Animation中的RotateAnimation不一致。
RotateAnimation 的旋轉,表現為平面的旋轉
而rotationX、Y 旋轉,是立體的旋轉,預設是以View的中心點,做x y的水平線,面向水平線進行翻轉
3)scaleX 和 scaleY:控制View基於pivotX和pivotY的縮放。
4)pivotX 和 pivotY:旋轉的軸點和縮放的基準點,預設是View的中心點。
5)x 和 y:描述了view在其父容器中的最終位置,是左上方左標和位移量(translationX,translationY)的和。
6)aplha:透明度,1是完全不透明,0是完全透明。
以上這些屬性與Tween Animation的動畫屬性值差不多
ObjectAnimator 對象動畫
該動畫,一次只能表示一個動作屬性。
ObjectAnimator的xml實現xml定義動畫
res/animator/scale_object_animator.xml
代碼載入 動畫xml
imageview_scale.setBackground(getResources().getDrawable(R.drawable.a11));ObjectAnimator scaleAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.scale_object_animator);scaleAnimator.setTarget(imageview_scale);//設定動畫作用的目標對象scaleAnimator.setDuration(1000);scaleAnimator.setRepeatCount(50);scaleAnimator.start();
AnimatorSet 動畫集
由ObjectAnimator 和 ValueAnimator 組成,對應的xml中的寫法 類似為
xml定義動畫
res/animator/set_rotate_scale.xml
代碼載入 動畫xml
imageview_rotate.setBackground(getResources().getDrawable(R.drawable.a11));AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.set_rotate_scale);animatorSet.setTarget(imageview_rotate);animatorSet.setDuration(1000);animatorSet.setInterpolator(new BounceInterpolator());//設定end時的彈跳插入器animatorSet.start();
PropertyValuesHolder
//使用PropertyValuesHolder 構造 Animator 組合成類似set的效果PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("scaleX",0f,2.5f);PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleY",0f,3f); ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(imageview, pvhX,pvhY);animator.setDuration(2000);animator.start();ViewPropertyAnimator
通過view.animate()來擷取ViewPropertyAnimator
imageview.setBackground(getResources().getDrawable(R.drawable.a11));ViewPropertyAnimator animate = imageview.animate();//該對象沒有setRepeat的方法//通過一些動畫屬性來設定 組合成類似set的效果animate.alpha(0);animate.rotationX(50);animate.translationXBy(500);animate.scaleX(1.5f);animate.scaleY(1.5f);animate.setInterpolator(new BounceInterpolator());animate.setDuration(2000);animate.start();
ValueAnimator
ValueAnimator代碼和xml設定中 沒有setPropertyName 因為不是操作對象,只是根據value進行某種動作
需要加監聽器,監聽值的變化 做相應的處理
xml定義動畫
代碼載入 動畫xml
ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator);valueAnimator.setTarget(tv_num);valueAnimator.setEvaluator(new TypeEvaluator() {@Override public Integer evaluate(float fraction, Integer startValue, Integer endValue) {System.out.println("百分比,fraction:" + fraction);System.out.println("結果值:" + (int)((startValue + fraction * (endValue - startValue)) / 10 * 10)); return (int)((startValue + fraction * (endValue - startValue)) / 10 * 10);}});valueAnimator.addUpdateListener(new AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {//在onAnimationUpdate中 該值返回第一個動畫的 當前幀的evaluate 值System.out.println("animation.getAnimatedValue()==" + animation.getAnimatedValue());tv_num.setText(animation.getAnimatedValue() + "");}});//valueAnimator.setInterpolator(new LinearInterpolator());valueAnimator.start();
Animator的監聽器AnimatorListener
new AnimatorListener() {@Overridepublic void onAnimationStart(Animator animation) {}@Overridepublic void onAnimationRepeat(Animator animation) {}@Overridepublic void onAnimationEnd(Animator animation) {}@Overridepublic void onAnimationCancel(Animator animation) {}}
AnimatorListenerAdapter
new AnimatorListenerAdapter() {//空實現了AnimatorListener@Overridepublic void onAnimationCancel(Animator animation) {super.onAnimationCancel(animation);}@Overridepublic void onAnimationEnd(Animator animation) {super.onAnimationEnd(animation);}@Overridepublic void onAnimationRepeat(Animator animation) {super.onAnimationRepeat(animation);}@Overridepublic void onAnimationStart(Animator animation) {super.onAnimationStart(animation);}@Overridepublic void onAnimationPause(Animator animation) {super.onAnimationPause(animation);}@Overridepublic void onAnimationResume(Animator animation) {super.onAnimationResume(animation);}}AnimatorUpdateListener
new AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {//在onAnimationUpdate中 該值返回第一個動畫的 當前幀的evaluate 值System.out.println("animation.getAnimatedValue()==" + animation.getAnimatedValue());}}
一些操作函數:animator.pause(); animator.resume();animator.reverse(); animator.end();animator.cancel();
animator.start();animator.isStarted();animator.isPaused();animator.isRunning();
用屬性動畫換背景色
詳見ApiDemo要下的 BouncingBalls.java
private static final int RED = 0xffFF8080;private static final int BLUE = 0xff8080FF;private static final int CYAN = 0xff80ffff;private static final int GREEN = 0xff80ff80;{//動畫 變色 ObjectAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", CYAN, BLUE, RED);colorAnim.setTarget(ll_animation);colorAnim.setEvaluator(new ArgbEvaluator());colorAnim.setRepeatCount(ValueAnimator.INFINITE);colorAnim.setRepeatMode(ValueAnimator.REVERSE);colorAnim.setDuration(3000);colorAnim.start();}