三种基本动画
1. 帧动画(Drawable Animation)
首先在res/anim下建立一个frame.xml来存放帧动画
<?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/girl_1" android:duration="100"/>
<item android:drawable="@drawable/girl_2" android:duration="100"/>
<item android:drawable="@drawable/girl_3" android:duration="100"/>
<item android:drawable="@drawable/girl_4" android:duration="100"/>
<item android:drawable="@drawable/girl_5" android:duration="100"/>
<item android:drawable="@drawable/girl_6" android:duration="100"/>
<item android:drawable="@drawable/girl_7" android:duration="100"/>
<item android:drawable="@drawable/girl_8" android:duration="100"/>
<item android:drawable="@drawable/girl_9" android:duration="100"/>
<item android:drawable="@drawable/girl_10" android:duration="100"/>
<item android:drawable="@drawable/girl_11" android:duration="100"/>
</animation-list>
布局文件中
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dip"
android:layout_height="200dip"
android:background="@anim/frame" />
类代码中
AnimationDrawable animationDrawable=(AnimationDrawable) imageView1.getBackground();
if(!animationDrawable.isRunning()){
animationDrawable.start();
}
2. 补间动画(Tween Animation)
不会改变控件真实的坐标
补间动画与逐帧动画在本质上是不同的,逐帧动画通过连续播放图片来模拟动画的效果,而补间动画则是通过在两个关键帧之间补充渐变的动画效果来实现的。补间动画的优点是可以节省空间。目前 Android应用框架支持的补间动画效果有以下5种。具体实现在android.view.animation类库中。
AlphaAnimation:透明度(alpha)渐变效果,对应<alpha/>标签。
TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应<translate/>标签。
ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应<scale/>标签。
RotateAnimation:旋转渐变,可以指定旋转的参考点,对应<rotate/>标签。
AnimationSet:组合渐变,支持组合多种渐变效果,对应<set/>标签。
实现Demo
动画定义在Xml中
// 透明动画
public void alphaImpl(View v) {
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.alpha_demo);
imageView.startAnimation(animation);
}
// 旋转动画
public void rotateImpl(View v) {
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.rotate_demo);
imageView.startAnimation(animation);
}
// 缩放动画
public void scaleImpl(View v) {
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.scale_demo);
imageView.startAnimation(animation);
}
// 移动效果
public void translateImpl(View v) {
// XML文件
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.translate_demo);
animation.setRepeatCount(Animation.INFINITE);//循环显示
imageView.startAnimation(animation);
/*
* 第一种 imageView.setAnimation(animation); animation.start();
*/
// 第二种
// Java代码
/*
* TranslateAnimation translateAnimation = new TranslateAnimation(0,
* 200, 0, 0); translateAnimation.setDuration(2000);
* imageView.startAnimation(translateAnimation);
*/
}
// 综合实现set_demo.xml中的动画
public void setAll(View v) {
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.set_demo);
imageView.startAnimation(animation);
}
alpha_demo.xml
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"/>
<!--
fromAlpha :起始透明度
toAlpha:结束透明度
1.0表示完全不透明
0.0表示完全透明
-->
rotate_demo.xml
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"/>
<!--
fromDegrees:表示旋转的起始角度
toDegrees:表示旋转的结束角度
repeatCount:旋转的次数 默认值是0 代表旋转1次 如果值是repeatCount=4 旋转5次,值为-1或者infinite时,表示补间动画永不停止
repeatMode 设置重复的模式。默认是restart。当repeatCount的值大于0或者为infinite时才有效。
repeatCount=-1 或者infinite 循环了
还可以设成reverse,表示偶数次显示动画时会做与动画文件定义的方向相反的方向动行。
-->
scale_demo.xml
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="0.2"
android:toXScale="1.5"
android:fromYScale="0.2"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
<!--
fromXScale:表示沿着x轴缩放的起始比例
toXScale:表示沿着x轴缩放的结束比例
fromYScale:表示沿着y轴缩放的起始比例
toYScale:表示沿着y轴缩放的结束比例
图片中心点:
android:pivotX="50%"
android:pivotY="50%"
-->
translate_demo.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="320"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="2000"/>
<!--
android:interpolator 动画的渲染器
1、accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速
2、decelerate_interpolator(动画减速器)使动画在开始的时候 最快,然后逐渐减速
3、accelerate_decelerate_interpolator(动画加速减速器)
中间位置分层: 使动画在开始的时候 最慢,然后逐渐加速
使动画在开始的时候 最快,然后逐渐减速 结束的位置最慢
fromXDelta 动画起始位置的横坐标
toXDelta 动画起结束位置的横坐标
fromYDelta 动画起始位置的纵坐标
toYDelta 动画结束位置的纵坐标
duration 动画的持续时间
-->
set_demo.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:shareInterpolator="true" >
<scale
android:duration="2000"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5" />
<rotate
android:duration="1000"
android:fromDegrees="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="360" />
<translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="320"
android:toYDelta="0" />
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
代码实现动画
AlphaAnimation
public void click2(View v) {
//fromDegrees开始旋转的角度
// RotateAnimation ra = new RotateAnimation(0, 360);
//0.5的意思是 控件的宽*0.5
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(2000);
ra.setRepeatCount(1);//设置动画执行重复的次数
ra.setRepeatMode(Animation.REVERSE);
//iv开始执行动画
iv.startAnimation(ra);
}
RotateAnimation
public void click2(View view) {
RotateAnimation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setRepeatCount(5);
animation.setRepeatMode(RotateAnimation.RESTART);
animation.setDuration(2000);
lv.startAnimation(animation);
}
ScaleAnimation
public void click3(View view) {
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f);
scaleAnimation.setDuration(2000);
scaleAnimation.setRepeatCount(2);
scaleAnimation.setRepeatMode(Animation.REVERSE);
lv.startAnimation(scaleAnimation);
}
TranslateAnimation
public void click4(View view) {
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.3f);
translateAnimation.setDuration(2000);
translateAnimation.setFillAfter(true);
lv.startAnimation(translateAnimation);
}
属性动画
会改变控件真实的坐标 api level 11
xml实现
AlphaAnimation
<?xml version="1.0" encoding="utf-8"?>
<animator xmlns:android="http://schemas.android.com/apk/res/android" >
<objectAnimator
android:propertyName="translationX"
android:duration="2000"
android:valueFrom="10"
android:valueTo="100"
></objectAnimator>
</animator>
RotateAnimation
<?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="0.5"
android:pivotY="0.5"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="restart">
</rotate>
ScaleAnimation
<?xml version="1.0" encoding="utf-8"?>
<scale
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
android:repeatCount="1"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</scale>
TranslateAnimation
<?xml version="1.0" encoding="utf-8"?>
<translate
android:fromXDelta="0%p"
android:toXDelta="0%p"
android:fromYDelta="0%p"
android:toYDelta="20%p"
android:duration="2000"
android:fillAfter="true"
xmlns:android="http://schemas.android.com/apk/res/android">
</translate>
类中使用
ObjectAnimator oa = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.oanimator);
//设置执行目标
oa.setTarget(iv);
oa.start();
代码中实现
public void click1(View view) {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.5f, 0, 1,0,1,0,1,0,1,0,1);
objectAnimator.setDuration(2000);
objectAnimator.start();
}