说起动画,其实一点也不陌生,在使用一款app的时候为了优化用户体验,多多少少的,都会加入动画。

安卓中的动画,分为两大类:补间动画和属性动画。本篇博文会详细介绍总结这两大动画,希望本篇博文对你的学习和生活有所帮助。

**补间动画**

 补间动画分为四类:平移动画,旋转动画,缩放动画和渐变动画。这几类动画用法都差不多,只是对象参数不同这里我统一展示出来。以下是效果图:

android ObjectAnimator移动后的位置 android平移动画_ide

实现代码很简单:



btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //平移动画
                TranslateAnimation translate=new TranslateAnimation(Animation.RELATIVE_TO_SELF,300,
                        Animation.RELATIVE_TO_SELF,300);
                translate.setDuration(3000);
                translate.setFillAfter(true);
                img.startAnimation(translate);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //旋转动画
                RotateAnimation rotate=new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0.5f);
                rotate.setFillAfter(true);
                rotate.setDuration(3000);
                img.startAnimation(rotate);
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //缩放动画
                ScaleAnimation scale=new ScaleAnimation(0,2,0,2,Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0.5f);
                scale.setDuration(3000);
                img.startAnimation(scale);
            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //渐变动画
                AlphaAnimation alpha =new AlphaAnimation(0,1);
                alpha.setDuration(3000);
                img.startAnimation(alpha);
            }
        });



 总结以下:

AlphaAnimation:渐变动画,0.0f完全透明,1.0f完全显示。

RotateAnimation:旋转动画。起始角度,结束角度,相对值,横坐标的比例,相对值,纵坐标的比例。(结束角度为正就为顺时针,负数为逆时针)

ScaleAnimation:比例动画,x的位置比例起始,x的位置比例结束,y的位置比例起始,y的位置比例结束,x的锚点,x的参数,y的锚点,y的参数



setDuration:设置动画播放时间。



true):界面停留在动画结束的状态·而不是初始化状态。



 除此之外我们还可以将这几个动画效果组合起来,实现组合动画。



效果图:




android ObjectAnimator移动后的位置 android平移动画_移动开发_02


代码也是简单易懂:


AnimationSet animationSet=new AnimationSet(false);
                TranslateAnimation translate=new TranslateAnimation(Animation.RELATIVE_TO_SELF,300,
                        Animation.RELATIVE_TO_SELF,300);
                translate.setDuration(3000);
                translate.setFillAfter(true);
                animationSet.addAnimation(translate);
                RotateAnimation rotate=new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0.5f);
                rotate.setFillAfter(true);
                rotate.setDuration(3000);
                animationSet.addAnimation(rotate);
                ScaleAnimation scale=new ScaleAnimation(0,2,0,2,Animation.RELATIVE_TO_SELF,0.5f,
                        Animation.RELATIVE_TO_SELF,0.5f);
                scale.setDuration(3000);
                animationSet.addAnimation(scale);
                AlphaAnimation alpha =new AlphaAnimation(0,1);
                alpha.setDuration(3000);
                animationSet.addAnimation(alpha);
                img.startAnimation(animationSet);


AnimationSet通过addAnimation()方法,可以将动画结合起来,实现更多的动画效果。

**属性动画**

早起的补间动画是没有改变view的参数的,view在刷新的时候会变回原来的样子。所以,在安卓3.0之后,谷歌推出了属性动画。

而属性动画的实质就是通过例如:translationX,translationY,scaleX,scaleY,rotationX,rotationY等等,这些新增的属性,记录View的值

从而使View的动画可以得到保存,刷新后不会消失。

下面是一个简单例子,通过属性动画改变x上的缩放值:

android ObjectAnimator移动后的位置 android平移动画_移动开发_03

接下来是实现代码:


btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(img,"scaleX",0.5f);
                objectAnimator.setDuration(3000);
                objectAnimator.setStartDelay(1000);
                objectAnimator.start();
            }
        });


如果这样实现,其实是有很多弊端的。因为,属性动画只有在3.0的版本之后能用,版本过低,是没法使用的。并且,实现一个属性动画,参数也过于繁琐。所以,这里推荐

使用开源框架:nineoldandroids。

如果你使用的是Android studio 在gradle中加入:dependencies {
compile 'com.nineoldandroids:library:2.4.0'
}

 使用这个开源框架,不但能在低版本中使用属性动画,而且,实现更为简单更加方便。


btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //NIneOldAndroid中的一个类实现
                ViewPropertyAnimator
                        .animate(img)
                        .scaleX(0.5f)
                        .setInterpolator(new OvershootInterpolator())//超过一点回来
                        .setDuration(3000)
                        .start();
            }
        });


一个简单的例子:

android ObjectAnimator移动后的位置 android平移动画_属性动画_04

怎么样,是不是更加好看了?

代码也是非常简单:


ViewPropertyAnimator
                        .animate(img)
                        .translationY(600)
                        .setInterpolator(new BounceInterpolator())//弹性掉落
                        .setDuration(1500)
                        .start();


还可以这样:

android ObjectAnimator移动后的位置 android平移动画_ide_05

代码:


ViewPropertyAnimator
                        .animate(img)
                        .translationX(40)
                        .setInterpolator(new CycleInterpolator(6))//左右来回抖动
                        .setDuration(1500)
                        .start();