天天看点

animation之补间动画

补间动画

设定初始值和终止值,由系统算法算出过渡值的动画

补间动画与属性动画最大的不同在于补间动画的属性并不会真实作用在控件上

补间动画有四种(位移 , 缩放 , 透明度,旋转),四种动画动可以用xml和code实现

四种动画都有的公共属性

属性名称 xml code
执行时间 android:duration="1000" animation.setDuration(1000);
执行次数 android:repeatCount="1" animation.setRepeatCount(1);
重复方式 android:repeatMode="reverse" animation.setRepeatMode(Animation.REVERSE)
保留结束状态 android:fillAfter="true" animation.setFillAfter(true);
延时执行 android:startOffset ="1000" animation.setStartOffset(1000);
插值器

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

animation.setInterpolator(new AccelerateDecelerateInterpolator());

 注意

1)执行次数从0开始,所以真正的执行次数时n+1 ,传-1,一直执行

2)重复方式有两种:第一种是默认的Animation.RESTART 重新执行,第二种是Animation.REVERSE 翻转执行

3)动画执行时间单位是ms

位移动画:

xml实现

在anim文件夹下新建translate_anim文件,内部实现

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"   //X轴初始位置
    android:fromYDelta="0"   //Y轴初始位置
    android:toXDelta="100"   //X轴终止位置
    android:toYDelta="100"   //Y轴终止位置
    android:duration="1000" />
           
代码引用
ivPic.startAnimation(AnimationUtils.loadAnimation(this , R.anim.translate_anim));
           

code实现

TranslateAnimation animation = new TranslateAnimation(0 , 100 , 0 , 100);
一参是初始X轴位置
二参是终点X轴位置
三参是初始Y轴位置
四参是终点Y轴位置
animation.setDuration(1000);
ivPic.startAnimation(animation);
           

 缩放动画

xml实现

在anim文件夹下新建scale_anim文件,内部实现

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1"  //X轴初始缩放倍数
    android:fromYScale="1"  //Y轴初始缩放倍数
    android:toXScale="0"    //X轴动画终止时缩放倍数
    android:toYScale="0"    //Y轴终止缩放倍数
    android:pivotX="50%"    //X轴缩放中心点
    android:pivotY="50%"    //Y轴缩放中心点
    android:duration="1000">

</scale>
           

代码引用同上.

code实现

ScaleAnimation animation = new ScaleAnimation(1 , 0 , 1 , 0
   ,Animation.RELATIVE_TO_SELF , 0.5f , Animation.RELATIVE_TO_SELF , 0.5f);
//参1是X轴初始缩放倍数
//参3是Y轴初始缩放倍数
//参2是X轴动画终止时缩放倍数
//参4是Y轴终止缩放倍数
//参5和参6是控件相对于自己在X轴上的缩放中心点
//参7和参8是控件相对于自己再Y轴上的缩放中心点
animation.setDuration(1000);
ivPic.startAnimation(animation);
           

 透明度动画

xml实现

在anim文件夹下新建alpha_anim文件,内部实现

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1"  //初始透明度
    android:toAlpha="0"    //终止透明度
    android:duration="1000"/>
           

code实现

AlphaAnimation animation = new AlphaAnimation(1 , 0);
//参1是初始透明度 参2是终止透明度
animation.setDuration(1000);
ivPic.startAnimation(animation);
           

旋转动画

xml实现

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"  //初始旋转角度
    android:toDegrees="360"  //旋转角度
    android:pivotX="50%"     //X轴旋转中心点(相对于控件自身)
    android:pivotY="50%"     //Y轴旋转中心点(相对于控件自身)
    android:duration="1000"/>
           

code实现

RotateAnimation animation = new RotateAnimation(0 , 360 ,
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//参1是初始角度 参2是旋转角度 //参3456同上面缩放动画
animation.setDuration(1000);
ivPic.startAnimation(animation);
           

动画监听

animation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        //动画开始
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        //动画结束
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
      //动画重复
    }
});
           

注意:动画并不会改变控件属性,所以无论执行什么动画,还是只有原来控件所在的位置可以响应点击事件