Android动画之补间动画详解
一.概念
补间动画是指开发者无需定义动画过程中的每一帧,只需要定义动画的开始和结束两个关键帧,并指定动画变化的时间和方式等,然后交由Android系统进行计算,通过在这两个关键帧之间插入渐变值来实现平滑过渡,从而对View的内容完成一系列的图形变换来实现动画效果,主要包括四种基本效果:透明变化Alpha、大小变化Scale、位移变化Translate、以及旋转变化Route,这四种效果可以动态组合,从而实现复杂灵活的动画。同样,定义补间动画也可以分为XML资源文件和代码两种方式。
二.Android中动画的实现类介绍
Android系统中使用Animation类表示抽象的动画类,上面介绍的四种补间动画分别对应着如下几个动画类。
三.动画的实现
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).运行效果
代码方式:(运行效果与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).运行效果
代码方式:(运行效果与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).运行效果
代码方式:(运行效果与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).运行效果
代码方式:(运行效果与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).运行效果
代码方式:(运行效果与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
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android tabLayout+recyclerView实现锚点定位
原文链接:https://mp.weixin.qq.com/s/L3o2i3WTmg1ScXEYDS8YCg 在上一篇文章 [Android 实现锚点定位](https://mp.weixin.qq.com/s/EYyTBtM9qCdmB9nlDEF-3w)中,我们介绍了tablayout+scrollView实现的锚点定位,今天我们使用tablayout+recyclerView 来实现同样的效果。 效果图: 实现思路 实现的思路与上一篇文章是一致的: 1、监听recyclerView滑动到的位置,tablayout切换到对应标签 2、tablayout各标签点击,recyclerView可滑动到对应区域 数据模拟 数据模拟,使用上一文章的AnchorView作为recyclerView的每个字view,同时这里对recyclerView的最后一个子view的高度进行修改,让其充满屏幕。 private LinearLayoutManager manager; private String[] tabTxt = {"客厅", "卧室", "餐厅", "书房", "阳台", "儿童房"...
- 下一篇
Android使用CardView多出的角,怎么办?
某天一个同事问我,为什么他使用CardView会变成这样? 我看了他的代码 注意一行 app:cardbackgroundColor="@color/transparent" 原因就是透明色 去掉之后就正常了
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Red5直播服务器,属于Java语言的直播服务器
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- Hadoop3单机部署,实现最简伪集群
- CentOS关闭SELinux安全模块