|
补间(Tween)动画
Android 使用Animation代抽象的动画类,它包括如下几个子类。
Alpha动画3360更改透明度动画。创建动画时,请指定动画开始时的透明度、结束时的透明度和动画持续时间。其中透明度可以从0更改为1。缩放动画:缩放大小动画。创建动画时,请在动画开始时指定比例(用x轴、y轴的比例参数表示)、结束时的比例(用x轴、y轴的比例参数表示)和动画持续时间。缩放时,由于缩放效果以其他点为中心不同,因此在指定动画缩放时,还必须通过pivotX、pivotY指定缩放中心坐标。转换动画:变更位移动画。创建动画时,只需指定动画开始的位置(用x,y坐标表示)、结束的位置(用x,y坐标表示)和动画持续时间。RotateAnimation:旋转动画。创建动画时,只需指定动画开始时的旋转角度、结束时的旋转角度和动画持续时间。旋转时,以其他点为中心的旋转效果不同,因此在指定旋转动画时,还必须通过pivotX、pivotY指定旋转轴的坐标。当您为移动补间动画指定三个必要资讯时,Android会根据动画的开始影格、结束影格和动画持续时间,计算中间需要「进刀」的影格数目,并计算所有进刀影格的图形。当用户浏览补间动画时,他的眼睛仍然显示“逐帧动画”。为了控制动画期间需要动态“补入”的帧数,需要Interpolator Interpolator,具体取决于动画运行的时间
Interpolator根据特定算法计算整个动画所需的动态插入帧的密度和位置。简而言之,aterpolator控制动画的变化速度,因此基本动画效果(AIpha、Scale、Translate Roete)可以以恒定的速度变化、加速、减速、抛物线速度等多种速度。
Interpolator是定义所有Interpolator必须实现的方法(floatgetinterpolation(float imput))的接口。开发人员可以实现Interpolator,完全控制动画的更改速度。
Android为Interpolator提供了以下实现类,用于实现不同的动画更改速度:
LinearInterpolator:动画以恒定的速度变化。AccelerateInterpolator:在动画开始时变化缓慢,然后开始加速。acceleratedecelerateinterpolator :在动画开始和结束的地方更改速度减慢,中间加速。CycleInterpolator:动画会重复特定的次数,变更速度会根据正弦曲线变更。DecelerateInterpolator:在动画开始时迅速变化,然后开始减速。为了在移动资源文件中指定用于补间动画的Interpolator,请为补间动画设置set ./.定义/元素支援
android:interpolator属性,可以将属性值指定为Android默认支持的Interpolator。例如:
@ Android 3360 Anim/linear _ interpolator
@ android:anim
/accelerate_interpolator
@android:anim/accelerate_decelerate_interpolator
……
其实上面的写法很有规律,它们就是把系统提供的Interpolator实现类的类名的驼峰写法改为下画线写法即可。
一旦在程序中通过AnimationUtils 得到了代表补间动画的Animation 之后,接下来就可调用View的startAnimation(Animation anim)方法开始对该View执行动画了。
位移(translate)、旋转(rotate)、缩放(scale)、透明度(alpha)改变的补间动画
set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator" >
translate
android:duration="500"
android:fromYDelta="100%"
android:toYDelta="0%" />
rotate
android:duration="5000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="720" />
scale
android:duration="1000"
android:fillBefore="true"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%p"
android:pivotY="50%p"
android:toXScale="2.0"
android:toYScale="2.0" />
rotate
android:duration="1000"
android:fromDegrees="0.5"
android:toDegrees="1" />
set>
setCustomAnimations方法
setCustomAnimations (int enter, int exit, int popEnter, int popExit),该方法有4个参数:
[ol]第一个参数enter是指当一个Fragment被添加added 或者绑定attached到视图上,该Fragment进入视图时的animation或者animator的资源ID;第二个参数exit是指当一个Fragment从视图上被移除removed或者解除绑定detached时,该Fragment移出视图时的animation或者animator的资源ID;第三个参数popEnter是指当调用popBackStack()方法或者类似的方法弹出栈顶的Fragment后,Fragment栈的栈顶Fragment重新被添加readded或重新绑定reattached到视图上时,该Fragment进入视图时的animation或者animator的资源ID;第四个参数popExit是指当调用popBackStack()方法或者类似的方法弹出栈顶的Fragment时,该弹出的Fragment即从视图上被移除removed或者解除绑定detached,该Fragment移出视图时的animation或者animator的资源ID。[/ol]
getActivity().getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.comment_down_animation,R.anim.comment_down_animation)
.hide(feedCommentFragment).commitAllowingStateLoss();
getChildFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.comment_fullscreen_anim_enter, R.anim.comment_fullscreen_anim_out)
.show(commentFragment).commitAllowingStateLoss();
tips:setCustomAnimations方法必须成双出现,要么写两个要么写四个。上面部分代码表示在Activity、Fragment中加载新的Fragment的动画。 |
|