安卓动画详解——补间动画

AlphaAnimation
为视图增加透明度的变化动画。
使用Java代码定义AlphaAnimation动画

private void toAlpha(){
        //0为透明,1为不透明
        AlphaAnimation anim=new AlphaAnimation(0,1);
        //设置动画播放时间
        anim.setDuration(200);
        //设置动画播放结束后保持播放之后的效果
        anim.setFillAfter(true);
        //设置重复次数 <0 无限循环
        anim.setRepeatCount(-1);

        anim.setRepeatMode(Animation.REVERSE);

        iv.setAnimation(anim);
    }

使用XML资源文件设定AlphaAnimation

protected void toAlpha() {
     Animation anim=AnimationUtils.loadAnimation(Activity.this, R.anim.anim_alpha);
     iv.startAnimation(anim);
}
 <?xml version="1.0" encoding="utf-8"?>
 <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="200" android:fillAfter="true" android:fromAlpha="0.0" android:repeatCount="-1" android:repeatMode="reverse" android:toAlpha="1.0" >
 </alpha>

RotateAnimation
为视图增加旋转的变换动画

private void toRotate(){
        //其参数分别为旋转的起始角度 起始坐标 旋转参考系为自身中心
        RotateAnimation anim=new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
        anim.setDuration(2000);
        anim.setRepeatCount(-1);
        //LinearInterpolator 动画以均匀的速率改变
        //AccelerateDecelerateInterpolator 在动画开始与结束的地方速率改变比较慢,在中间的时候加速
        //AccelerateInterpolator 在动画开始的地方速率比较慢,然后开始加速
        //CycleInterpolator 动画循环播放特定次数,速率改变沿着正弦曲线
        //DecelerateInterpolator 在动画开始的地方速率改变比较慢 然后开始减速
        anim.setInterpolator(new LinearInterpolator());

        iv.setAnimation(anim);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="2" android:toDegrees="360" >
</rotate>

TranslateAnimation
为视图移动时增加位移动画

    private void toTranslate(){
        TranslateAnimation anim=new TranslateAnimation(0,500,0,500);
        anim.setDuration(2000);
        anim.setRepeatCount(-1);
        anim.setRepeatMode(Animation.REVERSE);
        iv.setAnimation(anim);
    }
<?xml version="1.0" encoding="utf-8"?>
  <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="10%p" android:toXDelta="20%p" android:fromYDelta="10%p" android:toYDelta="20%p" android:duration="2000" android:repeatCount="2" android:repeatMode="reverse">
 </translate>

ScaleAnimation
为视图的缩放增加动画效果

    private void toScale(){
        ScaleAnimation anim=new ScaleAnimation(0,2,0,2,
                Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f
                );
        anim.setDuration(2000);
        anim.setRepeatCount(-1);
        anim.setRepeatMode(Animation.REVERSE);
        iv.setAnimation(anim);
    }
 <?xml version="1.0" encoding="utf-8"?>
  <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:pivotX="50%" android:pivotY="50%" android:fromXScale="0.2" android:fromYScale="0.2" android:toXScale="2.0" android:toYScale="2.0" >
 </scale>

AnimationSet

private void toSetAnim(){
        AnimationSet as=new AnimationSet(true);
        as.setDuration(1000);

        TranslateAnimation anim1=new TranslateAnimation(0,0,0,500);
        anim1.setDuration(1000);
        anim1.setRepeatCount(-1);
        anim1.setRepeatMode(Animation.REVERSE);
        as.addAnimation(anim1);

        RotateAnimation anim2=new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
        anim2.setDuration(1000);
        anim2.setRepeatCount(-1);
        anim2.setRepeatMode(Animation.REVERSE);
        as.addAnimation(anim2);

        iv.startAnimation(as);
    }

你可能感兴趣的:(动画)