Animation集合(入门)

作者: 啸天AskSky | 来源:发表于2016-11-04 16:21 被阅读760次

补间动画Tween Animation

AlphaAnimtaion(透明)

  /**
    * 构造方法:
    * AlphaAnimation(fromAlpha, toAlpha)
    * fromAlpha:开始时的透明度
    * toAlpha:结束时的透明度
    * */
    AlphaAnimation animation = new AlphaAnimation(0, 1);
    animation.setDuration(1000);//设置动画执行时间
    v.startAnimation(animation);

RotateAnimation(旋转)


     /**
    * 构造方法: 
    * RotateAnimation(fromDegrees, toDegrees)
    * RotateAnimation(fromDegrees, toDegrees, pivotX, pivotY)
    * RotateAnimation(fromDegrees, toDegrees, pivotXType,pivotXValue, pivotYType, pivotYValue) 
    * fromDegrees:从哪个旋转角度开始
    * toDegrees:转到什么角度 后4个参数用于设置围绕着旋转的圆的圆心在哪里
    * pivotXType:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
    * pivotXValue:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴 pivotYType:确定y轴坐标的类型
    * pivotYType:确定y轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
    * pivotYValue:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
    * */
    RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation.setDuration(1000);// 设置动画执行时间
    v.startAnimation(rotateAnimation);

ScaleAnimation(缩放)

/**
    * 构造方法: 
    * ScaleAnimation(fromX, toX, fromY, toY)
    * ScaleAnimation(fromX, toX, fromY, toY, pivotX, pivotY)
    * ScaleAnimation(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue) 
    * fromX:x轴的初始值
    * toX:x轴收缩后的值
    * fromY:y轴的初始值
    * toY:y轴收缩后的值 
    * pivotXType:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
    * pivotXValue:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴 pivotYType:确定y轴坐标的类型
    * pivotYType:确定y轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
    * pivotYValue:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
    */
    ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1f, 0, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    scaleAnimation.setDuration(1000);// 设置动画执行时间
    v.startAnimation(scaleAnimation);

TranslateAnimation(位移)

/**
    * 构造方法: 
    * TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta) 
    * TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue)
    * fromXType:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、 RELATIVE_TO_PARENT相对于父控件的坐标 
    * fromXValue:x轴的初始值
    * toXType:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标 
    * toXValue:x轴的结束值
    * fromYType:确定y轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标 
    * fromYValue:y轴的初始值
    * toYType:确定y轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标 
    * toYValue:y轴的结束值
    * */
    TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f);
    translateAnimation.setDuration(1000);// 设置动画执行时间
    v.startAnimation(translateAnimation);

AnimationSet(动画集合)

/**
     * 是否使用统一interpolator,即动画的变化速率
     * */
    AnimationSet animationSet = new AnimationSet(true);
    //可以设置动画集合的执行时间,那么单独的动画执行时间就会变为无效
    //animationSet.setDuration(5000);
    AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
    alphaAnimation.setDuration(1000);// 设置动画执行时间
    animationSet.addAnimation(alphaAnimation);
    RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation.setDuration(1000);// 设置动画执行时间
    animationSet.addAnimation(rotateAnimation);
    ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1f, 0, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    scaleAnimation.setDuration(1000);// 设置动画执行时间
    animationSet.addAnimation(scaleAnimation);
    TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f);
    translateAnimation.setDuration(1000);// 设置动画执行时间
    animationSet.addAnimation(translateAnimation);
    v.startAnimation(animationSet);

Interpolator(动画变化速率)

    RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation.setDuration(1000);// 设置动画执行时间
    rotateAnimation.setRepeatCount(1000);// 设置动画执行次数
    /**
     * Interpolator 定义了动画的变化速度,可以实现匀速、正加速、负加速、无规则变加速等
     * AccelerateDecelerateInterpolator 在动画开始与结束的地方速率改变比较慢,在中间的时候加速
     * AnticipateInterpolator 开始的时候向后然后向前甩
     * AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值
     * BounceInterpolator   动画结束的时候弹起
     * CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
     * DecelerateInterpolator 在动画开始的地方快然后慢
     * LinearInterpolator   以常量速率改变
     * OvershootInterpolator    向前甩一定值后再回到原来位置
     * */
    rotateAnimation.setInterpolator(new LinearInterpolator());//匀速不变的
    v.startAnimation(rotateAnimation);

其他常用方法:

**setRepeatMode **设置动作结束后,将怎样处理
默认是Animation.RESTART,回到动作开始时的坐标 。参数应是Animation.RESTART或Animation.REVERSE或Animation.INFINITE。REVERSE是反转的意思,如果repeatCount大于1,repeatMode为Animation.REVERSE那么动作将会回来不断执行,即由a点去到b点后,会从b点和原来的动作相反的相同时间...
**setRepeatCount(repeatCount) **动画执行次数
参数是Animation.INFINITE则表示不断重复该动作,只有在repeatCount大于0或为INFINITE的时候setRepeatMode才会生效。

</br>

帧动画Frame Animation

  • 动画文件
    <?xml version="1.0" encoding="utf-8"?>
    <!-- 
        根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
        根标签下,通过item标签对动画中的每一个图片进行声明
        android:duration 表示展示所用的该图片的时间长度
     -->
    <animation-list
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:oneshot="false"
      >
        <item android:drawable="@drawable/ico0" android:duration="150"></item>
        <item android:drawable="@drawable/ico1" android:duration="150"></item>
        <item android:drawable="@drawable/ico2" android:duration="150"></item>
        <item android:drawable="@drawable/ico3" android:duration="150"></item>
        <item android:drawable="@drawable/ico4" android:duration="150"></item>
        <item android:drawable="@drawable/ico5" android:duration="150"></item>
    </animation-list>
  • 布局文件
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView 
                android:id="@+id/frame_animation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/anima"
                android:layout_centerInParent="true"/>
    </RelativeLayout>
  • 代码
ImageView iv = (ImageView) findViewById(R.id.frame_animation);
AnimationDrawable animationDrawable = (AnimationDrawable) iv.getDrawable();
animationDrawable.start();

相关文章

网友评论

    本文标题:Animation集合(入门)

    本文链接:https://www.haomeiwen.com/subject/jrkwuttx.html