安卓动画主要分为两类:tween动画及frame动画。其中tween动画又分为四小类:平移,缩放,旋转,透明度。
其中tween动画可通过xml定义,也可代码动态创建。因xml定义动画可读性更高,建议使用xml定义动画。
用法:
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromXDelta="100"
android:toXDelta="0"
android:interpolator="@android:anim/linear_interpolator"
android:fromYDelta="0"
android:toYDelta="0"
android:fillAfter="true"
>
</translate>
其中interpolator属性表示插值器,duration表示动画的持续时间,fillAfter表示动画结束后view是否停留在动画结束位置(注意:只是view的内容移动到了对应的位置,view本身并未移动)
用法:
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1"
android:fromYScale="1"
android:toXScale="0.5"
android:toYScale="0.5"
android:duration="800"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fillAfter="true"
android:pivotX="0"
android:pivotY="0"
>
</scale>
其中 pivotX,pivotY分别表示缩放轴点的x,y坐标,默认是为view的中心点。
用法:
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="800"
>
</rotate>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.6"
android:toAlpha="1"
android:duration="800"
>
</alpha>
其中 alpha的值从0-1分别表示完全透明到完全不透明。
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="800"
android:fillAfter="false"
android:fromXDelta="100"
android:fromYDelta="0"
android:interpolator="@android:anim/linear_interpolator"
android:toXDelta="0"
android:toYDelta="0" >
</translate>
<alpha
android:duration="800"
android:fromAlpha="0.6"
android:toAlpha="1" >
</alpha>
</set>
说明:上面set标签内的动画是同时开始的。
步骤:1创建类继承自Animation 2.重写它的initialize,跟applyTransformation方法。
其中initialize方法做初始化工作,applyTransformation方法做矩阵变换。
public class Rotate3dAnimation extendsAnimation {
floatmFromDegree;
floatmToDegree;
floatmCenterX;
floatmCenterY;
floatmDepthZ;
booleanmReverse;
CameramCamera;
publicRotate3dAnimation(float fromDegree, float toDegree, float centerX,
floatcenterY, float depthZ, boolean reverse) {
mFromDegree=fromDegree;
mToDegree=toDegree;
mCenterX=centerX;
mCenterY=centerY;
mDepthZ=depthZ;
mReverse=reverse;
}
@Override
publicvoid initialize(int width, int height, int parentWidth,
intparentHeight) {
super.initialize(width,height, parentWidth, parentHeight);
mCamera=newCamera();
}
@Override
protectedvoid applyTransformation(float interpolatedTime, Transformation t) {
floatfromDegrees=mFromDegree;
floatdegrees=fromDegrees+((mToDegree-fromDegrees)*interpolatedTime);
floatcenterX=mCenterX;
floatcenterY=mCenterY;
Cameracamera=mCamera;
Matrixmatrix =t.getMatrix();
camera.save();
if(mReverse)
{
camera.translate(0.0f,0.0f, mDepthZ*interpolatedTime);
}else{
camera.translate(0.0f,0.0f, mDepthZ*(1.0f-interpolatedTime));
}
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX,-centerY);
matrix.postTranslate(centerX,centerY);
}
}
帧动画是顺序播放一组预先定义好的图片,类似电影播放。用AnimationDrawable来使用帧动画(对应animation-list标签)。帧动画使用起来简单,但容易造成OOM。
用法:
<animation-listxmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
>
<itemandroid:drawable="" android:duration="800"></item>
<itemandroid:drawable="" android:duration="800"></item>
<itemandroid:drawable="" android:duration="800"></item>
</animation-list>
注意:某些版本中不可用。