Android-Animations的使用大全之一:Tweened Animations详解

来源:互联网 发布:杭州大当家网络 编辑:程序博客网 时间:2024/05/17 22:28

Android-Animations的使用大全之二:Frame Animation和其他

 

1 简介

Animations的使用

什么是Animations

提供了一系列的动画效果,可以应用在绝大多数控件中。通过startAnimations使用。

 

Animations的分类

1 Tweened Animations 渐变动画

提供了旋转,移动,伸展,淡出等效果

2 Frame-by-Frame Animations

可以创建一个Drawable序列,按照指定时间间歇一个个显示

 

2 Tweened Animations

 

   

2.1 Tweened Animations-XML:

XML里:

<alpha>

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" ><alphaandroid:fromAlpha="0.1"android:toAlpha="1.0"android:duration="3000"/> <!-- 透明度控制动画效果 alpha浮点型值:fromAlpha 属性为动画起始时透明度toAlpha 属性为动画结束时透明度说明: 0.0表示完全透明1.0表示完全不透明以上值取0.0-1.0之间的float数据类型的数字 长整型值:duration 属性为动画持续时间说明: 时间以毫秒为单位--></set>

 

<scale>

 

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:fromXScale="0.0"android:toXScale="1.4"android:fromYScale="0.0"android:toYScale="1.4"android:pivotX="50%"android:pivotY="50%"android:fillAfter="false"android:duration="700" /></set><!-- 尺寸伸缩动画效果 scale属性:interpolator 指定一个动画的插入器在我试验过程中,使用android.res.anim中的资源时候发现有三种动画插入器:accelerate_decelerate_interpolator 加速-减速 动画插入器accelerate_interpolator 加速-动画插入器decelerate_interpolator 减速- 动画插入器其他的属于特定的动画效果浮点型值: fromXScale 属性为动画起始时 X坐标上的伸缩尺寸 toXScale 属性为动画结束时 X坐标上的伸缩尺寸  fromYScale 属性为动画起始时Y坐标上的伸缩尺寸 toYScale 属性为动画结束时Y坐标上的伸缩尺寸  说明:以上四种属性值  0.0表示收缩到没有 1.0表示正常无伸缩 值小于1.0表示收缩 值大于1.0表示放大 pivotX 属性为动画相对于物件的X坐标的开始位置pivotY 属性为动画相对于物件的Y坐标的开始位置 说明:以上两个属性值 从0%-100%中取值50%为物件的X或Y方向坐标上的中点位置 长整型值:duration 属性为动画持续时间说明: 时间以毫秒为单位 布尔型值:fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用-->

 

 

<translate>

 

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromXDelta="30"android:toXDelta="-80"android:fromYDelta="30"android:toYDelta="300"android:duration="2000"/><!-- translate 位置转移动画效果整型值:fromXDelta 属性为动画起始时 X坐标上的位置 toXDelta 属性为动画结束时 X坐标上的位置fromYDelta 属性为动画起始时 Y坐标上的位置toYDelta 属性为动画结束时 Y坐标上的位置注意:没有指定fromXType toXType fromYType toYType 时候,默认是以自己为相对参照物 长整型值:duration 属性为动画持续时间说明: 时间以毫秒为单位--></set>

 

 

 

<rotate>

 

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:fromDegrees="0"android:toDegrees="+350"android:pivotX="50%"android:pivotY="50%"android:duration="3000" /> <!-- rotate 旋转动画效果属性:interpolator 指定一个动画的插入器在我试验过程中,使用android.res.anim中的资源时候发现有三种动画插入器:accelerate_decelerate_interpolator 加速-减速 动画插入器accelerate_interpolator 加速-动画插入器decelerate_interpolator 减速- 动画插入器其他的属于特定的动画效果 浮点数型值:fromDegrees 属性为动画起始时物件的角度 toDegrees 属性为动画结束时物件旋转的角度 可以大于360度   说明:当角度为负数——表示逆时针旋转当角度为正数——表示顺时针旋转 (负数from——to正数:顺时针旋转) (负数from——to负数:逆时针旋转) (正数from——to正数:顺时针旋转) (正数from——to负数:逆时针旋转)  pivotX 属性为动画相对于物件的X坐标的开始位置pivotY 属性为动画相对于物件的Y坐标的开始位置 说明: 以上两个属性值 从0%-100%中取值50%为物件的X或Y方向坐标上的中点位置 长整型值:duration 属性为动画持续时间说明: 时间以毫秒为单位--></set>

 

代码里:

public static Animation loadAnimation (Context context, int id) //第一个参数Context为程序的上下文 //第二个参数id为动画XML文件的引用//例子:myAnimation= AnimationUtils.loadAnimation(this,R.anim.my_action);//使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件

 

2.2 Tweened Animations-JavaCode:

 

//在代码中定义 动画实例对象private Animation myAnimation_Alpha;private Animation myAnimation_Scale;private Animation myAnimation_Translate;private Animation myAnimation_Rotate; //根据各自的构造方法来初始化一个实例对象myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f); myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f); myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);

 

 

AlphaAnimation

 

AlphaAnimation(float fromAlpha, float toAlpha) //第一个参数fromAlpha为 动画开始时候透明度//第二个参数toAlpha为 动画结束时候透明度myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);//说明: // 0.0表示完全透明// 1.0表示完全不透明
 

 

 

ScaleAnimation

 

ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) //第一个参数fromX为动画起始时 X坐标上的伸缩尺寸 //第二个参数toX为动画结束时 X坐标上的伸缩尺寸 //第三个参数fromY为动画起始时Y坐标上的伸缩尺寸 //第四个参数toY为动画结束时Y坐标上的伸缩尺寸 /*说明:以上四种属性值 0.0表示收缩到没有 1.0表示正常无伸缩 值小于1.0表示收缩 值大于1.0表示放大*///第五个参数pivotXType为动画在X轴相对于物件位置类型 //第六个参数pivotXValue为动画相对于物件的X坐标的开始位置//第七个参数pivotXType为动画在Y轴相对于物件位置类型 //第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

 

TranslateAnimation

 

TranslateAnimation(float fromXDelta, float toXDelta,float fromYDelta, float toYDelta) //第一个参数fromXDelta为动画起始时 X坐标上的移动位置 //第二个参数toXDelta为动画结束时 X坐标上的移动位置 //第三个参数fromYDelta为动画起始时Y坐标上的移动位置 //第四个参数toYDelta为动画结束时Y坐标上的移动位置
 

RotateAnimation

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)//第一个参数fromDegrees为动画起始时的旋转角度 //第二个参数toDegrees为动画旋转到的角度 //第三个参数pivotXType为动画在X轴相对于物件位置类型 //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置//第五个参数pivotXType为动画在Y轴相对于物件位置类型 //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);

 

2.3  Tweened Animations 通用属性

setDuration:持续时间

setFillAfter:控件样式为动画结束后样式

SetFillBefore:控件样式为动画开始前样式

setStartOffSet:动画开始时间

setRepeatCount:重复次数

 

3 Tweened AnimationsSet

 

XML:

 
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false"><!-- 旋转rotate要外围加<set android:interpolator="@android:anim/linear_interpolator">才能达到匀速 -->    <translate android:fromXDelta="100%" android:toXDelta="0"        android:duration="1000" android:interpolator="@android:anim/linear_interpolator" />    <set android:interpolator="@android:anim/linear_interpolator">        <rotate android:fromDegrees="0.0" android:toDegrees="360.0"            android:pivotX="50%" android:pivotY="50%" android:duration="1000"            android:repeatMode="restart" android:repeatCount="10"             />    </set>    <set android:interpolator="@android:anim/linear_interpolator">        <rotate android:fromDegrees="0.0" android:toDegrees="360.0"            android:pivotX="50%" android:pivotY="50%" android:startOffset="1000"            android:duration="1000" android:repeatMode="restart"            android:repeatCount="10" />    </set>    <scale android:fromXScale="1" android:toXScale="2.0"        android:fromYScale="1" android:toYScale="2.0" android:pivotX="50%"        android:pivotY="50%" android:startOffset="1000" android:duration="1000"        android:repeatMode="reverse" android:repeatCount="10"        android:fillBefore="true" />    <alpha xmlns:android="http://schemas.android.com/apk/res/android"        android:interpolator="@android:anim/accelerate_interpolator"        android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000"        android:startOffset="10000" />    <set android:interpolator="@android:anim/linear_interpolator">        <rotate android:fromDegrees="0.0" android:toDegrees="360.0"            android:pivotX="50%" android:pivotY="50%" android:startOffset="10000"            android:duration="1000" />    </set>    <scale android:fromXScale="2.0" android:toXScale="0.5"        android:fromYScale="2.0" android:toYScale="0.5" android:pivotX="50%"        android:pivotY="50%" android:startOffset="10000" android:duration="1000"        android:repeatMode="reverse" android:repeatCount="10"        android:fillBefore="true" /></set>

 

JavaCode:

1 创建一个AnimationSet对象

AnimationSet animationSet=new AnimationSet(true); 

2 根据需要创建相应的Animation对象

Animation myAnimationAlpha=new AlphaAnimation(0.1f, 1.0f);

3 根据软件动画的需求,为Animation对象设置相应数据

animationSet.setDuration(1000); //动画执行时间

4 将Animation对象添加到AnimationSet对象中

animationSet.addAnimation(alphaAnimation);

5 使用控件对象开始执行AnimationSet

imageView.startAnimation(animationSet);

 

 

使用例子:

 

 

 

Java代码  收藏代码
  1. AnimationSet animationSet = new AnimationSet(ture);  
  2. AlpahaAnimation alpha = new AlphaAnimation(...);  
  3. RotateAnimation rotate = new RotateAnimation(...);  
  4. animationSet.addAnimation(alpha);  
  5. animationSet.addAnimaion(rotate);  
  6. animationSet.setDuration(2000);  
  7. animationSet.setStartOffset(500);  
  8. imageView.startAnimation(animationSet);  

 

4 Interpolator的使用方法

Interpolator定义了动画变化速率,在Animations框架中定义了以下几种Interpolator

 

AccelerateDecelerateInterpolator:在动画开始和结束的地方速率变化较慢,中间的时候加速

AccelerateInterpolator:在动画开始的地方速率改变较慢,然后加速

CycleInterpolator:动画循环播放特定次数,速率改变沿正弦曲线

DecelerateInterpolator:在动画开始的地方速率改变较慢,然后减速

LinearInterpolator:以均匀的速率改变

 

设置的地方就在set标签中的 android:interpolator="@android:anim/accelerate_interpolator"

而之后还有一个android:shareInterpolator="true" 从名字就可以看到这是为set中所有的动画设置Interpolator

如果要单独设置 则将shareInterpolator设为false 然后为每个动画中单独定义Interpolator

 

以上是在xml中设置,如果要在代码中设置

animationSet.setInterpolator(new AccelerateInterpolator());(也可以单独设置)

注意在AnimationSet的构造方法中有一个boolean参数,这个参数就是shareInterpolator的设定

 

 

原创粉丝点击