一 . Animation 动画类型
Android的animation由四种类型组成:
XML中
alph 渐变透明度动画效果
scale 渐变尺寸伸缩动画效果
translate 画面转换位置移动动画效果
rotate 画面转移旋转动画效果
JavaCode中
AlphaAnimation 渐变透明度动画效果
ScaleAnimation 渐变尺寸伸缩动画效果
TranslateAnimation 画面转换位置移动动画效果
RotateAnimation 画面转移旋转动画效果
Animation主要有两种动画模式:
一种是tweened animation(渐变动画)
XML中 JavaCode
alpha AlphaAnimation
scale ScaleAnimation
一种是frame by frame(画面转换动画)
XML中 JavaCode
translate TranslateAnimation
rotate RotateAnimation
二 .动画实现
(1)alpha动画实现
方式1 (xml设定)
anim/anim_alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<!--
透明度控制动画效果 alpha
浮点型值:
fromAlpha 属性为动画起始时透明度
toAlpha 属性为动画结束时透明度
说明:
0.0表示完全透明
1.0表示完全不透明
以上值取0.0-1.0之间的float数据类型的数字
长整型值:
duration 属性为动画持续时间
说明:
时间以毫秒为单位
-->
</set>
Java code:
(1)private Animation myAnimation_Alpha;
(2)
myAnimation_Alpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
imageView.startAnimation(myAnimation_Alpha);
方式2 (java code)
(1)private Animation myAnimation_Alpha;
(2)
//根据各自的构造方法来初始化一个Animation实例对象
//第一个参数fromAlpha为 动画开始时候透明度
//第二个参数toAlpha为 动画结束时候透明度
//说明:0.0表示完全透明,1.0表示完全不透明
myAnimation_Alpha = new AlphaAnimation(0.1f, 1.0f);
//设置时间持续时间为 5000毫秒
myAnimation_Alpha.setDuration(5000);
(3)
//true表示使用的是interpolator,false则是使用得自己的
AnimationSet animationSet = new AnimationSet(true);
//将myAnimation_Alpha对象添加到animationSet中
animationSet.addAnimation(myAnimation_Alpha);
//使用imageView的startAnimation执行动画
imageView.startAnimation(animationSet);
(2)scale动画实现
方式1 (xml设定)
anim/anim_scale.xml
<scale
android:duration="1000"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.4"
android:toYScale="1.4" />
<!--
尺寸伸缩动画效果 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 ,该动画转化在动画结束后被应用
-->
Java code:
(1)private Animation myAnimation_Scale;
(2)
myAnimation_Scale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
imageView.startAnimation(myAnimation_Scale);
方式2: (java code)
//true表示使用的是interpolator,false则是使用得自己的
AnimationSet animationSet1 = new AnimationSet(true);
//第一个参数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);
//设置时间持续时间为 700毫秒
myAnimation_Scale.setDuration(700);
//将myAnimation_Scale对象添加到animationSet中
animationSet.addAnimation(myAnimation_Scale);
//使用imageView的startAnimation执行动画
imageView.startAnimation(animationSet1);
(3)translate动画实现
方式1 (xml设定)
anim/anim_translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fromXDelta="30"
android:fromYDelta="30"
android:toXDelta="-80"
android:toYDelta="300" />
<!--
translate 位置转移动画效果
整型值:
fromXDelta 属性为动画起始时 X坐标上的位置
toXDelta 属性为动画结束时 X坐标上的位置
fromYDelta 属性为动画起始时 Y坐标上的位置
toYDelta 属性为动画结束时 Y坐标上的位置
注意:
没有指定fromXType toXType fromYType toYType 时候,
默认是以自己为相对参照物
长整型值:
duration 属性为动画持续时间
说明: 时间以毫秒为单位
-->
</set>
Java code:
(1)private Animation myAnimation_Translate;
(2)
myAnimation_Translate = AnimationUtils.loadAnimation(this,R.anim.anim_translate);
imageView.startAnimation(myAnimation_Translate);
方式2 (java code)
//true表示使用的是interpolator,false则是使用得自己的
AnimationSet animationSet2 = new AnimationSet(true);
//第一个参数fromXDelta为动画起始时 X坐标上的移动位置
//第二个参数toXDelta为动画结束时 X坐标上的移动位置
//第三个参数fromYDelta为动画起始时Y坐标上的移动位置
//第四个参数toYDelta为动画结束时Y坐标上的移动位置
myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
//设置时间持续时间为 2000毫秒
myAnimation_Translate.setDuration(2000);
//将myAnimation_Translate对象添加到animationSet中
animationSet.addAnimation(myAnimation_Translate);
//使用imageView的startAnimation执行动画
imageView.startAnimation(animationSet2);
(4)rotate动画实现
方式1 (xml设定)
anim/anim_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="3000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="+350" />
<!--
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>
java code 部分
(1)
private Animation myAnimation_Rotate;
(2)
myAnimation_Rotate = AnimationUtils.loadAnimation(this, R.anim.my_animrotate);
imageView.startAnimation(myAnimation_Rotate);
方式2 (Java code)
//true表示使用的是interpolator,false则是使用得自己的
AnimationSet animationSet3 = new AnimationSet(true);
//第一个参数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);
//设置时间持续时间为 3000毫秒
myAnimation_Rotate.setDuration(3000);
//将myAnimation_Rotate对象添加到animationSet中
animationSet.addAnimation(myAnimation_Rotate);
//使用imageView的startAnimation执行动画
imageView.startAnimation(animationSet3);
三 .此外:
还可以使用Animation-list实现逐帧动画
drawable/load_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
根标签下,通过item标签对动画中的每一个图片进行声明
android:duration 表示展示所用的该图片的时间长度
-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/cmmusic_progress_1"
android:duration="150">
</item>
<item
android:drawable="@drawable/cmmusic_progress_2"
android:duration="150">
</item>
<item
android:drawable="@drawable/cmmusic_progress_3"
android:duration="150">
</item>
<item
android:drawable="@drawable/cmmusic_progress_4"
android:duration="150">
</item>
<item
android:drawable="@drawable/cmmusic_progress_5"
android:duration="150">
</item>
<item
android:drawable="@drawable/cmmusic_progress_6"
android:duration="150">
</item>
<item
android:drawable="@drawable/cmmusic_progress_7"
android:duration="150">
</item>
<item
android:drawable="@drawable/cmmusic_progress_8"
android:duration="150">
</item>
</animation-list>
如何引用?
(1)
private ImageView imageview;
private AnimationDrawable animationDrawable;
(2)
//Sets a drawable as the content of this ImageView.
imageview.setImageResource(R.drawable.load_anim);
//给动画资源赋值
animationDrawable = (AnimationDrawable) imageview.getDrawable();
//开始动画
animationDrawable.start();
//停止动画
//animationDrawable.stop();