Android中动画分为三种:逐帧动画
补间动画
属性动画
逐帧动画
逐帧动画类似于gif或是电影的原理,通过将一系列图片连续播放来获得动画效果。它本质是一种Drawable,由ImageView播放。
定义逐帧动画有两种方式:
drawable文件夹下定义xml:<?xml version="1.0" encoding="utf-8"?>
android:oneshot="boolean">
android:drawable="@..."
android:duration="integer"/>
android:oneshot 指示是否重复播放
java代码:
创建 AnimationDrawable 对象,使用 addFrame(Drawable frame, int duration) 向动画添加帧。
AnimationDrawable默认是不播放的,调用 start() 、 stop() 来控制动画的播放和停止。
补间动画
补间动画指定动画开始和结束时的状态(透明度、大小、位移、旋转等),由系统根据指定的Interpolator(插值器)来生成过程帧,并实现动画过程。
补间动画有个很明显的缺点, 动画改变的只是显示,并没有改变事件响应的位置 。比方说我们通过补间动画将Button移到了另一个位置,但是我们依然得点Button原来的位置才能触发Button的点击事件。
动画的显示会依赖于父控件的大小,若父控件太小,动画可能会移出父控件边缘,导致动画不可见。
创建动画的基本流程:创建动画实例
设置动画持续时间、插值器等
调用view的 startAnimation() 方法并传入动画实例透明度动画 AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(1000);
view.startAnimation(alphaAnimation)
0代表完全透明,1为不透明。
旋转动画 RotateAnimation rotateAnimation = new RotateAnimation(0, 720, 75, 75); // 开始角度、结束角度、旋转中心X坐标、旋转中心Y坐标(此处X,Y是以View自身为参考系)
RotateAnimation rotateAnimation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 2f, Animation.RELATIVE_TO_SELF, 2f);// 开始角度、结束角度、X旋转参考,X方向上与自身大小比值、Y旋转参考,Y方向上与自身大小比值(参考系依然为View本身)
旋转动画可指定旋转中心的坐标与参考View宽高的比值, RELATIVE_TO_SELF 或 RELATIVE_TO_PARENT ,值得注意的是,X,Y的坐标始终是以 View自身为参考系 。
位移动画 TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 300);// 起始X、结束X、起始Y、结束Y
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 2);// 相对方式
缩放动画 ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 2, 0.5f, 2);// 数字表倍率
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);// 指定缩放中心坐标
动画集合
通过AnimationSet,可以组合各种动画,使其同时播放 AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.setInterpolator(new AccelerateInterpolator());
animationSet.setDuration(1000);
image.startAnimation(animationSet);
监听器
有时候我们需要监听动画的开始或结束等来做进一步操作,这时候可以对动画设置监听器: animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
属性动画
属性动画,顾名思义,是对针对属性的动画,通过持续地改变某个属性来实现View的动画效果。与补间动画不同,属性动画真实地改变了View的属性值。ObjectAnimator ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(button, "translationX", 300);
objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
objectAnimator.setDuration(300);
objectAnimator.start();
要操作的属性必须具有get和set方法。如果属性没有get、set方法,可以自定义一个包装类: private static class WrapperView {
private View mTarget;
public WrapperView(View target) {
this.mTarget = target;
}
public int getWidth() {
return mTarget.getLayoutParams().width;
}
public void setWidth(int width) {
mTarget.getLayoutParams().width = width;
mTarget.requestLayout();
}
}
PropertyValuesHolder
要同时针对多个属性进行动画时使用: PropertyValuesHolder propertyValuesHolder1 = PropertyValuesHolder.ofFloat("translationX", 300);
PropertyValuesHolder propertyValuesHolder2 = PropertyValuesHolder.ofFloat("scaleX", 1, 0.1f);
PropertyValuesHolder propertyValuesHolder3 = PropertyValuesHolder.ofFloat("scaleY", 1, 0.1f);
ObjectAnimator.ofPropertyValuesHolder(button, propertyValuesHolder1, propertyValuesHolder2, propertyValuesHolder3).setDuration(1000).start();
ValueAnimator
ValueAnimator更像是一个数值发生器,使用时通过监听数值的变换来自己完成动画的实现: ValueAnimator animator = ValueAnimator.ofFloat(0, 100);
animator.setTarget(view);
animator.setDuration(1000).start();
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float value = (Float) animation.getAnimatedValue();
// TODO use the value
}
});
动画时间监听
一个完整动画有四个过程:start、repeat、end、cancel。有两种监听器可以选: animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
或: animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
AnimatorListenerAdapter可以按需要实现监听start、repeat、end、cancel、pause、resume过程。
AnimatorSet
AnimatorSet 不能能像PropertyValuesHolder那样同时实现多个动画,同时还能精准控制动画的顺序: ObjectAnimator animator1 = ObjectAnimator.ofFloat(button, "translationX", 300);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(button, "scaleX", 1, 0, 1);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(button, "scaleY", 1, 0, 1);
AnimatorSet set = new AnimatorSet();
set.setDuration(1000);
set.playTogether(animator1, animator2, animator3);
set.start();
playTogether、playSequentially、play().with()、play().before()、play().after()等来控制动画顺序。
xml中使用属性动画
animator文件夹下xml: <?xml version="1.0" encoding="utf-8"?>
android:duration="1000"
android:propertyName="scaleX"
android:valueFrom="1.0"
android:valueTo="2.0"
android:valueType="floatType">
java代码中: Animator animator = AnimatorInflater.loadAnimator(this, R.animator.scalex);
animator.setTarget(view);
animator.start();
view的animate方法 button.animate().alpha(0).y(300).setDuration(300)
.withStartAction(new Runnable() {
@Override
public void run() {
}
})
.withEndAction(new Runnable() {
@Override
public void run() {
}
})
.start();
布局动画
布局动画是作用在ViewGroup上当布局中View增加的动画过渡效果。可在xml中添加 android:animateLayoutChanges="true" ,为布局添加默认动画。
java中自定义动画:LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1);
sa.setDuration(1000);
LayoutAnimationController lac = new LayoutAnimationController(sa, 0.5f); // 第二个参数是延迟
lac.setOrder(LayoutAnimationController.ORDER_NORMAL); // 当延迟不为0时,可指定View显示顺序,还有随机和反序
ll.setLayoutAnimation(lac);
来自:http://www.jianshu.com/p/e1591694ccbe