您现在的位置是:首页 > 文章详情

Android动画之补间动画详解

日期:2018-07-30点击:313

一.概念

补间动画是指开发者无需定义动画过程中的每一帧,只需要定义动画的开始和结束两个关键帧,并指定动画变化的时间和方式等,然后交由Android系统进行计算,通过在这两个关键帧之间插入渐变值来实现平滑过渡,从而对View的内容完成一系列的图形变换来实现动画效果,主要包括四种基本效果:透明变化Alpha、大小变化Scale、位移变化Translate、以及旋转变化Route,这四种效果可以动态组合,从而实现复杂灵活的动画。同样,定义补间动画也可以分为XML资源文件和代码两种方式。

二.Android中动画的实现类介绍

Android系统中使用Animation类表示抽象的动画类,上面介绍的四种补间动画分别对应着如下几个动画类。

img_7ba96de251ff9c8b56c81b13be445f7e.png
image

三.动画的实现

1.AlphaAnimation

XML资源文件方式:

1).在res/anim文件中创建alphaanim.xml文件(如果没有anim文件夹的话需要自己新建)
2).在alphaanim.xml中配置动画的属性
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" //一次动画的持续时间 android:startOffset ="1000" //开始动画之前的时间间隔 android:fillEnabled= "true"//是否允许动画结束之后回到开始的状态默认为true android:fillBefore = "true" //动画结束后是否回到初始状态fillEnabled为true时此属性生效,默认为true android:fillAfter = "false"//动画结束后是否保持结束的状态 android:repeatMode= "restart"//重复播放的模式,restart代表正序重放,reverse代表倒序回放 android:repeatCount = "0"//重复的次数为infinite或-1时无限重复 android:interpolator ="@android:anim/linear_interpolator"//插值器,改变动画速率 android:fromAlpha="1"//开始时的透明度范围为0~1 android:toAlpha="0" //结束时的透明度取值范围为0~1> </alpha> 
3).在MainActivity中
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alphaanim); imageView.startAnimation(animation); 
4).运行效果
img_5f834ff42beed89fa3500a929c5acf54.gif

代码方式:(运行效果与XML方式一致)

 animation=new AlphaAnimation(1,0);//设置开始透明度和结束透明度范围0~1 animation.setDuration(2000);//设置单次动画的重复时间 animation.setFillAfter(false);//设置动画结束后是否保持结束时的状态,默认为false animation.setFillEnabled(true);//设置是否允许动画结束后保持开始时状态默认为true animation.setFillBefore(true);//设置动画结束时是否保持开始时的状态,默认为true animation.setInterpolator(new LinearInterpolator());//设置动画的插值器 animation.setRepeatMode(Animation.RESTART);//设置动画的重复模式默认为Animation.RESTART animation.setRepeatCount(0);//设置动画的重复次数0为1次Animation.INFINITE为无限重复 imageView.startAnimation(animation);//开始动画 

2.ScaleAnimation

XML资源文件方式:

1).在res/anim文件中创建scaleanim.xml文件(如果没有anim文件夹的话需要自己新建)
2).在scaleanim.xml中配置动画的属性
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" //一次动画的持续时间 android:startOffset ="1000" //开始动画之前的时间间隔 android:fillEnabled= "true"//是否允许动画结束之后回到开始的状态默认为true android:fillBefore = "true" //动画结束后是否回到初始状态fillEnabled为true时此属性生效,默认为true android:fillAfter = "false"//动画结束后是否保持结束的状态 android:repeatMode= "restart"//重复播放的模式,restart代表正序重放,reverse代表倒序回放 android:repeatCount = "0"//重复的次数为infinite或-1时无限重复 android:interpolator ="@android:anim/linear_interpolator"//插值器,改变动画速率 android:fromXScale="1.0" //初始的X轴缩放倍数 android:toXScale="2.0" //结束时的X轴缩放倍数 android:fromYScale="1.0" //初始的Y轴缩放倍数 android:toYScale="2.0" //结束时Y轴的缩放倍数 android:pivotX="50%" //缩放的中心点X轴坐标 android:pivotY="50%" //缩放的中心点Y轴坐标 > </scale> 
3).在MainActivity中
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scaleanim); imageView.startAnimation(animation); 
4).运行效果
img_bcd8f0b016dc87485e042b54680d1aea.gif
image

代码方式:(运行效果与XML方式一致)

 animation=new ScaleAnimation(1,2,1,2,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); // fromX:初始X轴缩放倍数 // toX:结束时X轴缩放倍数 // fromY:初始Y轴缩放倍数 // toY:结束时Y轴的缩放倍数 // Animation.RELATIVE_TO_SELF:相对于自身 // Animation.RELATIVE_TO_PARENT:相对于父控件 // Animation.ABSOLUTE:相对于窗口0 // pivotXValue:缩放中心X轴的坐标百分百比(float类型) // pivotYValue;缩放中心Y轴的坐标百分百比(float类型) animation.setDuration(2000);//设置单次动画的重复时间 animation.setFillAfter(false);//设置动画结束后是否保持结束时的状态,默认为false animation.setFillEnabled(true);//设置是否允许动画结束后保持开始时状态默认为true animation.setFillBefore(true);//设置动画结束时是否保持开始时的状态,默认为true animation.setInterpolator(new LinearInterpolator());//设置动画的插值器 animation.setRepeatMode(Animation.RESTART);//设置动画的重复模式默认为Animation.RESTART animation.setRepeatCount(0);//设置动画的重复次数0为1次Animation.INFINITE为无限重复 imageView.startAnimation(animation); 

3.TranslateAnimation

XML资源文件方式:

1).在res/anim文件中创建translateanim.xml文件(如果没有anim文件夹的话需要自己新建)
2).在translateanim.xml中配置动画的属性
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" //一次动画的持续时间 android:startOffset ="1000" //开始动画之前的时间间隔 android:fillEnabled= "true"//是否允许动画结束之后回到开始的状态默认为true android:fillBefore = "true" //动画结束后是否回到初始状态fillEnabled为true时此属性生效,默认为true android:fillAfter = "false"//动画结束后是否保持结束的状态 android:repeatMode= "restart"//重复播放的模式,restart代表正序重放,reverse代表倒序回放 android:repeatCount = "0"//重复的次数为infinite或-1时无限重复 android:interpolator ="@android:anim/linear_interpolator"//插值器,改变动画速率 android:fromXDelta="0" //开始时X轴的移动距离 android:toXDelta="500" //结束时X轴的移动距离 android:fromYDelta="0" //开始时Y轴的移动距离 android:toYDelta="500" //结束时Y轴的移动距离 > </translate> 
3).在MainActivity中
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translateanim); imageView.startAnimation(animation); 
4).运行效果
img_048087f9576caf684a3248f827e08164.gif
image

代码方式:(运行效果与XML方式一致)

animation=new TranslateAnimation(0,500,0,500); // fromXDelta:开始时X轴的移动距离 // toXDetlta:结束时X轴的移动距离 // fromYDelta:开始时Y轴的移动距离 // toYDelta:结束时Y轴的移动距离 animation.setDuration(2000);//设置单次动画的重复时间 animation.setFillAfter(false);//设置动画结束后是否保持结束时的状态,默认为false animation.setFillEnabled(true);//设置是否允许动画结束后保持开始时状态默认为true animation.setFillBefore(true);//设置动画结束时是否保持开始时的状态,默认为true animation.setInterpolator(new LinearInterpolator());//设置动画的插值器 animation.setRepeatMode(Animation.RESTART);//设置动画的重复模式默认为Animation.RESTART animation.setRepeatCount(0);//设置动画的重复次数0为1次Animation.INFINITE为无限重复 imageView.startAnimation(animation); 

4.RotateAnimation

XML资源文件方式:

1).在res/anim文件中创建translateanim.xml文件(如果没有anim文件夹的话需要自己新建)
2).在translateanim.xml中配置动画的属性
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" //一次动画的持续时间 android:startOffset ="1000" //开始动画之前的时间间隔 android:fillEnabled= "true"//是否允许动画结束之后回到开始的状态默认为true android:fillBefore = "true" //动画结束后是否回到初始状态fillEnabled为true时此属性生效,默认为true android:fillAfter = "false"//动画结束后是否保持结束的状态 android:repeatMode= "restart"//重复播放的模式,restart代表正序重放,reverse代表倒序回放 android:repeatCount = "0"//重复的次数为infinite或-1时无限重复 android:interpolator ="@android:anim/linear_interpolator"//插值器,改变动画速率 android:fromDegrees="0" //开始状态的旋转角度 android:toDegrees="360" //结束时的旋转角度 android:pivotY="50%" //旋转中心的Y轴坐标,相对于自身 android:pivotX="50%" //旋转中心的X轴坐标,相对于自身 > </translate> 
3).在MainActivity中
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotateanim); imageView.startAnimation(animation); 
4).运行效果
img_721da174446a084d8f6f28c71e80e249.gif
image

代码方式:(运行效果与XML方式一致)

 animation=new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); // fromDegrees:开始时的角度 // toDegress:结束时角度 //// Animation.RELATIVE_TO_SELF:相对于自身 //// Animation.RELATIVE_TO_PARENT:相对于父控件 //// Animation.ABSOLUTE:相对于窗口0 //// pivotXValue:缩放中心X轴的坐标百分百比(float类型) //// pivotYValue;缩放中心Y轴的坐标百分百比(float类型) animation.setDuration(2000);//设置单次动画的重复时间 animation.setFillAfter(false);//设置动画结束后是否保持结束时的状态,默认为false animation.setFillEnabled(true);//设置是否允许动画结束后保持开始时状态默认为true animation.setFillBefore(true);//设置动画结束时是否保持开始时的状态,默认为true animation.setInterpolator(new LinearInterpolator());//设置动画的插值器 animation.setRepeatMode(Animation.RESTART);//设置动画的重复模式默认为Animation.RESTART animation.setRepeatCount(0);//设置动画的重复次数0为1次Animation.INFINITE为无限重复 imageView.startAnimation(animation);//开始动画 

5.动画集合(即各种动画效果的叠加)

XML资源文件方式:

1).在res/anim文件中创建anmiset.xml文件(如果没有anim文件夹的话需要自己新建)
2).在translateanim.xml中配置动画的属性
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:startOffset ="1000" android:fillBefore = "true" android:fillAfter = "false" android:fillEnabled= "true" android:repeatMode= "restart" android:interpolator ="@android:anim/linear_interpolator" android:repeatCount = "0"> <alpha android:duration="2000" android:fromAlpha="1" android:toAlpha="0"> </alpha> <rotate android:duration="2000" android:fromDegrees="0" android:toDegrees="360" android:pivotY="50%" android:pivotX="50%" > </rotate> <scale android:duration="2000" android:fromXScale="1.0" android:toXScale="2.0" android:fromYScale="1.0" android:toYScale="2.0" android:pivotX="50%" android:pivotY="50%" > </scale> <translate android:duration="2000" android:fromXDelta="0" android:toXDelta="500" android:fromYDelta="0" android:toYDelta="500" > </translate> </set> 
3).在MainActivity中
animation = AnimationUtils.loadAnimation(this, R.anim.anmiset); imageView.startAnimation(animation); 
4).运行效果
img_15c43656e0117ddef1fe27bc38be173c.gif
image

代码方式:(运行效果与XML方式一致)

AnimationSet animationSet=new AnimationSet(true); animationSet.addAnimation(new AlphaAnimation(1,0)); animationSet.addAnimation(new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f)); animationSet.addAnimation(new ScaleAnimation(1,2,1,2,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f)); animationSet.addAnimation(new TranslateAnimation(0,500,0,500)); animationSet.setDuration(2000);//设置单次动画的重复时间 animationSet.setFillAfter(false);//设置动画结束后是否保持结束时的状态,默认为false animationSet.setFillEnabled(true);//设置是否允许动画结束后保持开始时状态默认为true animationSet.setFillBefore(true);//设置动画结束时是否保持开始时的状态,默认为true animationSet.setInterpolator(new LinearInterpolator());//设置动画的插值器 animationSet.setRepeatMode(Animation.RESTART);//设置动画的重复模式默认为Animation.RESTART animationSet.setRepeatCount(0);//设置动画的重复次数0为1次Animation.INFINITE为无限重复 imageView.startAnimation(animationSet); 

个人博客:https://myml666.github.io

原文链接:https://yq.aliyun.com/articles/665570
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章