Andorid动画
一 分类:
可以分为三类:view动画,帧动画 和属性动画
View动画:通过对场景里的对象不断的做图像变换,从而产生动画 想过,是一种渐进式动画,支持自定义动画,但是它的位置没有发生正在的改变,知识视觉上让我们感觉改变了
帧动画:通过播放一系列图像从而产生动画效果,可以简单的理解为图片切换动画,当如果图片过多的话容易OOM,开发中自己很少使用.
属性动画:通过动态的修改对象的属性从而达到动画效果,它的位置是真正的发生了变化,是API11新增的属性,对于低版本我们可以使用兼容库使用.
*参照<Android开发艺术探索>一书*
二:基本使用:
View动画:
有4种动画效果,平移、旋转、渐变和缩放,其实view动画在本质上并没有发生位置的变化,只是在视觉上感觉变化了.对应的类分别是 TranslateAnimation、RotateAnimation 、AlphaAnimation、ScaleAnimation
方式:
1 使用xml方法:在res文件夹下创建anim文件夹,在这个文件下,选择new然后选择Animation Resource file 创建对应的动画文件.
创建完成后
在xml文件中实现我们想要的动画
<!--View动画集合-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--平移动画 -->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="50"
android:toXDelta="100"
android:fromYDelta="50"
android:toYDelta="100"
android:duration="200"
>
</translate>
<!--旋转动画-->
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="180"
android:pivotX="0.5"
android:pivotY="0.5">
</rotate>
<!--渐变动画-->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.3"
android:toAlpha="0.8">
</alpha>
<!--缩放动画-->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0"
android:toXScale="2"
android:fromYScale="0"
android:toYScale="2"
android:pivotX="0.5"
android:pivotY="0.5">
</scale>
</set>
在代码中如何使用:
Animation animation= AnimationUtils.loadAnimation(this,R.anim.translate);
animation.setDuration(300); // 动画执行时间
animation.setFillAfter(true); // 执行完动画后是否停留在结束位置
animation.setInterpolator(new LinearInterpolator()); // 设置插值器
mTv.startAnimation(animation); // 执行动画
2 在java代码中实现:
旋转动画:
/*
参数详解
float fromDegrees:旋转的开始角度。
float toDegrees: 旋转的结束角度。
int pivotXType: X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF(相对于自己)、RELATIVE_TO_PARENT(相对于父容器)。
float pivotXValue:X坐标的伸缩值。
int pivotYType: Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
float pivotYValue:Y坐标的伸缩值。
*/
RotateAnimation mRotate=new RotateAnimation(0,90,Animation.RELATIVE_TO_SELF,0.5F,Animation.RELATIVE_TO_SELF,0.5F);
mRotateAnimation.setDuration(200);
mRotateAnimation.setFillAfter(true);
mTv.startAnimation(mRotateAnimation);
平移动画:
/**
float fromXDelta, x轴开始的位置
float toXDelta, x轴结束的位置
float fromYDelta, y轴的起始值
float toYDelta y轴的结束值
*/
TranslateAnimation animation=new TranslateAnimation(0,150,0,150);
animation.setDuration(200);
animation.setFillAfter(true);
animation.setInterpolator(new LinearInterpolator());
animation.setFillAfter(true);
mTv.startAnimation(animation);
缩放动画:
/*
参数详解
float fromX x轴缩放的起始值
float toX x轴缩放的结束值
float fromY Y轴缩放的起始值
float toY Y轴缩放的结束值
int pivotXType 动画在X轴相对于物件位置类型
Animation.RELATIVE_TO_SELF(相对于自身)
Animation.RELATIVE_TO_PARENT(相对于父控件(容器))
float pivotXValue 动画相对于物件的X坐标的开始位置
int pivotYType 动画在Y轴相对于物件位置类型
float pivotYValue 动画相对于物件的Y坐标的开始位置
/*
mScaleAnimation = new ScaleAnimation(1,0.3F,1,0.3F, RELATIVE_TO_SELF,0.5f, RELATIVE_TO_SELF,0.5f);
mScaleAnimation.setDuration(200);
mScaleAnimation.setFillAfter(true);
mTv.startAnimation(mScaleAnimation);
渐变动画:
/**
float fromAlpha, 开始渐变的值
float toAlpha 结束渐变的值
*/
AlphaAnimation mAlpha= new AlphaAnimation(0,1);
mAlphaAnimation.setDuration(200);
mAlphaAnimation.setFillAfter(true);
mTv.startAnimation(mAlphaAnimation);
动画集合
AnimationSet animationSet=new AnimationSet(true);
animationSet.setDuration(300);
animationSet.addAnimation(mRotateAnimation); // 添加旋转动画
animationSet.addAnimation(mAlphaAnimation);// 添加渐变动画
animationSet.addAnimation(mScaleAnimation); // 添加缩放动画
animationSet.addAnimation(animation); // 添加平移动画
mTv.startAnimation(animationSet)