View Animation之一 Tween Animation
Tween Animation是一种补间动画, View Animation改变的是View的绘制效果(调用ondraw方法),View的实质属性是没有变化的(交互监听的位置等),tween animation只能更改四种绘制属性(translation,rotate,alpha,scale),只能作用于View上
- 1.在xml中定义ViewAnimation:
在Res/anim/目录下新建.xml文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
<!--所有viewAnimation 是否都用相同的速度(加速,减速等)运行,可以在各个ViewAnimation中分别设置interpolator-->
android:shareInterpolator="false"
<!--所有ViewAnimation的运行时长,可分别设置-->
android:duration="@android:integer/config_longAnimTime"
<!--动画结束时 画面停留在最开始的第一帧,只能在set中设置才有效-->
android:fillAfter="true"
<!--动画结束时,画面停留在最后一帧,只能在set中设置才有效-->
android:fillBefore="false"
<!--这个动画开始距离上一个动画开始的间隔时长,可分别设置-->
android:startOffset="@android:integer/config_shortAnimTime"
<!--动画重复次数,可分别设置-->
android:repeatCount="3"
<!--动画重复的模式,可以是顺播(restart)或者是倒播(reverse),可分别设置-->
android:repeatMode="reverse">
<!--AlphaAnimation 透明度动画,alpha取值范围[0.0f,1.0f],0.0f表示完全透明,1.0f表示完全不透明-->
<alpha
android:fromAlpha="0.5f"
android:toAlpha="1.0f"/>
<!--ScaleAnimation,缩放动画,沿X轴Y轴定点缩放-->
<scale
android:fromXScale="0f" //起始X轴方向的缩放比例,[0.0f,1.0f]表示缩小的值,>1.0f表示放大的值
android:toXScale="2.0f"//最终X轴方向的缩放比例
android:fromYScale="0,5f" //起始Y轴方向的缩放大小
android:toYScale="1.0f" //最终Y轴方向的缩放大小
android:pivotX="50%"//X方向上的定点缩放的位置 50%表示其view的X方向上中心位置
android:pivotY="50%" //Y轴上定点缩放的位置 50%表示其view的Y方向上中心位置
android:interpolator="@android:interpolator/accelerate_decelerate" />//设置插值为先加速后减速
<!--TranslateAnimation 位移动画-->
<translate
android:fromXDelta="-100%" //位移X轴起始位置,[-100%,100%]取值是相对于view本身的位置,[-100%p,100%p]相对于父容器的位置,具体取值代表绝对的位置
android:toXDelta="50%p"
android:fromYDelta="200dp"
android:toYDelta="100dp"
android:duration="android:duration="@android:integer/config_shortAnimTime"/>//动画时长
<!--RotateAnimation 旋转动画-->
<rotate
android:fromDegrees="0f" //起始角度
android:toDegrees="120f" //最终旋转扫描过的角度,按照逆时针方向为正值
android:pivotY="5%"//X轴方向的旋转轴心,5%表示相对窗体本身左边缘5%的位置 5%p相对父容器左边缘5%的位置
android:pivotX="5%p"//Y方向上的旋转轴心,5%p表示相对父容器上边缘5%的位置
</set>
在代码中实例化xml中的ViewAnimation:
Animation animation = AnimationUtils.loadAnimation(this, R.anim.left_in);
将ViewAnimation运用到View上
view.startAnimation(animation);
- 2.动态定义View Animation 直接在代码中定义
Animation animation1 = new AlphaAnimation(0.5f, 1.0f);
Animation animation2 = new ScaleAnimation(0f, 1.0f, 0f, 1.0f);
Animation animation3 = new RotateAnimation(0f, 120f);
Animation animation4 = new TranslateAnimation(100f, 20f, 0f, 20f);
view.startAnimation(animation);
- 3.对ViewAnimation动画的监听,setAnimationListener
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//自己的逻辑代码,动画开始时系统自动调用
}
@Override
public void onAnimationEnd(Animation animation) {
//自己的逻辑代码,动画结束时系统自动调用
}
@Override
public void onAnimationRepeat(Animation animation) {
// 自己的逻辑代码,动画重复播放时,系统自动调用
}
});
4.设置切换activity时的动画:overridePendingAnimation
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);//finish();切换动画代码在startActivity()或者finish()之后
overridePendingTransition(R.anim.animation1, R.anim.animation2);
//animation1为当前activity退出时的动画,animation2为下一个activity进入时的动画,根据需要对ViewAnimation的四种动画排列组合