意思:
只需要开发者设置好动画的开始与结束的关键帧 中间帧通过计算来补齐
分为4种:
①alpha 透明度
②scale 缩放
③translate 位置移动
④rotate 旋转动画
AnimationSet 多种结合
需要在res/anim 资源文件下
Tween(两者之间) 动画与 Interpolator(插值)
-
android使用Animation代表抽象动画类,它包括如下几个子类
- AlphaAnimation:AlphaAnimation :透明度改变的动画。需指定动画起、止的透明度(0~1)及持续时间。
- ScaleAnimation:大小缩放的动画。需指定动画起、止的缩放比(X、Y 轴向缩放参数)及持续时间。还应通过 pivotX、pivotY 指定缩放中心的坐标。
- TranslateAnimation:位移变化的动画。需指定动画起、止的位置(X、Y 坐标)及持续时间。
- RotateAnimation:旋转动画。需指定动画起、止的旋转角度(X、Y 轴向缩放参数)及持续时间。还应通过 pivotX、pivotY 指定旋转轴心的坐标。
-
-
InterPolator根据特定的算法计算出来正果动画所需动态插入帧密度和位置。它是一个接口,定义了所有Interpolator都需要实现的方法。
- 开发者可通过实现 Interpolator 来控制动画的变化速度。
-
-
Android为Interpolator提供了如下几个实现类:
- LinearInterpolator :动画以匀速改变。
- AccelerateInterpolator:动画以匀加速改变。
- AccelerteDecelerateIterpolator:动画的改变以匀加速开始,以匀减速结束,中间改变最快。
- CycleInterpolator :动画循环播放特定的次数,变化速度按正统曲线改变。
- DecelerateInterpolator :动画以匀减速改变。
- BounceInterpolator 回弹
- OvershootInterpolator
为了在动画资源文件中指定补间动画所使用的 Interpolator,定义动画的元素 <set.../>、<alpha.../>、<scale.../>、<translate.../>、<rotate.../> 都可指定一个 android:interpolator 属性以指定动画的变化速度。Android 系统的 R.anim 类中包含了大致常量,它们定义了不同的动画速度。如:
@android:linear_interpolator :匀速变换
@android:accelerate_interpolator :加速变换
@android:decelerate_interpolator :减速变换
1.....XMl文件实现 结合代码 res/anim
、、常用的几个属性
fillAfter:是否停止在动画结束时位置,如果是在set标签中,只有在set节点中设置才有效果
interpolator:插值器,如果是在set标签中,只有在set解ID呢设置才有效果
repeatCount:重复次数,不包括第一次,infinite(无限循环)
repratMode:重复模式,与repeatCount配合使用 :reverse 反转 restart重新启动
、alpha透明度alpha 透明度<alpha></alpha>
、scale比例缩放scale 比例缩放 <scale></scale>
、translate位置移动translate 位置移动<translate></translate>
android:duration="3000" 持续时间
android:fillAfter="true" 是否停在最后位置
android:fromYDelta="0" Y轴开始坐标,
android:toYDelta="-100%p" Y轴结束坐标,以像素为单位,如果是正数,表示向下运行,如果为负数,表示向上运行,%p表示相对父控件坐标,%表示相对于自己
android:interpolator="@android:anim/accelerate_interpolator" 匀加速运行
、rotate旋转动画rotate 旋转动画 <rotate></rotate>
、综合应用:<set></set> 辅助类 AnimationUtils.loadAnimation(context, id);
、代码中应用动画:
- 找到播放动画的控件:findViewById(R.id.flower);
- 通过AnimationUtils.loadAnimation(this, R.anim.anim);装载动画
- 开始动画:flower.startAnimation(anim);
2......Java代码实现 :
创建想要哪个控件实现的动画:如下
将控件设置动画 View.startAnimation(Animation);
-
alpha 透明度 AlphaAnimation(AlphaAnimation alpha = new AlphaAnimation(1f, 0.2f);)
- fromAlpha:开始透明度,
- toAlpha:结束透明度。 取值范围(全透明0.0f~不透明1.0f)
publicvoid
alphacode(ViewcView) {
AlphaAnimation alphaAnimation =new
AlphaAnimation(1f, 0.2f);
alphaAnimation.setDuration(3000);
alphaAnimation.setRepeatCount(Animation.INFINITE);
alphaAnimation.setRepeatMode(Animation.REVERSE);
rockImage.startAnimation(alphaAnimation);
}
-
scale 比例缩放 ScaleAnimation(new ScaleAnimation(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue);)
- // fromX// toX::开始(结束)缩放的X轴倍数。如1.0f:本身大小;如2.0f:从自己两倍开始
- // fromY//toY:始缩放的Y轴倍数。
-
translate 位置移动(new TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue))
- // pivotXType:X轴缩放中心点类型;可选值有:
-
// pivotXValue:在pivotXType的基础上,X轴缩放中心的位置。如:0.5f:缩放中心就在控件的一半的位置。如果是0.0f,则会在控件本身的左边位置
- Animation.RELATIVE_TO_SELF相对自己--常用
- Animation.RELATIVE_TO_PARENT相对父窗体
- Animation.ABSOLUTE 绝对的---不常用
- // pivotYType:X轴缩放中心点类型;同上 ...
- // pivotYValue:在pivotYType的基础上,Y轴缩放中心的位置。
- rotate 旋转动画(new RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue))
fromDegrees: | 开始旋转的角度;三点方向为0度,六点方向为90度。 |
toDegrees: | 结束旋转的角度 |
pivotXType:/pivotYType: | 参照缩放中心dian动画类型 Animation.RELATIVE_TO_SELF相对自己--常用 Animation.RELATIVE_TO_PARENT相对父窗体 Animation.ABSOLUTE 绝对的---不常用 |
pivotXValue/pivotYValue: | 参照缩放中心位置 如:0.5f:缩放中心就在控件的一半的位置。如果是0.0f,则会在控件本身的左边位置 |
publicvoid
rotetecode(ViewcView) {
//定义动画
RotateAnimation rotateAnimation =new
RotateAnimation(-15, 15, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, -2f);
rotateAnimation.setDuration(3000); //持续时间
rotateAnimation.setRepeatCount(Animation.INFINITE);
//执行次数
rotateAnimation.setRepeatMode(Animation.REVERSE);
//执行模式
rockImage.setAnimation(rotateAnimation);
}
- 综合应用:AnimationSet,set.addAnimation(a);
// 动画集合
AnimationSet set = new AnimationSet(true);
set.addAnimation(alpha);
set.addAnimation(rotate);
set.setFillAfter(true);
// 播放动画
flower.startAnimation(set);
补间动画侦听方法:
通过Animation.setAnimationListener的方法。传入AnimationListener的接口对象。实现接口的三个方法。
alphaAnimation.setAnimationListener(new
AnimationListener() {
@Override
publicvoid
onAnimationStart(Animationanimation) {
//
TODO Auto-generated method stub
Log.i("TAG","---------------onAnimationStart---");
}
@Override
publicvoid
onAnimationRepeat(Animationanimation) {
//
TODO Auto-generated method stub
Log.i("TAG","---------------onAnimationRepeat---");
}
@Override
publicvoid
onAnimationEnd(Animationanimation) {
//
TODO Auto-generated method stub
Log.i("TAG","---------------onAnimationEnd---");
}
});
例子:Activity跳转动画
enter_anim.xml
<?xmlversion="1.0"encoding="utf-8"?>
<setxmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000">
<alpha
android:fromAlpha="1"
android:toAlpha="0.3"/>
<scale
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.2"
android:toYScale="0.2"/>
</set>
exit_anim.xml
<?xmlversion="1.0"encoding="utf-8"?>
<setxmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000">
<alpha
android:fromAlpha="0.2"
android:toAlpha="1"/>
<scale
android:fromXScale="0.2"
android:fromYScale="0.2"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1"/>
</set>
跳转时启动动画
Intent intent =new
Intent(this,ScondActivity.class);
startActivity(intent);
//设置动画 填写两个动画效果
overridePendingTransition(R.anim.after,
R.anim.befrom);