一、 Animation代码生成说明:
Animation是个抽象类。有5个子类AlphaAnimation, AnimationSet, RotateAnimation, ScaleAnimation, TranslateAnimation分别实现了平移、旋转、缩放、Alpha变换、和以上动画的组合变换动画。
动画类主要作用于View
主要方法:
initialize(int width, int height, int parentWidth,
int parentHeight);
初始化动画的尺寸。
width, heigth动画自身的宽、高
parentWidth, parentHeight为动画组件父类的宽高
void applyTransformation(float interpolatedTime,
Transformation t);
该方法指定了动画每一帧的变换效果。
interpolatedTime: 动画执行的进度。即Interpolator计算出的结果。是一个大于等于0、小于等于1的浮点数。
Transformation t: 为每一帧变化的载体。主要有两个属性。Alpha和Matrix
1、 AlhpaAnimation
继承自Animation,实现了简单的旋转动画。
构造函数:
·AlphaAnimation(Context context, AttributeSet attrs);
从XML中构建AlhpaAnimation对象。
·AlphaAnimation(float fromAlpha, float toAlpha);
fromAlpha:开始时的Alpha值。取值范围为0—1;
toAlpha:结束时的Alpha值。取值范围为0-1;
2、 RotateAnimation
继承自Animation,实现了简单的旋转动画。
构造函数:
·RotateAnimation(Context context, AttributeSet attrs);
从XML中构建AlhpaAnimation对象。
·RotateAnimation(float fromDegrees, float toDegrees);
fromDegrees:开始时的角度。
toDegrees:结束时的角度。
注:轴心点坐标pivotX, pivotY默认为(0,0)即左上角。
·RotateAnimation(float fromDegrees, float toDegrees,
float pivotX, float pivotY);
pivotX: 围绕旋转的轴心点X坐标。
pivotY: 围绕旋转的轴心点Y坐标。
注:以上两个参数取值为绝对坐标。
·RotateAnimation(float fromDegrees, float toDegrees,
int pivotXType, float pivotXValue,
int pivotYType, float pivotYValue);
pivotXType, pivotYType:分别指定了pivotXValue, pivotYValue的表示类
型。取值范围为:
Animation.ABSOLUTE:指定了其表示的类型为绝对坐标。
Animation.RELATIVE_TO_SELF:指定了其表示类型为相对于自身的坐标。 1.0即表示100%
Animation.RELATIVE_TO_PARENT:指定了其表示类型为相对于父控件的坐标。 1.0即表示100%p
3、 ScaleAnimation
继承自Animation,实现了简单的缩放动画。
构造方法:
·ScaleAnimation(Context context, AttributeSet attrs);
从XML中构建ScaleAnimation对象
ScaleAnimation(float fromX, float toX, float fromY,
float toY);
fromX:为开始时的横向缩放比例。
toX:为结束时横向的缩放比例。
fromY:为开始时纵向的缩放比例。
toY: 为结束时纵向的缩放比例。
注:为缩放轴心点为0,0即左上角。
·ScaleAnimation(float fromX, float toX, float fromY,
float toY, float pivotX, float pivotY);
pivotX, pivotY缩放的轴心点。
·ScaleAnimation(float fromX, float toX, float fromY, float toY,
int pivotXType, float pivotXValue,
int pivotYType, float pivotYValue);
pivotXType:轴心点x轴相对于组件的位置类型。
pivotXValue:轴心点x轴的坐标。根据pivotXType代表不懂的含义。
pivotYType:轴心点y轴相对于组件的位置类型。
pivotYValue:轴心点y轴的坐标。
4、 TranslateAnimation
继承自Animation,实现了简单的平移动画。
构造方法:
·TranslateAnimation(Context context, AttributeSet atrrs);
从XML中构建TranslateAnimation
·TranslateAnimation(float fromXDelta, float toXDelta,
float fromYDelta, float toYDelta);
fromXDelta, fromYDelta分别为开始时的X, Y坐标。
toXDelta, toYDelta分别为结束时的X, Y坐标。
TranslateAnimation(int fromXType, float fromXValue,
int toXType, float toXValue,
int fromYType, float fromYValue,
int toYType, float toYValue);
fromXType:开始时x轴相对于组件的位置类型。
fromXValue:开始时x轴的坐标。根据fromXType代表不同的意义。
toXType:结束时x轴相对于组件的位置类型。
toXValue:结束时x轴的坐标。根据toXType代表不同的意义。
fromYType:开始时y轴相对于组件的位置类型。
fromYValue:开始时y轴的坐标。根据fromYType代表不同的意义。
toYType:结束时y轴相对于组件的位置类型。
toYValue:结束时y轴的坐标。根据toYType代表不同的意义。
5、 AnimationSet
该类继承自Animation,在实现了Animation的基础上将各种动画效果合并在一起。
构造方法:
·AnimationSet(Context context, AttributeSet attrs);
从XML中构建AnimationSet
·AnimationSet(boolean shareInterpolator);
参数指定了整合后的各种动画是否要使用同样的Interpolator;
void addAnimtaion(Animation animation);
将设置好的动画加入到AnimtaionSet中。
二、 Interpolator接口
该接口主要是来对动画播放的时间进行控制。Animation在播放每一帧是会计算出当的进度,即当前已播放的百分比。然后将该值交给Interpolator进行一次处理,由Interpolator控制播放速度。
接口方法:
float getInterpolation (float input);
传入当前播放的百分比。0为开始,1为结束。
实现该接口的类有9个。用以实现不同的播放速度。比如先快后慢、先慢后快等。
例如:
LinearInterpolator:实现动画的线性比方(匀速播放)
AccelerateInterpolator:实现动画的加速播放。该类有一个参数factor为加
速因子。越打加速效果越明显。
DecelerateInterpolator:实现了减速播放的效果。同样该类也有一个减速因子factor,值越大减速效果越明显。
AccelerateDecelerateInterpolator:实现了开始和结束时较慢。中间阶段会先加速在减速。
当创建了Interpolator示例后可以通过
Animation的setInterpolator(Interpolator i)来设置到动画中。
三、 Animation XML生成说明
各类的XML属性说明:
1、 AlphaAnimation
说明:实现alpha渐变,可以使界面逐渐消失或者逐渐显现
XML标签:
<alpha
android:fromAlpha=float
android:toAlpha=float
/>
属性:
fromeAlpha:开始的alpha值。取值0-1
toAlpha:结束的alpha值。取值0-1
TranslateAnimation
说明:实现位置移动渐变,需要指定移动的开始和结束坐标。其中的属性值支持以下三种格式的:
如果取值为-100到100,并且以%结束,说明是以自身为参照点;如果取值-100到100,并且以%p结束,说明这个百分比是以他父控件为参照点;如果是一个float值并且没有后缀,则是一个绝对的像素数值
XML标签:
<translate
android:fromX=string
android:toX=string
android:fromY=string
android:toY=string
/>
属性:
fromXDelta:开始时的X位置
toXDelta:结束时的X位置
fromYDelta:开始时的Y位置
toYDelta:结束时的Y位置
注:默认是以自己为相对参照物。
2、 ScaleAnimation:
说明:实现缩放渐变,可以指定缩放的参考点。如果将轴心点设置为0, 0则以左上角为参照点,向右下角缩放。
XML标签:
<scale
android:fromXScale=float
android:toXScale=float
android:fromYScale=float
android:toYScale=float
android:pivotX=string
android:pivotY=string
/>
属性:
fromXScale:开始时X的尺寸,1.0表示不改变。
toXScale:结束时Y的尺寸,1.0表示不改变。
fromYScale:开始时X的尺寸,1.0表示不改变。
toYScale:结束时Y的尺寸,1.0表示不改变。
pivotX:轴心点的X坐标。
pivotY:轴心点的Y坐标。
4、RotateAnimation:实现旋转渐变,可以指定旋转的参考点,默认值为(0,0)左上角。
XML标签:
<rotate
android:fromDegrees=float
android:toDegrees=float
android:pivotX=string
android:pivotY=string
/>
属性:
fromDegrees:开始的角度。
toDegrees:结束的角度。
pivotX:轴心点的X坐标。是以像素为单位。0, 0为左上角。
pivotY:中心店的Y坐标。是以像素为单位。0, 0为左上角。
5、AnimationSet:
说明:代表上面的渐变组合。
XML标签:
<set android:shareInterpolator=boolean>
</set>
属性:
shareInterpolator:指定一个Interpolator(动画播放曲线)。包含的所有动画将共享此Interpolator。该值为boolean类型
用法:
<set android:shareInterpolator=boolean>
<translate/>
<alpha/>
<interpolator/>
</set>
6、Interpolator:
说明:定义了一个播放曲线,例如:先快后慢,先慢逐步加快,等等。可以
在R.styleable中定义它的所有子类。例如:<CycleInterpolator>, <EaseInInterpolator>, 和 <EaseOutInterpolator>
7、共有属性:
说明:除了上<alpha>, <scale>, <translate>, <rotate>, 和<set>定义的属性外他们还继承了其父类的共有属性:
duration:动画的持续时间。以毫秒为单位。
starOffset:动画开始时的延迟时间。以毫秒为单位。
fillBefore:如果为true,即在动画开始变换之前即开始。
fillAfter:如果为true,即在动画变化结束后动画结束。
repeatCount:指定了动画的循环次数。
repeatMode:指定了动画的循环模式。其为:restart:动画结束后从头开始播。reverse:动
动画结束后从后往前播。
zAdjustment:指定了动画运行时的z轴模式。其值为:normal, top, or bottom;
interpolator:指定动画的变化曲线。可以引用R.anim里定义好的一些值。引用方式为
@android.R.anim/interpolatorName。
例如:
interpolator=@android:anim/accelerate_interpolator;
8、XML导入方式:
例如:
Animation animation = AinmationUtils.loadAnimation(this,
R.anim.trals);
Animations的分类
1、 Tweened Animations渐变动画、补间动画
提供了旋转、移动、伸展和淡出等效果
2、 Frame-by-Frame Animations
可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示。
Tweened Animations的分类
1、 Alpha:淡入淡出效果
2、 Scale:缩放效果
3、 Rotate:旋转效果
4、 Translate:移动效果
Animations的第一种使用方法:
使用Tweened Animations的步骤
1、 创建一个AnimationSet对象
2、 根据需要创建相应的Animation对象
3、 根据软件动画的需求,为Animation对象设置相应的数据
4、 将Animation对象添加到AnimationSet对象当中
5、 使用控件对象开始执行AnimationSet
Animation的四个子类:
AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation
private class RotateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个RotateAnimation对象
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT, 0.2f);旋转点的x坐标为0.5倍父控件的宽度,y坐标为0.2倍父控件的长度
//设置动画执行的时间(单位:毫秒)
rotateAnimation.setDuration(5000);
//将RotateAnimation对象添加到AnimationSet当中
animationSet.addAnimation(rotateAnimation);
//开始动画
imageView.setAnimation(animationSet);
}
}
private class ScaleButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个ScaleAnimation对象
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
//设置动画执行的时间(单位:毫秒)
scaleAnimation.setDuration(5000);
//将ScaleAnimation对象添加到AnimationSet当中
animationSet.addAnimation(scaleAnimation);
//开始动画
imageView.setAnimation(animationSet);
}
}
private class AlphaButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlphaAnimation对象
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//设置动画执行的时间(单位:毫秒)
alphaAnimation.setDuration(1000);
//将AlphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
//开始动画
imageView.setAnimation(animationSet);
}
}
private class TranslateButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个TranslateAnimation对象
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f);
//设置动画执行的时间(单位:毫秒)
translateAnimation.setDuration(5000);
//将TranslateAnimation对象添加到AnimationSet当中
animationSet.addAnimation(translateAnimation);
//开始动画
imageView.setAnimation(animationSet);
}
}
Tween Animation的通用属性:
1、 setDuration(long durationMills)
设置动画持续时间(单位:毫秒)
2、 setFillAfter(Boolean fillAfter)
如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
3、 setFillBefore(Boolean fillBefore)
如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态
4、 setStartOffSet(long startOffSet)
设置动画执行之前的等待时间
5、 setRepeatCount(int repeatCount)
设置动画重复执行的次数
Animations的第二种使用方法:
1、 在res文件夹下面新建一个名为anim的文件夹
2、 创建xml文件,并首先加入set标签,改标签如下:
<set xmlns:android= "http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/accelerate_interpolator">
</set>
在该标签当中加入rotate, alpha, scale或者translate标签
3、 在代码当中使用AnimationUtils当中装载xml文件,并生成Animation对象
Alpha的xml文件编写方法 alpha.xml
<<alpha android:fromAlpha= "0.1"
android:toAlpha= "0.0"
android:startOffset= "500"
android:duration= "500" />
代码中:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
imageView.startAnimation(animation);
Rotate的xml文件编写方法 rotate.xml
<rotate android:fromDegrees="0"
android:toDegrees="+350"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"/>
android:pivotX的值共有三种设置方法:
1、 android:pivoteX= “50”这种方法使用绝对位置定位
2、 android:pivoteX= “50%”这种方法相对于控件本身定位
3、 android:pivoteX= “50%p”这种方法相对于控件的父控件定位
translate的xml文件编写方法
<translate android:fromXDelta="50%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="2000" />
scale的xml文件编写方法
<scale android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
AnimationSet
1、 AnimationSet是Animation的子类
2、 一个AnimationSet包含了一系列的Animation
3、 针对AnimationSet设置一些Animation的常见属性(如startOffset, duration等等),可以被包含在AnimationSet当中的Animation集成。
往AnimationSet中加入多个动画
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个RotateAnimation对象
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT, 0.2f);旋转点的x坐标为0.5倍父控件的宽度,y坐标为0.2倍父控件的长度
//创建一个AlphaAnimation对象
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//将AlphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
//设置动画执行的时间(单位:毫秒)
animationSet.setDuration(5000);
//将RotateAnimation对象添加到AnimationSet当中
animationSet.addAnimation(rotateAnimation);
//开始动画
imageView.setAnimation(animationSet);
也可以在加载布局文件的时候,布局文件里面定义多个动画效果。
什么是Interpolator
Interpolator定义动画变化的速率,在Animations框架当中定义了以下几种Interpolator:
1、AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候加速
2、AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速
3、CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
4、DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速。
5、LinearInterpolator:在动画的以均匀的速率改变
在xml里面设置:android:interpolator= ….
在代码里设置: (AlphaAnimation、AnimationSet等)animation.setInterpolator(new DecelerateInterpolator());
Frame-By-Frame Animations的使用方法:
在res/drawable当中创建一个xml文件(如anim_nv.xml),用于定义Animations的动画序列:
<animation-list >
<item />
<item />
</animation-list>
代码里:
imageView.setBackgroundResource(R.drawable.anim_nv);
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
animationDrawable.start();
什么是LayoutAnimationController
1、 LayoutAnimationController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果。
2、 每一个控件都有相同的动画效果
3、 这些控件的动画效果在不同的时间显示出来
4、 LayoutAnimationController可以在xml文件当中设置,也可以在代码当中进行设置。
在xml当中使用LayoutAnimationController
1、 在res/anim文件夹当中创建一个新文件,名为list_anim_layout.xml文件:
<layoutAnimationxmlns:android= http://schemas.android.com/apk/res/android
android:delay= “0.5”
android:animationOrder= “random”
android:animation= “@anim/list_anim” />
2、 在布局文件当中为ListView添加如下配置:
android:layoutAnimation= “@anim/list_anim_layout”
在代码当中使用LayoutAnimationController
1、 创建一个Animation对象:
可以通过装载xml文件,或者直接使用Animation的构造函数创建Animation对象
Animation animation = (Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_item);
2、 使用如下代码创建LayoutAnimationController对象:
LayoutAnimationControllerlac = new LayoutAnimationController(animation);
3、 设置控件显示的顺序:
Lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
4、 为ListView设置LayoutAnimationController属性:
listView.setLayoutAnimation(lac);
什么是AnimationListener
1、 AnimationListener是一个监听器
2、 该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法
3、 主要包含以下三个方法:
a) onAnimationEnd(Animation animation)
b) onAnimationRepeat(Animation animation)
c) onAnimationStart(Animation animation)
animation.setAnimationListener()
原文地址 http://wenku.baidu.com/link?url=UOs93PQ1YCT3U22yeRdU5_8Od66eNHt9WmWiDMWGWMvSzV24ZcyeIuYj0ntHhzAbzDmFbp4wBJ8QuXQlldCnkV4ep_YY56SWPtivWmKQHrC
http://wenku.baidu.com/link?url=3sDw7g_Cm2oerRI99wYnJepghY_bBE-Y6Sni3oU0HmBf-UKzJc7bsKfcTfeAXxQbrp983G19OHGRpG76GHfeuMAHPDqGuv_Nd7D-Ao9mU1O