標籤:
Android動畫基本上分為兩個時代,一個是3.0(api14)之前的Animation動畫,一個是3.0之後的Animator動畫。
Animation動畫ViewGroup在繪製當前所要播放動畫的view的時候,先回通過ViewGroup的drawChild函數擷取該View的Animation的Transformation值, 然後調用canvas.concat方法,通過矩陣運算和invalidate方法完成動畫,這樣消耗更多的CPU資源。
1. Tween動畫Alpha、Scale、Rotate、Translation
1.代碼 AnimationSet動畫屬性集
2.布局 AnimationUtil
2. Frame動畫圖片輪詢播放
Animator動畫
採用AnimatorSet和ObjectAnimator配合,使用ObjectAnimator進行更精細化控制,並且能夠自動驅動,多個ObjectAnimator組合到AnimatorSet形成一個動畫。減少動畫過程中頻繁繪製,減少CPU的資源消耗.
可操縱的屬性參數:x/y;scaleX/scaleY;rotationX/ rotationY;transitionX/ transitionY;alpha
1.translationX和translationY:控制著View對象從它版面配置容器的左上方座標開始的位置。
2.rotation、rotationX和rotationY:控制View對象圍繞支點進行2D和3D旋轉。
3.scaleX和scaleY:控制著View對象圍繞它的支點進行2D縮放。
4.pivotX和pivotY 控制著View對象的支點位置,預設情況下,該支點的位置就是View對象的中心點。
5.x和y:描述了View對象在它的容器中的最終位置,它是最初的左上方座標和translationX和translationY值的累計和。
6.alpha:它表示View對象的alpha透明度。預設值是1,0代表完全透明。
ValueAnimator---自由落體
final ValueAnimator animator = ValueAnimator.ofFloat(0, screenHeight - imageView.getHeight());animator.setTarget(view);animator.setInterpolator(new BounceInterpolator());animator.setDuration(1000).start();animator.addUpdateListener(new AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {Float value = (Float) animation.getAnimatedValue();imageView.setTranslationY(value);}});
或者:
ObjectAnimator animator = ObjectAnimator.ofFloat(mImageView, "translationY", 100.0f, 0.0f);animator.setInterpolator(new BounceInterpolator());animator.setDuration(1000);animator.start();
ObjectAnimator---透明度和位移
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 0f, 1.0f);animator.setDuration(1000);animator.start();animator.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {super.onAnimationEnd(animation);imageView.setTranslationY(400);}});或者:
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"android:duration="1500"android:propertyName="alpha"android:valueFrom="0.0"android:valueTo="1.0"android:valueType="floatType" ></objectAnimator>Animator animator = AnimatorInflater.loadAnimator(LoginAlphaAnimation.this, R.animator.alpha_login);animator.setTarget(mImageView);animator.start();
AnimatorSet---組合動畫
ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f);ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 2f);ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 500f);ObjectAnimator animator4 = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0f);AnimatorSet set = new AnimatorSet();set.setDuration(1000);set.playTogether(animator1, animator2, animator3,animator4);set.start();
下載Demo
Android動畫使用