Android动画-属性动画-AnimatorSet

AnimatorSet是对属性动画的一个集合,可以让很多动画按一定顺序或者 同时进行。

概览

先看一下属性动画的结构,View动画的基类是Animation,属性动画的基类是Animator

  • ValueAnimator:这个动画是针对属性的值进行动画的 ,不会对UI造成改变,不能直接实现动画效果。需要通过对动画的监听去做一些操作,在监听中将这个值设置给对应的属性,对应的属性才会改变。
  • ObjectAnimator:直接动画所给的对象,他会调用对象对应属性的get/set方法吧属性的值设置给对象的属性,直接实现动画效果。

简单使用

AnimatorSet主要就是方便整合一大堆动画的,这里先定义两个动画

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnShow,"translationX",0,300);
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(btnShow,"alpha",0.1f,1f);

然后使用AnimatorSet整合起来:

AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(objectAnimator,objectAnimator1);
animatorSet.setDuration(2000);
animatorSet.setInterpolator(new BounceInterpolator());
animatorSet.start();

里面其他方法跟一般的动画没什么区别,Duration可以单独设置也可以一起设置,都设置以AnimatorSet的为准。同样,animatorSet.setTarget也是以AnimatorSet的为准。如果加上一句animatorSet.setTarget(btnStart);那么动画的就是btnStart了,而不是btnShow。

主要是看playSequentially方法,这个方法参数就是要执行的属性动画 ,playSequentially表示按顺序执行这些动画:

如果调用animatorSet.playTogether(objectAnimator,objectAnimator1);表示这些动画一起执行:

AnimatorSet.Builder

AnimatorSet有一个内部类AnimatorSet.Builder,AnimatorSet只提供了playSequentially和playTogether两种方式,而AnimatorSet.Builder可以自由组合各种方式。

AnimatorSet.Builder有四个方法:

方法 解释
after(long delay) 在一定时间后执行play里的动画
after(Animator anim) 先执行after里的动画,再执行play里的动画
before(Animator anim) 先执行play里的动画,再执行after里的动画
with(Animator anim) 一起执行

AnimatorSet.Builder通过调用AnimatorSet的play方法获取。

下面定义两个动画,用after:

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnShow, "translationX", 0, 300);
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(btnShow, "rotation", 0, 360);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(2000);
animatorSet.play(objectAnimator).after(objectAnimator1);
animatorSet.start();

before:

animatorSet.play(objectAnimator).before(objectAnimator1);

with:

animatorSet.play(objectAnimator).with(objectAnimator1);

对AnimatorSet的监听

AnimatorSet内部有一个私有的监听AnimatorSetListener,这个用来他自己处理内部动画。

自己加监听一般是Animator.AnimatorListener

animatorSet.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        Log.i(TAG, "onAnimationStart: ");
                    }
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        Log.i(TAG, "onAnimationEnd: ");
                    }
                    @Override
                    public void onAnimationCancel(Animator animation) {
                        Log.i(TAG, "onAnimationCancel: ");
                    }
                    @Override
                    public void onAnimationRepeat(Animator animation) {
                        Log.i(TAG, "onAnimationRepeat: ");
                    }
                });

这个时候,AnimatorSet中的所有动画成了一个整体,onAnimationEnd是在所有动画完成后才会调用,不管是顺序执行还是同时执行。

XML表示

AnimatorSet在XML中的标签也是<set>,但是也是要把文件定义在res/animator中

如 res/animator/anim_set:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together">
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="360"
        android:propertyName="rotation"/>
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="360"
        android:propertyName="translationX"/>
</set>

java中:

AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(context,R.animator.anim_set);
                animatorSet.setTarget(btnShow);
                animatorSet.setDuration(2000);
                animatorSet.start();

android:orderin

android:orderin有两个值,sequentially和together,分别对应playSequentially和playTogether两个方法的效果。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 写的非常好,强烈推荐给大家 转载请注明出处:http://blog.csdn.net/guolin_blog/ar...
    天天大保建阅读 808评论 0 1
  • Animation Animation类是所有动画(scale、alpha、translate、rotate)的基...
    四月一号阅读 1,936评论 0 10
  • 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们提供了两种实现动画效果...
    Ten_Minutes阅读 3,881评论 3 11
  • 都说青春就是任性的,不然白活一场,这任性不是我们理解的肆意妄为。 有智慧的人将任性理解为拼和闯,...
    4点半的恩赐阅读 205评论 0 0
  • 等车时,拿出手机写上一两句。坐车时,没有座位时听有声书,当听到好的内容时记录在手机里;有座位时,安安静静地写上几句...
    启鸿阅读 303评论 0 0