android支持三种类型的动画:
·属性动画 一种补间动画,通过在目标对象的任何属性的两个值之间应用赠了变化,可以生成一种动画效果。这种动画可以用来生成各种效果,例如:改变视图的颜色、透明条、淡入淡出、改变字体大小或者增加字符的生命力。
·视图动画 一种补间动画,可以用来旋转、缩放和拉伸一个视图。
·帧动画 逐帧的格子动画,来显示一系列的drawable图片。
属性动画示例:1秒内以增量的方式在0和1之间调用目标对象的setAlpha方法,从而改变目标对象的透明度。
xml文件存放位置res/animator 文件夹下
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="alpha"
android:duration="1000"
android:valueFrom="0.0"
android:valueTo="1.0"> </objectAnimator>
调用方法:
private ImageView img; img=(ImageView)this.findViewById(R.id.imageView1);
Animator animator=AnimatorInflater.loadAnimator(this, R.animator.alpha);
animator.setTarget(img);
animator.start();
视图动画示例:目标在旋转360°的同时收缩并淡出。
xml文件存放位置 res/anim
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" > <rotate
android:duration="1000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="500"
android:toDegrees="360" /> <scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="500"
android:toXScale="0.0"
android:toYScale="0.0" /> <alpha
android:duration="500"
android:fromAlpha="1.0"
android:startOffset="500"
android:toAlpha="0.0" /> </set>
调用方法:
Animation anim=AnimationUtils.loadAnimation(this, R.anim.amin_scoal);
img.setAnimation(anim);
img.animate().start();
动画类型 | 效果 | 属性 | 有效值 |
Alpha | 淡入/淡出 | fromAlpha/toAlpha | 0~1的浮点数 |
Scale | 缩放 | fromXScale/toXScale | 0~1的浮点数 |
fromYScale/toYScale | 0~1的浮点数 | ||
pivotX/pivotY | 表示图像的宽/高比的字符串,从0%~100% | ||
translate | 移动 | fromX/toX | 0~1的浮点数 |
fromY/toY | 0~1的浮点数 | ||
Rotate | 旋转 | fromDegrees/toDegrees | 0~360的浮点数 |
pivotX/pivotY | 表示图像的宽/高比的字符串,从0%~100% |
·duration 动画的持续时间,已毫秒为单位。
·startOffset 动画开始之前的延迟,毫秒为单位
·fillBeforetrue 在动画开始之前动画变形。
·fillAftertrue 在动画开始只有变形
·interpolator 设置效果随时间改变的速度
<scale>标签为缩放节点
android:fromXscale="1.0" 表示开始时X轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)
android:toXscale="0.0"表示结束时X轴缩放比例为0.0(原图大小 *0.0 为缩小到看不见)
android:fromYscale="1.0" 表示开始时Y轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)
android:toYscale="0.0"表示结束时Y轴缩放比例为0.0(原图大小 *0.0 为缩小的看不到了)
android:pivotX="50%" X轴缩放的位置为中心点
android:pivotY="50%" Y轴缩放的位置为中心点
android:duration="2000" 动画播放时间 这里是2000毫秒也就是2秒
逐帧动画示例:循环显示一系列位图资源,每个资源会持续半秒。
xml文件存放位置res/drawable
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/test1" android:duration="500"></item>
<item android:drawable="@drawable/test2" android:duration="500"></item>
<item android:drawable="@drawable/test3" android:duration="500"></item>
</animation-list>
调用方法:
ImageView view=new ImageView(this); view.setBackgroundResource(R.drawable.animtion_d); AnimationDrawable anim=(AnimationDrawable)view.getBackground(); anim.start();