一、什么叫视图动画(View Animation)
视图动画是一个比较旧的动画系统,并且它仅能用于View对象。
基于View的渐变动画,她只改变了View的绘制效果,而实际属性值未变。比如动画移动一个按钮位置,但按钮点击的实际位置仍未改变。在代码中定义动画,可以参考AnimationSet类和Animation的子类;而如果使用XML,可以在res/anim/文件夹中定义XML文件。这个动画系统相对与容易创建,并且对于应用程序中所遇到的大多数需求提供了足够的能力。
在 Android 中,分别可以在 xml 中定义View Animation,也可以在程序代码中定义视图动画类型 。
Android 的 animation 由四种类型组成
XML 中 |
代码中 | 动画效果 |
alpha |
AlphaAnimation |
渐变透明度动画效果 |
scale |
ScaleAnimation |
渐变尺寸伸缩动画效果 |
translate |
TranslateAnimation |
画面转换位置移动动画效果 |
rotate |
RotateAnimation |
画面转移旋转动画效果 |
Android 动画模式
Animation 主要有两种动画模式:
一种是 tweened animation(渐变动画)
alpha
AlphaAnimation
scale
ScaleAnimation
Animation 主要有两种动画模式:
一种是 tweened animation(渐变动画)
alpha
AlphaAnimation
scale
ScaleAnimation
一种是 frame by frame(画面转换动画)
translate
TranslateAnimation
rotate
RotateAnimation
二、如何在 XML 文件中定义动画
1.在Android工程中res目录下创建一个anim文件夹
2.在anim目录心爱新建一个anim.xml文件(文件名小写)
3.加入xml代码
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <!-- Android动画XML解析--> <alpha android:duration="1000" android:fromAlpha="0.1" android:toAlpha="1.0" /> <scale android:duration="700" 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:startOffset="700" android:toXScale="1.4" android:toYScale="1.4" /> <translate android:duration="2000" android:fromXDelta="30" android:fromYDelta="30" android:toXDelta="-80" android:toYDelta="300" /> <rotate android:duration="3000" android:fromDegrees="0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toDegrees="+350" /> </set> |
每个元素表示不同的动画效果
Android动画XML解析
1).alpha:透明度控制动画效果
1.
fromAlpha 属性为动画起始时透明度
toAlpha 属性为动画结束时透明度
说明:
0.0 表示完全透明
1.0 表示完全不透明
以上值取 0.0-1.0 之间的 float 数据类型的数字
2.
duration 属性为动画持续时间
说明:
时间以毫秒为单位
|
2).scale:尺寸伸缩动画效果
1.
interpolator 指定一个动画的插入器 ,在android.res.anim 下有一些自带的插入器。
有三种动画插入器:
其他的属于特定的动画效果
2.
fromXScale 属性为动画起始时 X 坐标上的伸缩尺寸
toXScale 属性为动画结束时 X 坐标上的伸缩尺寸
fromYScale 属性为动画起始时 Y 坐标上的伸缩尺寸
toYScale 属性为动画结束时 Y 坐标上的伸缩尺寸
startOffset 属性为从上次动画停多少时间开始执行下个动画
说明:
以上四种属性值
0.0 表示收缩到没有
1.0 表示正常无伸缩
值小于 1.0 表示收缩
值大于 1.0 表示放大
3.
pivotX 属性为动画相对于物件的 X 坐标的开始位置
pivotY 属性为动画相对于物件的 Y 坐标的开始位置
说明:
以上两个属性值 从 0%-100%中取值
50%为物件的 X 或 Y 方向坐标上的中点位置
4.
duration 属性为动画持续时间
说明: 时间以毫秒为单位
5.
fillAfter 属性 当设置为 true ,该动画转化在动画结束后被应用
|
3).translate:位置转移动画效果
fromXDelta 属性为动画起始时 X 坐标上的位置
toXDelta 属性为动画结束时 X 坐标上的位置
fromYDelta 属性为动画起始时 Y 坐标上的位置
toYDelta 属性为动画结束时 Y 坐标上的位置
注意:
没有指定 fromXType toXType fromYType toYType 时候,
默认是以自己为相对参照物
2.
duration 属性为动画持续时间
说明: 时间以毫秒为单位
|
4).rotate:旋转动画效果
1.
interpolator 指定一个动画的插入器 ,在android.res.anim 下有一些自带的插入器。
有三种动画插入器:
其他的属于特定的动画效果
2. fromDegrees 属性为动画起始时物件的角度 toDegrees 属性为动画结束时物件旋转的角度 可以大于 360 度 说明: 当角度为负数——表示逆时针旋转 当角度为正数——表示顺时针旋转 (负数 from——to 正数:顺时针旋转) (负数 from——to 负数:逆时针旋转) (正数 from——to 正数:顺时针旋转) (正数 from——to 负数:逆时针旋转)
3.
pivotX 属性为动画相对于物件的 X 坐标的开始位置 pivotY 属性为动画相对于物件的 Y 坐标的开始位置 说明:
以上两个属性值 从 0%-100%中取值
50%为物件的 X 或 Y 方向坐标上的中点位置 3. duration 属性为动画持续时间 说明: 时间以毫秒为单位 |
Animation animation = AnimationUtils.loadAnimation(this,R.anim.my_anim);
//第一个参数 Context 为程序的上下文
//第二个参数 id 为动画 XML 文件的引用
//第二个参数 id 为动画 XML 文件的引用
//使用 AnimationUtils 类的静态方法 loadAnimation()来加载 XML 中的动画 XML 文件
1.)在代码中定义 动画实例对象 ,根据各自的构造方法来初始化一个实例对象
2.)设置各自的属性
AlphaAnimation
① AlphaAnimation 类对象定义
private AlphaAnimation alpha;
② AlphaAnimation 类对象构造
AlphaAnimation(float fromAlpha, float toAlpha)
//第一个参数 fromAlpha 为 动画开始时候透明度
//第二个参数 toAlpha 为 动画结束时候透明度
alpha=new AlphaAnimation(0.1f, 1.0f);
//说明:
// 0.0 表示完全透明
// 1.0 表示完全不透明
③ 设置动画持续时间
alpha.setDuration(5000);
//设置时间持续时间为 5000 毫秒
① AlphaAnimation 类对象定义
private AlphaAnimation alpha;
② AlphaAnimation 类对象构造
AlphaAnimation(float fromAlpha, float toAlpha)
//第一个参数 fromAlpha 为 动画开始时候透明度
//第二个参数 toAlpha 为 动画结束时候透明度
alpha=new AlphaAnimation(0.1f, 1.0f);
//说明:
// 0.0 表示完全透明
// 1.0 表示完全不透明
③ 设置动画持续时间
alpha.setDuration(5000);
//设置时间持续时间为 5000 毫秒
ScaleAnimation
①ScaleAnimation 类对象定义
private AlphaAnimation scale;
② 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 坐标的开始位置
scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
③ 设置动画持续时间
scale.setDuration(700);
//设置时间持续时间为 700 毫秒
TranslateAnimation
① TranslateAnimation 类对象定义
private AlphaAnimation translate;
② TranslateAnimation 类对象构造
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
//第一个参数 fromXDelta 为动画起始时 X 坐标上的移动位置
//第二个参数 toXDelta 为动画结束时 X 坐标上的移动位置
//第三个参数 fromYDelta 为动画起始时 Y 坐标上的移动位置
//第四个参数 toYDelta 为动画结束时 Y 坐标上的移动位置
③ 设置动画持续时间
translate.setDuration(2000);
//设置时间持续时间为 2000 毫秒
RotateAnimation
① RotateAnimation 类对象定义
private AlphaAnimation rotate;
② RotateAnimation 类对象构造
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
int pivotYType, float pivotYValue)
//第一个参数 fromDegrees 为动画起始时的旋转角度
//第二个参数 toDegrees 为动画旋转到的角度
//第三个参数 pivotXType 为动画在 X 轴相对于物件位置类型
//第四个参数 pivotXValue 为动画相对于物件的 X 坐标的开始位置
//第五个参数 pivotXType 为动画在 Y 轴相对于物件位置类型
//第六个参数 pivotYValue 为动画相对于物件的 Y 坐标的开始位置
rotate=new RotateAnimation(0.0f, 350.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f) ;
③ 设置动画持续时间
rotate.setDuration(3000);
//设置时间持续时间为 3000 毫秒
//第一个参数 fromDegrees 为动画起始时的旋转角度
//第二个参数 toDegrees 为动画旋转到的角度
//第三个参数 pivotXType 为动画在 X 轴相对于物件位置类型
//第四个参数 pivotXValue 为动画相对于物件的 X 坐标的开始位置
//第五个参数 pivotXType 为动画在 Y 轴相对于物件位置类型
//第六个参数 pivotYValue 为动画相对于物件的 Y 坐标的开始位置
rotate=new RotateAnimation(0.0f, 350.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f) ;
③ 设置动画持续时间
rotate.setDuration(3000);
//设置时间持续时间为 3000 毫秒
----------------------------------------------------------------------------------------------------------------
简单用 RotateAnimation小李子 简单展示一下两种方法的用法。
对于其他动画,如
ScaleAnimation,AlphaAnimation,TranslateAnimation 原理是一样的
方法一、
在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:toDegrees="360" /> </set> |
public void onclick(View v) {
Animation animation = AnimationUtils.loadAnimation(this, R.id.my_anim);
textView.startAnimation(animation);
}
|
方法二、
代码定义动画
代码如下
public void onClick(View v) {
Animation anim = null; anim = new RotateAnimation(0.0f,+360.0f); anim.setInterpolator(new AccelerateDecelerateInterpolator()); anim.setDuration(3000); findViewById(R.id.TextView01).startAnimation(anim); . } |