Android动画一共分为三大类分别为Tween Animation(补间动画)、Frame Animation(逐帧动画)、Propetry Animation(属性动画)。
而补间动画又分为AlphaAnimation(透明动画), RotateAnimation(旋转动画), ScaleAnimation(缩放动画), TranslateAnimation(平移动画), AnimationSet(组合动画)
补间动画效果既可以用代码形式定义,也可以通过xml语言来定义
AlphaAnimation
// 透明动画
public void alphaAnimation() {
// 代码实现
// AlphaAnimation aa = new AlphaAnimation(0, 1);//0代表完全不透明,1代表完全透明
// aa.setDuration(1000);//设置动画持续时间
// ivIcon.startAnimation(aa);
// 使用xml方式实现
ivIcon.startAnimation(AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.alpha));
}
R.anim.alpha文件:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration="1000">
<!--
fromAlpha 动画开始时的透明度0表示完全透明
toAlpha 动画结束时的透明度1表示完全不透明
duration 动画持续时间
-->
</alpha>
RotateAnimation
// 旋转动画
public void rotateAnimation() {
// 代码实现
// 构造方法一,实现360度环绕
// RotateAnimation ra = new RotateAnimation(0, 360);
// 构造方法二,实现绕自身中心360度环绕,pivoX,pivoY的单位为像素!pivoX是距离执行动画控件左边缘的距离,pivoY是距离执行动画控件上边缘的距离
// RotateAnimation ra = new RotateAnimation(0, 360, ivIcon.getWidth() /
// 2,ivIcon.getHeight() / 2);
// 构造方法三。实现绕自身中心360度环绕
// RotateAnimation ra = new RotateAnimation(0, 360,
// Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
// 0.5f);
// 设置动画执行时间
// ra.setDuration(1000);
// 为ivIcon控件(一个ImageView)设置动画
// ivIcon.startAnimation(ra);
// 使用xml方式实现
ivIcon.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,
R.anim.rotate));
}
R.anim.rotate文件:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="reverse">
<!--
fromDegrees:旋转起始角度
toDegrees:旋转终止角度
pivotX:可以设置百分比,50%即执行动画控件的宽度的一半,若设置为一个数,则代表距离控件左边缘的距离
pivotX:可以设置百分比,50%即执行动画控件的高度的一半,若设置为一个数,则代表距离控件上边缘的距离
duration:动画持续时间
repeatCount:重复次数,设置为-1或infinite代表无限重复
repeatMode:重复模式,设置为reverse代表,旋转偶数次数时,控件倒转,restart则表示始终是顺时针转动
-->
</rotate>
ScaleAnimation
// 缩放动画
public void scaleAnimation() {
// 代码实现
// 常用的有三个构造方法,这里的pivoX,pivoY和旋转动画的是一样的,不设置的话默认为左上角
// 0代表完全缩放,1代表不缩放
// ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1,
// ivIcon.getWidth() / 2, ivIcon.getHeight() / 2);
// 也可以使用这个构造方法,通rotateAnimation
// ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1,
// Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
// 0.5f);
// sa.setDuration(1000);
// ivIcon.startAnimation(sa);
// xml实现
ivIcon.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,
R.anim.scale));
}
R.anim.scale文件:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1"
android:duration="1000">
<!-- 基本同rotateAnimation -->
</scale>
TranslateAnimation
// 平移动画
public void translateAnimation() {
// 表示从控件所在位置,向x,y方向移动相应增量
// TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 200);
// ta.setDuration(1000);
// ivIcon.startAnimation(ta);
// xml方法:
ivIcon.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,
R.anim.translate));
}
R.anim.translate文件:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="200"
android:toYDelta="200" >
<!--
toXSelta:x方向上的增量
toYDelta:y方向上的增量
-->
</translate>
AnimationSet
// 组合动画
public void setAnimation() {
// AnimationSet as = new AnimationSet(true);
// as.setDuration(3000);
//
// AlphaAnimation aa = new AlphaAnimation(0, 1);
// aa.setDuration(3000);
// as.addAnimation(aa);
//
// RotateAnimation ra = new RotateAnimation(0, 360,
// Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
// 0.5f);
// ra.setDuration(1000);
// as.addAnimation(ra);
//
// ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1,
// Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
// 0.5f);
// sa.setDuration(1000);
// as.addAnimation(sa);
//
// TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 200);
// ta.setDuration(3000);
// as.addAnimation(ta);
//
// ivIcon.startAnimation(as);
// xml方法:
ivIcon.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,
R.anim.set));
}
R.anim.set文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:shareInterpolator="true" >
<alpha
android:duration="3000"
android:fromAlpha="0"
android:toAlpha="1" />
<scale
android:duration="1000"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1" />
<rotate
android:duration="1000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
<translate
android:duration="3000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="200"
android:toYDelta="200" />
</set>