补间动画(Tween Animation)
补间动画是通过在两个关键帧之间补充渐变的动画效果来实现的。补间动画的优点是可以节省空间。目前Android应用框架支持的补间动画效果有以下5种。具体实现在android.view.animation类库中。 AlphaAnimation:透明度(alpha)渐变效果,对应alpha标签。 TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应translate标签。 ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应scale标签。 RotateAnimation:旋转渐变,可以指定旋转的参考点,对应rotate标签。 AnimationSet:组合渐变,支持组合多种渐变效果,对应set标签。
补间动画的代码实现
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f , 0.0f );
alphaAnimation.setDuration(2000 );
alphaAnimation.setRepeatCount(2 );
alphaAnimation.setRepeatMode(Animation.REVERSE);
alphaAnimation.setFillAfter(true );
iv.startAnimation(alphaAnimation);
RotateAnimation rotateAnimation = new RotateAnimation(0 , 360 )
rotateAnimation.setDuration (3000 )
rotateAnimation.setRepeatCount (1 )
rotateAnimation.setRepeatMode (Animation.RESTART )
iv.setAnimation (rotateAnimation)
RotateAnimation rotateAnimation=
newRotateAnimation(0 ,360 ,Animation.RELATIVE _TO_SELF, 0.5 f, Animation.RELATIVE _TO_SELF,0.5 f)
ScaleAnimation sa = new ScaleAnimation(0.0f ,1.0f ,0.0f ,1.0f ,Animation.RELATIVE_TO_SELF,0.5f ,Animation.RELATIVE_TO_SELF,0.5f );
sa.setDuration(3000 );
iv.setAnimation(sa);
④TranslateAnimation(位移效果)
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE _TO_PARENT, 0 , Animation.RELATIVE _TO_PARENT,0 , Animation.RELATIVE _TO_PARENT, 0 ,Animation.RELATIVE _TO_PARENT, 0.2 f)
ta.setDuration (3000 )
iv.setAnimation (ta)
AnimationSet set = new AnimationSet(true );
set .addAnimation(am);
set .addAnimation(sm);
set .addAnimation(rm);
rl_root.startAnimation(set );
补间动画的xml实现方式
在res目录下新建文件夹anim,在anim文件夹下新建xml文件,要实现什么动画,就选择什么动画,下面给出与代码实现相同效果的xml实现方法。 xml方式定义动画的使用方式。
Animation am = AnimationUtils.loadAnimation (getApplicationContext(), R.anim .alpha )
iv.setAnimation (am)
<?xml version="1.0" encoding="utf-8"?>
<alpha
android:fromAlpha ="0"
android:toAlpha ="1"
android:duration ="2000"
android:fillAfter ="true"
android:repeatCount ="2"
android:repeatMode ="reverse"
xmlns:android ="http://schemas.android.com/apk/res/android" >
</alpha >
<?xml version="1.0" encoding="utf-8"?>
<rotate
android:fromDegrees ="0"
android:toDegrees ="360"
android:duration ="3000"
android:repeatCount ="1"
android:repeatMode ="reverse"
android:fillAfter ="true"
//这里的50 %是相对自身的,如果要相对于父控件,则写成50 %p
android:pivotX ="50%"
android:pivotY ="50%"
xmlns:android ="http://schemas.android.com/apk/res/android" >
</rotate >
<?xml version="1.0" encoding="utf-8"?>
<scale
android:fromXScale ="0"
android:toXScale ="1"
android:fromYScale ="0"
android:toYScale ="1"
android:duration ="3000"
android:pivotX ="50%"
android:pivotY ="50%"
xmlns:android ="http://schemas.android.com/apk/res/android" >
</scale >
④TranslateAnimation(位移效果)
<?xml version="1.0" encoding="utf-8"?>
<translate
//这里表示控件向下移动相对于父控件20 %的距离
android:fromXDelta ="0%p"
android:toXDelta ="0%p"
android:fromYDelta ="0%p"
android:toYDelta ="20%p"
android:duration ="3000"
xmlns:android ="http://schemas.android.com/apk/res/android" >
</translate >
⑤AnimationSet(组合效果) 组合效果很简单,对应set标签,把其他动画效果的uml标签放进去即可
<?xml version="1.0" encoding="utf-8"?>
<set >
<rotate
android:fromDegrees ="0"
android:toDegrees ="360"
android:duration ="1000"
android:fillAfter ="true"
android:pivotX ="50%"
android:pivotY ="50%"
xmlns:android ="http://schemas.android.com/apk/res/android" >
</rotate >
<alpha
android:fromAlpha ="0"
android:toAlpha ="1"
android:duration ="2000"
android:fillAfter ="true"
xmlns:android ="http://schemas.android.com/apk/res/android" >
</alpha >
<scale
android:fromXScale ="0"
android:toXScale ="1"
android:fromYScale ="0"
android:toYScale ="1"
android:duration ="1000"
android:fillAfter ="true"
android:pivotX ="50%"
android:pivotY ="50%"
xmlns:android ="http://schemas.android.com/apk/res/android" >
</scale >
</set >
补间动画的监听事件
set.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart (Animation animation) {
}
@Override
public void onAnimationRepeat (Animation animation) {
}
@Override
public void onAnimationEnd (Animation animation) {
}
});
}
注意
使用动画和效果时,虽然看着控件发生里位移或旋转,但实际上控件并没有移动,它还是在原来位置。