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、Translation1.代码 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() {
@Override
public 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() {
@Override
public 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