Activity 转场动画 有一个函数: overridePendingTransition(R.anim.in_translate_top, R.anim.out_translate_top); 第一个参数为进场动画,第二个是出场. Use 0 for no animation. 此外,必须紧跟在startActivity(Intent) or
finish()后面调用
不过 更多使用 theme 的方法
<!-- 窗口进入和退出动画都有 -->
<style name="ActivityInOutAnimation" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/push_left_in</item>
<item name="android:activityOpenExitAnimation">@anim/push_left_out</item>
<item name="android:activityCloseEnterAnimation">@anim/push_right_in</item>
<item name="android:activityCloseExitAnimation">@anim/push_right_out</item>
</style>
open 系列的 item 是 A-->B A的退场动画 和 B的 进场动画 进场都是 enter
close 系列的 item 是 B-->back A的进场动画 和 B的 退场动画 退场都是 exit
view 动画 xml 的编辑文件存放于 res/drawable/anim
public abstract class
Animation
extends Object
implements Cloneable
java.lang.Object
↳ android.view.animation.Animation
Known Direct Subclasses
<strong>AlphaAnimation, AnimationSet, RotateAnimation, ScaleAnimation, TranslateAnimation </strong>
由 view.animation 的继承结构可以看出 一共有 4种动画 : 透明度 的变化, 旋转动画, 缩放 动画 , 位移动画 . AnimationSet是 动画的组合
view.animation一般用 xml文件 配置:
Alpha: 父类animation的属性
From classandroid.view.animation.Animation
Attribute Name Related Method Description android:detachWallpaper setDetachWallpaper(boolean) Special option for window animations: if this window is on top of a wallpaper, don't animate the wallpaper with it. 在壁纸上运行 android:duration setDuration(long) Amount of time (in milliseconds) for the animation to run. 动画持续时间,毫秒为单位 android:fillAfter setFillAfter(boolean) When set to true, the animation transformation is applied after the animation is over. 控件动画结束时是否保持动画最后的状态
默认值是 falseandroid:fillBefore setFillBefore(boolean) When set to true or when fillEnabled is not set to true, the animation transformation is applied before the animation has started.
默认值是 trueandroid:fillEnabled setFillEnabled(boolean) When set to true, the value of fillBefore is taken into account. android:interpolator setInterpolator(Interpolator) Defines the interpolator used to smooth the animation movement in time. 插值器 android:repeatCount setRepeatCount(int) Defines how many times the animation should repeat. 重复次数 (结果是 n+1 次)
设置此值 可以 重复无限次
Constant Value Description infinite
-1
android:repeatMode setRepeatMode(int) Defines the animation behavior when it reaches the end and the repeat count is greater than 0 or infinite.
Constant Value Description restart
1 The animation starts again from the beginning. reverse
2 The animation plays backward.
android:startOffset setStartOffset(long) Delay in milliseconds before the animation runs, once start time is reached. android:zAdjustment setZAdjustment(int) Allows for an adjustment of the Z ordering of the content being animated for the duration of the animation.
android:fillAfter android:fillBefore android:fillEnabled (注意只能在该xml文件的 root tag上写这些属性,否则不会生效) 进行的测试结果 :
filEnable fillBefore fillAfter 结果 结论 true true true 最后一帧
false true 最后一帧
false false 第一帧
true false 第一帧
false true true 最后一帧
false true 最后一帧
false false 第一帧
true false 第一帧
表一:SET属性
名称 属性 备注 android:shareInterpolator 是否共享插入器 共享时,四个子节点都用一个插入器 android:interpolator 指定一个动画的插入器 使用系统资源 android:fillEnabled
当设置为true时,fillAfter和fillBefroe将会都为true,此时会忽略fillBefore 和fillAfter两种属性 android:fillAfter 该动画转化是否在动画结束后被应用 boolean android:fillBefore 该动画转化是否在动画开始前被应用 boolean android:repeatMode 重复模式 "restart" 或者 "reverse" android:repeatCount 重复次数 integer android:duration 动画持续时间 integer android:startOffset 动画时间间隔 long android:zAdjustment 定义动画z order的变换 [normal] or [top] or [bottom] android:detachWallpaper 未知 boolean
表二:alpha 名称 属性 备注 android:toAlpha 动画结束时的透明度 float [0,1] 0表示完全透明 1表示完全不透明 android:duration 动画持续时间 integer android:fromAlpha 动画开始时的透明度 float [0,1] 0表示完全透明 1表示完全不透明
表三:SCALE 名称 属性 备注 android:fromXScale 动画开始时,X坐标上的伸缩尺寸 float,0.0表示收缩到没有、1.0表示正常无伸缩,小于1表示收缩,大于1表示放大 android:toXScale 动画结束时,X坐标上的伸缩尺寸 同上 android:fromYScale 动画开始时,Y坐标上的伸缩尺寸 同上 android:toYScale 动画结束时,Y坐标上的伸缩尺寸 同上 android:pivotX 动画相对于物件的X的开始位置 [0%,100%],50%表示中间 android:pivotY 动画相对于物件的Y的开始位置 0%,100%],50%表示中间 android:duration 同set 同set
表四:TRANSLATE 名称 属性 备注 android:fromXDelta 动画开始时,X坐标上的位置 integer 默认以自己为参照物 android:toXDelta 动画结束时,X坐标上的位置 integer 默认以自己为参照物 android:fromYDelta 动画开始时,Y坐标上的位置 integer 默认以自己为参照物 android:toYDelta 动画结束时,Y坐标上的位置 integer 默认以自己为参照物
表五:ROTATE 名称 属性 备注 android:fromDegrees 动画开始时,物件的角度 负数:逆时针旋转,正数:顺时针旋转 android:toDegrees 动画结束时,物件的角度(可大于360度) 同上 android:pivotX 动画相对于物件的X的开始位置 [0%,100%],50%表示中间 android:pivotY 动画相对于物件的Y的开始位置 [0%,100%],50%表示中间
---注意---android:pivotX的值共有三种设置方法:
1. android:pivotX="50"这种方法使用自身定位 (控件左上角 为坐标原点)
2. android:pivotX="50%"这种方法相对于控件本身定位 ( 100% 代表 控件的 一个 宽度)
3. android:pivotX="50%p"这种方法相对于控件的父控件定位
android:fromXDelta android:toXDelta 也是 这个 用法 .
以
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) Constructor to use when building a TranslateAnimation from code 为例
xValue 和 xType 其实就是 xXDelta 的分开写法 xtype的 取值 有 3个常量 RELATIVE_TO_SELF, RELATIVE_TO_PARENT,ABSOLUTE
踩用 相对 形式时 xValue 的取值 以 小数形式写 : 1.0-->100%; 绝对模式下直接写数值
补间动画 的 用法 : 在代码中直接 new 一个然后设置状态, 或者用 AnimationUtils.loadAnimation 加载一个xml动画