android动画入门
android,图形,3D动画2016-06-07
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/flower1" android:duration="1000"/>
    <item android:drawable="@drawable/flower2" android:duration="1000" />
</animation-list>
android:oneshot控制动画是否重复播放,如果为true就不重复。
具体用法如下:
1、在xml布局中设置ImageView的背景为动画(anim/xx.xml) 或者用代码:myView.setBackgroundResource(+R.anim.xx);(注意R前面的"+"号,如果没有可能会报错)
2.AnimationDrawable anima = (AnimationDrawable)imageView.getBackground();
3.anima.start(); //stop()是停止
补间动画
android使用Animation抽象类来定义动画,他有下面几个子类:TranslateAnimation AlphaAnimation RotateAnimation ScaleAnimation。 只要为补间动画定义了三个必要的信息,android就会根据开始帧,结束帧,持续时间计算出中间还需要补入多少帧,并计算所有补入帧的图形。为了计算动画期间需要补入多少帧以及具体什么时间补帧,android引入了Interpolator配合补间动画,用来控制动画速度,这样就可以使动画以匀速,变速,抛物线速度等速度进行展示。
Interpolator是一个接口,常见的有如下几个实现类,分别实现不同的动画变换速度。 AccelerateInterpolator :开始较慢,后面加速。 AccelerateDecelerateInterpolator ;开始和结束较慢,中间加速。 CycleInterpolator :变换速度按正玄曲线。 DecelerateInterpolator 开始较快,然后开始减速。 LinearInterpolator :动画匀速变换。 一般都会使用资源文件来定义补间动画,这些定义可以写在资源文件中的android:interpolator属性里面,就像下面这样:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <scale android:fromXScale="1.0"
        android:toXScale="0.01"
        android:fromYScale="1.0"
        android:toYScale="0.01"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="true"
        android:duration="3000" />
    <alpha android:fromAlpha="1.0"
        android:toAlpha="0.05"
        android:duration="3000" />
    <rotate android:fromDegrees="0"
        android:toDegrees="1800"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="3000" />
</set>    补间动画的大致用法如下:2、加载动画 Animation anim = AnimationUtils.loadAnimation(this, R.anim.xxx);
animation.setFillAfter(true);
或者在代码里面写:
TranslateAnimation anim = new TranslateAnimation(curX,nextX,curY,nextY);
anim.setDuration(time);    public MyAnimation(int x,int y,int d)
    {
        cX =x;
        cY = y;
        duration = d;
    }
    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        setDuration(duration);
        setFillAfter(true); //设置动画结束后效果保留
        setInterpolator(new LinearInterpolator());
    }
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        camera.save();
        camera.translate(100 - 100 * interpolatedTime, 150 * interpolatedTime - 150, 80 - 80 * interpolatedTime);
        camera.rotateX(720 * interpolatedTime);
        camera.rotateZ(720 * interpolatedTime);
        Matrix matrix = t.getMatrix();
        camera.getMatrix(matrix);
        matrix.preTranslate(-cX, -cY);
        matrix.postTranslate(cX, cY);        
        camera.restore();
    }2、在MainActivity中启动该动画即可listView.setAnimation(new MyAnimation(screenWidth / 2, screenHeight / 2, 7000));