Android动画总结

本文总结常用属性方法等,详细学习可使用如下郭霖大神文章:

Android属性动画完全解析(上),初识属性动画的基本用法

Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法

Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法

视图动画(View animation)

视图动画分两种子动画Tween animation和Frame animation,下面分别介绍他们的用法。

视图动画(View Animation)之帧动画(Frame Animation)

  • 创建res/anim/sd_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item
        android:drawable="@mipmap/img_1"
        android:duration="80" />
    <item
        android:drawable="@mipmap/img_2"
        android:duration="80" />
    <item
        android:drawable="@mipmap/img_3"
        android:duration="80" />
    ...
</animation-list>
  • xml布局中
 <ImageView
        android:id="@+id/img_show"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="@anim/miao_gif" />
  • 使用
ImageView img_show = (ImageView) findViewById(R.id.img_show);
AnimationDrawable  anim = (AnimationDrawable) img_show.getBackground();
anim.start();//开始帧动画
anim.stop();//停止帧动画

视图动画(View Animation)之补间动画(Tween Animation)

AlphaAnimation(透明度渐变)

anim_alpha.xml:

    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromAlpha="1.0"
        android:toAlpha="0.1"
        android:duration="2000"/>

属性解释:

  • fromAlpha :起始透明度
  • toAlpha:结束透明度
  • 透明度的范围为:0-1,完全透明-完全不透明

ScaleAnimation(缩放渐变)

anim_scale.xml:

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXScale="0.2"
    android:toXScale="1.5"
    android:fromYScale="0.2"
    android:toYScale="1.5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="2000"/>

属性解释:

  • fromXScale/fromYScale:沿着X轴/Y轴缩放的起始比例
  • toXScale/toYScale:沿着X轴/Y轴缩放的结束比例
  • pivotX/pivotY:缩放的中轴点X/Y坐标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点

TranslateAnimation(位移渐变)

anim_translate.xml:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXDelta="0"
    android:toXDelta="320"
    android:fromYDelta="0"
    android:toYDelta="0"
    android:duration="2000"/>

属性解释:

  • fromXDelta/fromYDelta:动画起始位置的X/Y坐标
  • toXDelta/toYDelta:动画结束位置的X/Y坐标

RotateAnimation(旋转渐变)

anim_rotate.xml:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="1000"
    android:repeatCount="1"
    android:repeatMode="reverse"/>

属性解释:

  • fromDegrees/toDegrees:旋转的起始/结束角度
  • repeatCount:旋转的次数,默认值为0,代表一次,假如是其他值,比如3,则旋转4次 另外,值为-1或者infinite时,表示动画永不停止
  • repeatMode:设置重复模式,默认restart,但只有当repeatCount大于0或者infinite或-1时 才有效。还可以设置成reverse,表示偶数次显示动画时会做方向相反的运动!

AnimationSet(组合渐变)

anim_set.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="true" >

    <scale
        android:duration="2000"
        android:fromXScale="0.2"
        android:fromYScale="0.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5" />

    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toDegrees="360" />

    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="320"
        android:toYDelta="0" />

    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />

</set>

Android开发中必定会涉及到动画方面的效果,那么就会遇到一个问题,如果控制动画开始速度,与结束速度。还有其他的一些效果

控制动画速度(动画差值器)

在xml里面设置属性

android:interpolator="@android:anim/accelerate_interpolator" 设置动画为加速动画(动画播放中越来越快)

android:interpolator="@android:anim/decelerate_interpolator" 设置动画为减速动画(动画播放中越来越慢)

android:interpolator="@android:anim/accelerate_decelerate_interpolator" 设置动画为先加速在减速(开始速度最快 逐渐减慢)

android:interpolator="@android:anim/anticipate_interpolator" 先反向执行一段,然后再加速反向回来(相当于我们弹簧,先反向压缩一小段,然后在加速弹出)

android:interpolator="@android:anim/anticipate_overshoot_interpolator" 同上先反向一段,然后加速反向回来,执行完毕自带回弹效果(更形象的弹簧效果)

android:interpolator="@android:anim/bounce_interpolator" 执行完毕之后会回弹跳跃几段(相当于我们高空掉下一颗皮球,到地面是会跳动几下)

android:interpolator="@android:anim/cycle_interpolator" 循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2* mCycles* Math.PI* input)

android:interpolator="@android:anim/linear_interpolator" 线性均匀改变

android:interpolator="@android:anim/overshoot_interpolator" 加速执行,结束之后回弹

在代码中设置,顺序效果同上

animation.setInterpolator(new AccelerateInterpolator());

animation.setInterpolator(new DecelerateInterpolator());

animation.setInterpolator(new AccelerateDecelerateInterpolator());

animation.setInterpolator(new AnticipateInterpolator());

animation.setInterpolator(new AnticipateOvershootInterpolator());

animation.setInterpolator(new BounceInterpolator());

animation.setInterpolator(new CycleInterpolator(2));

animation.setInterpolator(new LinearInterpolator());

animation.setInterpolator(new OvershootInterpolator());

动画不设置Interpolator属性即为默认值,匀速
Interpolator属性也可自定义

动画状态的监听

  • setAnimationListener(new AnimationListener())方法,重写下面的三个方法:
  • onAnimationStart():动画开始
  • onAnimationRepeat():动画重复
  • onAnimationEnd():动画结束

属性动画

Animator ————
         ———— AnimatorSet
         ———— ValueAnimator
                    ———— ObjectAnimator
                    ———— TimeAnimator

AnimatorSet:Animator 子类,用来组合动画
ObjectAnimator:ValueAnimator 子类,

ValueAnimator

  • res/animator/***.xml 中定义ValueAnimator
    语法
    <animator
            android:duration="int"  
            android:valueFrom="float | int | color"
            android:valueTo="float | int | color"
            android:startOffset="int"
            android:repeatCount="int"
            android:repeatMode=["repeat" | "reverse"]
            android:valueType=["intType" | "floatType"]/>

使用

        //使用xml属性动画
        ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.valueanimator);
        //动画监听
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer animatedValue = (Integer) animation.getAnimatedValue();
                view.scrollTo(animatedValue,0);
            }
        });
        valueAnimator.start();
  • Java属性介绍
        //动画从0到10再到5再到0
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 10, 5, 0);
        //ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 10, 5, 0);
        /*
        ofObject不同于ofFloat和ofInt,多了TypeEvaluator参数;其实ofInt和ofFloat使用的是系统默认的IntEvaluator和FloatEvaluator,而ofObject中参数TypeEvaluator需要我们自定义
        */
        //ValueAnimator.ofObject(TypeEvaluator evaluator, Object... values);后文介绍
        //动画中每一帧更新的时候调用
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                final float animatedValue = (float) animation.getAnimatedValue();
            }
        });
        //动画的监听
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            //动画取消
            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
            }

            //动画结束
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }

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

            //动画开始
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }

            //动画暂停
            @Override
            public void onAnimationPause(Animator animation) {
                super.onAnimationPause(animation);
            }

            //动画重启
            @Override
            public void onAnimationResume(Animator animation) {
                super.onAnimationResume(animation);
            }
        });
        //动画执行时间
        valueAnimator.setDuration(500);
        //动画重复次数
        valueAnimator.setRepeatCount(Integer.MAX_VALUE);
        /*
        动画循环模式,有两种取值:
        ValueAnimator.RESTART:重新开始
        ValueAnimator.REVERSE:反向重新开始
         */
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
        //动画差值器
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        //设置动画延迟播放
        valueAnimator.setStartDelay(1000);
        //开始播放动画
        valueAnimator.start();
  • TypeEvaluator

ObjectAnimator

  • 属性介绍
    ObjectAnimator 使用与 唯一不同在于
        /**
         第二个参数 propertyName 说明:
         alpha:透明度
         rotation:旋转
         rotationX:围绕x轴旋转
         rotationY:围绕y轴旋转
         translationX:在x轴平移
         translationY:在y轴平移
         scaleX:在x轴缩放
         scaleY:在y轴缩放
         backgroundColor:可以修改控件背景颜色
         等等
         */
        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1, 10, 10, 5, 1);
        alphaAnimator.setDuration(2000);
        alphaAnimator.setRepeatMode(ValueAnimator.REVERSE);
        alphaAnimator.setRepeatCount(10);
        alphaAnimator.setInterpolator(new DecelerateInterpolator());
        alphaAnimator.start();
        alphaAnimator.setStartDelay(1000);

了解更多 资源收集ResourceCollection

posted @ 2018-02-24 13:32  SiberiaDante  阅读(829)  评论(0编辑  收藏  举报