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

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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值