Android开发艺术探索-动画深入研究View动画
来源:互联网 发布:华资软件怎么样 编辑:程序博客网 时间:2024/04/27 15:11
- view动画
- view动画的监听事件
- view动画的特殊使用场景
view动画
View Animation也就是补间动画。View Animation分为4大类:AlphaAnimation,RotateAnimation,ScaleAnimation,TranslateAnimation,分别对应透明度,旋转,大小,位移四种变化。继承Animation。
View Animation的效果由四个因素决定:1初始状态;2结束状态;3持续时间;4Interpolator。所以要定义一个View Animation,你只要定义以上四个因素,中间的过程怎么变化则有系统自动计算出来。其中前3个因素很容易理解,第四个因素Interpolator比较特别,Interpolator能够影响动画的速度。
这四种动画既可以通过代码动态传家,也可以通过xml来定义,对于View动画来说,建议用xml定义,因为xml格式的动画可读性更好,分别使用xml文件定义AlphaAnimation,RotateAnimation,ScaleAnimation,TranslateAnimation这四种动画:
AlphaAnimation:
<?xml version="1.0" encoding="utf-8"?><!-- * android:interpolator:修饰动画效果,定义动画的变化率,可以使存在的动画效果accelerated(加速), * decelerated(减速),repeated(重复),bounced(弹跳)等。 * android:fromAlpha="0.0":设置动画开始时控件的透明度,0.0为透明,控件不显示,1.0为不透明,控件全部显示 * android:toAlpha="1.0":设置动画结束时的控件的透明度 * android:duration="2500":设置每一次动画持续的时间值 * android:repeatCount="5":设置动画重复的次数 * android:repeatMode="reverse":设置动画重复的模式,reverse为0.0 -> 1.0 -> 0.0,动画反复执行; * restart为0.0 -> 1.0, 0.0 -> 1.0,动画每次都重新开始执行 --><alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2500" android:repeatCount="5" android:repeatMode="reverse" />
RotateAnimation:
<!-- * android:fromDegrees="-50":动画开始执行时的控件起始状态的角度; * android:toDegrees="360":动画结束执行时的控件结束状态的角度; * android:pivotX="50%":旋转动画的中心点的X轴方向的横坐标,Value加%时,意思是相对于控件的原始位置往右方向的50%控件宽度的位置为横坐标, * 若Value为负值(例如-50%),则表示往左方向;当Value为数值时,则代表相对于屏幕的(px)像素值; * android:pivotY="50%":旋转动画的中心店的Y轴方向的纵坐标,原理同上; * android:startOffset="1000":动画执行前的延迟时间; * android:fillAfter="true":当Value为true,表示动画执行结束后停留在结束后的状态; * android:duration="2500":设置每一次动画持续的时间值,单位为毫秒(ms); * android:repeatCount="5":设置动画重复的次数; * android:repeatMode="reverse":设置动画重复的模式: * reverse为0.0 -> 1.0 -> 0.0,动画反复执行; * restart为0.0 -> 1.0, 0.0 -> 1.0,动画每次都重新开始执行 --><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="520" android:toDegrees="3600" android:pivotX="50%" android:pivotY="50%" android:startOffset="1000" android:fillAfter="true" android:duration="2500" android:repeatCount="4" android:repeatMode="reverse" />
ScaleAnimation:
<?xml version="1.0" encoding="utf-8"?><!-- * android:fromXScale="0.0":设置动画开始时X轴方向的缩放起始值。0.0为X轴方向的控件缩成一点,1.0为X轴方向的控件不缩放 * android:toXScale="1.0":设置动画结束时X轴方向的缩放结束值。 * android:fromYScale="0.5":设置动画开始时Y轴方向的缩放起始值。0.0为Y轴方向的控件缩成一点,1.0为Y轴方向的控件不缩放 * android:toYScale="1.0":设置动画结束时Y轴方向的缩放结束值 * android:pivotX="100%":动画相对于物件的X坐标的开始位置,当value加了百分号%时(例如80%),则实现相对于控件本身 * 的位置而定的,当value是数值时(不加%号),则是指相对于父控件的位置而定的,单位是px。 * android:pivotY="80%":动画相对于物件的Y坐标的开始位置,当value加了百分号%时(例如80%),则实现相对于控件本身 * 的位置而定的,当value是数值时(不加%号),则是指相对于父控件的位置而定的,单位是px。 * android:duration="2500":设置每一次动画持续的时间值,单位为毫秒(ms) * android:repeatCount="5":设置动画重复的次数 * android:repeatMode="reverse":设置动画重复的模式: * reverse为0.0 -> 1.0 -> 0.0,动画反复执行; * restart为0.0 -> 1.0, 0.0 -> 1.0,动画每次都重新开始执行 --><scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.5" android:toYScale="1.0" android:pivotX="100%" android:pivotY="80%" android:duration="2500" android:repeatCount="5" android:repeatMode="reverse" /><?xml version="1.0" encoding="utf-8"?><!-- * android:fromXScale="0.0":设置动画开始时X轴方向的缩放起始值。0.0为X轴方向的控件缩成一点,1.0为X轴方向的控件不缩放 * android:toXScale="1.0":设置动画结束时X轴方向的缩放结束值。 * android:fromYScale="0.5":设置动画开始时Y轴方向的缩放起始值。0.0为Y轴方向的控件缩成一点,1.0为Y轴方向的控件不缩放 * android:toYScale="1.0":设置动画结束时Y轴方向的缩放结束值 * android:pivotX="100%":动画相对于物件的X坐标的开始位置,当value加了百分号%时(例如80%),则实现相对于控件本身 * 的位置而定的,当value是数值时(不加%号),则是指相对于父控件的位置而定的,单位是px。 * android:pivotY="80%":动画相对于物件的Y坐标的开始位置,当value加了百分号%时(例如80%),则实现相对于控件本身 * 的位置而定的,当value是数值时(不加%号),则是指相对于父控件的位置而定的,单位是px。 * android:duration="2500":设置每一次动画持续的时间值,单位为毫秒(ms) * android:repeatCount="5":设置动画重复的次数 * android:repeatMode="reverse":设置动画重复的模式: * reverse为0.0 -> 1.0 -> 0.0,动画反复执行; * restart为0.0 -> 1.0, 0.0 -> 1.0,动画每次都重新开始执行 --><scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.5" android:toYScale="1.0" android:pivotX="100%" android:pivotY="80%" android:duration="2500" android:repeatCount="5" android:repeatMode="reverse" />
TranslateAnimation:
<?xml version="1.0" encoding="utf-8"?><!-- * android:interpolator:修饰动画效果,定义动画的变化率,可以使存在的动画效果accelerated(加速), decelerated(减速),repeated(重复),bounced(弹跳)等。 * android:fromXDelta="0%"动画开始执行时X轴方向控件的起始位置,当value加%时(例如80%),意思是相对于控件的原始位置往右的80%控件宽度的位置 * 又例如value为-20%时,意思是相对于控件的原始位置往左的20%控件宽度的位置。当Value不加%时(例如80),表示相对于 * 控件的原始位置往右80个像素(px)的位置,又如当Value为-20时,即往左20个像素。 * android:toXDelta="100%"动画结束执行时X轴方向控件的结束位置,Value可以加%,也可以不加%,原理同上 * android:fromYDelta="0%"动画开始执行时Y轴方向控件的起始位置,Value可以加%,也可以不加%,原理同上 * android:toYDelta="100%"动画结束执行时Y轴方向控件的结束位置,Value可以加%,也可以不加%,原理同上 * android:duration="2500"设置每一次动画持续的时间值 * android:repeatMode="reverse"设置动画重复的模式,reverse为0% -> 100% -> 0%,动画反复执行; * restart为0% -> 100%, 0% -> 100%,动画每次都重新开始执行 * android:repeatCount="1"设置动画重复的次数 * android:startOffset="1000":动画执行前的延迟时间 * android:fillEnabled="false"当设置为true时,fillAfter和fillBefroe将会都为true,此时会忽略fillBefore和fillAfter两种属性 * android:fillAfter="true":当Value为true,表示动画执行结束后停留在结束后的状态 * android:fillBefore="false":当Value为false:表示动画执行结束后回到动画开始时的状态 --><translate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/bounce_interpolator" android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0%" android:toYDelta="100%" android:duration="2500" android:repeatMode="reverse" android:repeatCount="1" android:startOffset="1000" android:fillEnabled="false" android:fillAfter="true" android:fillBefore="true" />
View动画还可以通过xml的set标签对应AnimationSet,AnimationSet能够将四种动画添加到一起,而且AnimationSet只能把所有的view动画一起播放,如:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true" android:duration="1000" android:repeatMode="reverse" android:repeatCount="2" > <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:fillAfter="true" android:duration="1000" android:repeatCount="1" android:repeatMode="reverse" /> <scale android:fromXScale="1.0" android:toXScale="0.0" android:fromYScale="1.0" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" android:duration="1000" android:repeatCount="1" android:repeatMode="reverse" /> <translate android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0%" android:toYDelta="100%" android:fillAfter="true" android:duration="1000" android:repeatCount="1" android:repeatMode="reverse" /> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" android:duration="1000" android:repeatCount="1" android:repeatMode="reverse" /></set>
定义好view动画的xml文件就可以通过Animation animation = AnimationUtils.loadAnimation(context, R.anim.xx);
获取Animation对象,然后通过view的startAnimation()开启播放动画如:
Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation_alpha); mAlphaAnimation.startAnimation(animation);
使用view动画的三个步骤:
1通过xml设置动画
2通过 AnimationUtils.loadAnimation(context, R.anim.xx)加载动画
3view通过startAnimation()开始播放动画
缺点:Animation 是针对视图外观的动画实现,动画被应用时外观改变但视图的触发点不会发生变化,还是在原来定义的位置。
监听事件
通过Animation的setAnimationListene()方法可以给View动画添加过程监听,接口入如下:
public interface AnimationListener { void onAnimationStart(Animation var1); void onAnimationEnd(Animation var1); void onAnimationRepeat(Animation var1); }
使用比较简单:
translateAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } });
view动画的特殊使用场景之LayoutAnimation
view动画可以控制子元素在ViewGroup中的出场效果,为ViewGroup指定一个动画,这样它的子元素出场时都会具有这种动画效果,这种效果通常被用在ListView,Recycelrview上面,时常会看到一种特殊的RecycelerView,它的每个item都有一定的动画实现,其实这并非什么高深的技术,它使用的就是LayoutAnimation。
使用步骤:
1. 定义LayoutAnimation
<?xml version="1.0" encoding="utf-8"?><!-- android:delay 子类动画时间间隔 (延迟) 70% 也可以是一个浮点数 如“1.2”等 android:animationOrder="random" 子类的显示方式 random表示随机 android:animationOrder 的取值有 normal 0 默认 reverse 1 倒序 random 2 随机 android:animation="@anim/slide_right" 表示孩子显示时的具体动画是什么--><layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:animation="@anim/animation_translate" android:animationOrder="normal" android:delay="30%"/>
- 为子元素指定具体的入场动画
可以是Animation动画,缩放,旋转,透明度,平移,以及集合,
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true" > <alpha android:fromAlpha="1.0" android:toAlpha="0" /> <translate android:fromYDelta="0" android:toYDelta="500" /></set>
- 为viewGroup指定android:layoutAnimaion属性
<android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rv" android:layoutAnimation="@anim/animation_layout_animation" />
当然也可以通过设置,如:
layoutAnimationController = new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.animation_layout_animation)); layoutAnimationController.setDelay(0.5f); layoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL); mRv.setLayoutAnimation(layoutAnimationController);
view动画的特殊使用场景之Activity切换效果
Activity有默认的切换效果,但是这个效果我们可以自定义的,主要用到overridePendingTransition(int enterAnim,int exitAnim),这个方法必须在startActivity(intent),或者finish()之后使用。
int enterAnim:activity被打开时所需要的动画id
int exitAnim:activity暂停时所需要的动画id
/*** * 启动activity的时候,按照自定义的切换效果 */ startActivity(new Intent(MainActivity.this,LayoutAnimationActivity.class)); overridePendingTransition(R.anim.enter_anim,R.anim.exit_anim);
/** * 退出的时候 */ @Override public void finish() { super.finish(); overridePendingTransition(R.anim.enter_anim,R.anim.exit_anim); }
- Android开发艺术探索-动画深入研究View动画
- Android 动画深入分析(Android开发艺术探索读书笔记)
- 《android开发艺术探索笔记》Part7、Android动画深入分析
- Android开发艺术探索__android动画深入分析(七)
- 开发艺术探索 -- 动画深入分析
- android 动画深入分析----属性动画(二)《android开发艺术与探索》
- 【读书笔记】【Android 开发艺术探索】第 7 章 Android 动画深入分析
- 《Android开发艺术探索》读书笔记 (7) 第7章 Android动画深入分析
- android 动画深入分析(一)《android开发艺术与探索》
- Android 开发艺术探索笔记 第七章 Android动画深入分析
- Android开发艺术探索——第七章:Android动画深入分析
- 第七章Andorid动画深入分析(Android开发艺术探索)
- Android动画(Android开发艺术探索笔记)
- Android开发艺术探索读书笔记(第7章 动画)
- android 艺术开发探索之动画(animation)
- Android开发艺术-第七章Android动画深入分析读书笔记
- 《Andorid开发艺术探索第七章》-动画
- 《Android开发艺术探索》 自定义View
- Java8 Stream API介绍
- 网络唤醒原理浅析(Wake On LAN)
- UE 运行ruby 设置
- select into from 和 insert into select 区别
- bzoj 5029: 贴小广告
- Android开发艺术探索-动画深入研究View动画
- Docker简介
- MIPI DSI协议介绍
- ES学习和使用笔记之Near Real-Time Search和数据可靠性保证机制
- 网络虚拟化技术(一): Linux网络虚拟化
- iOS App审核上线
- ADF 表格删除获取不到当前行
- 手写日志
- 技术文章 | 智能语音交互:阿里的研究和实践