一次Android动画工具类的封装

起因

最近在做一个组件化框架的封装,现在开发到一些常用工具类的封装了,突然意识到好像还没有做动画的工具类,于是开始着手开发之。

思路

既然要做动画,肯定是要做属性动画的工具类的封装了,由于补间动画和逐帧动画并不能改变目标动画主题的实际属性,在Android的开发中已经越来越少人去用这两个动画框架做开发了,而属性动画则相对的越来越广泛的使用在开发过程中了,于是这次的工具类的封装,只针对属性动画来封装。

属性动画对应的类叫做ObjectAnimator,主要就是用这个类来实现动画的一些基础设置,其具体的使用方式我就不写了,有兴趣的朋友可以自行学习属性动画的相关知识。

封装属性动画工具类不可避免的还要考虑到属性动画的组合播放动画的需求,而属性动画的组合播放有大约三种方式:

1.使用AnimatorSet的Builder来组合播放
​ AnimatorSet.Builder是一个使用的动画工具类,用于方便向AnimatorSet添加动画以及设置各种动画之间的关系。在 AnimatorSet.Builder中,共声明了after(long)、after(Animator)、before(Animator)、with(Animator)等四个方法。

  • after(delay) 设置动画延迟delay时间后播放
  • after(anim) 设置在anim动画结束后播放此动画
  • before(anim) 设置此动画早于anim播放
  • with(anim) 设置此动画与anim一起播放

然后再调用paly(anim)方法来链式调用动画

1
2
AnimatorSet set=new AnimatorSet();
set.play(anim1).before(anim2).with(anim3).after(anim4);

我们注意到他是先执行的after,然后是play和with同时执行,最后执行的before。所以大家记住这个顺序,无论怎么写,都是这个执行顺序。

2.使用AnimatorSet的playSequentially

API

playSequentially(List items):添加一组动画,播放顺序为逐一播放
playSequentially(Animator… items):添加一组动画,播放顺序为逐一播放

1
2
3
4
5
6
7
8
9
AnimatorSet bouncer = new AnimatorSet();
ObjectAnimator objectAnimatorA = ObjectAnimator.ofFloat(btnProperty, PropertyConstant.PROPERTY_TRANSLATION_X, 0f, 300f);
ObjectAnimator objectAnimatorB = ObjectAnimator.ofFloat(btnProperty, PropertyConstant.PROPERTY_TRANSLATION_Y, 0f, 300f);
ObjectAnimator objectAnimatorC = ObjectAnimator.ofFloat(btnProperty, PropertyConstant.PROPERTY_ROTATION, 0f, 360f);

bouncer.playSequentially(objectAnimatorA, objectAnimatorB, objectAnimatorC);

bouncer.setDuration(6000);
bouncer.start();

3.使用AnimatorSet的palyTogether

API

playTogether(Collection items):添加一组动画,播放顺序为一起播放
playTogether(Animator… items):添加一组动画,播放顺序为一起播放

1
2
3
4
5
6
7
8
9
AnimatorSet bouncer = new AnimatorSet();
ObjectAnimator objectAnimatorA = ObjectAnimator.ofFloat(btnProperty, PropertyConstant.PROPERTY_TRANSLATION_X, 0f, 300f);
ObjectAnimator objectAnimatorB = ObjectAnimator.ofFloat(btnProperty, PropertyConstant.PROPERTY_TRANSLATION_Y, 0f, 300f);
ObjectAnimator objectAnimatorC = ObjectAnimator.ofFloat(btnProperty, PropertyConstant.PROPERTY_ROTATION, 0f, 360f);

bouncer.playSequentially(objectAnimatorA, objectAnimatorB, objectAnimatorC);

bouncer.setDuration(6000);
bouncer.start();

掌握以上的知识点后,我的思路是,其实最后就是对执行方式的封装,所谓的执行方式就是如何正常的调用play,playSequentially和playTogether三个方法,这里需要合理的封装。

还有就是对于监听接口的封装,每个ObjectAnimator都有三个接口:

  • Animator.AnimatorListener 对整个动画生命周期的监听

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    anim.addListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animator) {
    Toast.makeText(MainActivity.this, "start", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onAnimationEnd(Animator animator) {
    Toast.makeText(MainActivity.this, "End", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onAnimationCancel(Animator animator) {
    Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onAnimationRepeat(Animator animator) {
    Toast.makeText(MainActivity.this, "rapeat", Toast.LENGTH_LONG).show();
    }
    });
    anim.start();
  • ValueAnimator.AnimatorUpdateListener 对于该动画逐帧的监听

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ValueAnimator vanim = ValueAnimator.ofInt(0,10,20);
    vanim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {

    //如果之前的ValueAnimtor指定的是Int的i话,那么返回的Value就是int类型,
    也就是返回值类型与你创建的类型一致
    int value = (int) valueAnimator.getAnimatedValue();
    }
    });
  • Animator.AnimatorPauseListener 对于该动画的暂停和播放的监听

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    new Animator.AnimatorPauseListener() {
    @Override
    public void onAnimationPause(Animator animator) {

    }

    @Override
    public void onAnimationResume(Animator animator) {

    }
    }

    由于我的初步构想是使用建造者模式的链式调用模式来设计我的工具类,如果按照普通的写法,那么整个监听接口的设置将会是灾难性的,因为所有的监听接口的设置都是混乱的,所以这里必须处理,我的思路是,学习SpringSecurity的链式调用设计,为每个类型的监听设置自己的类,然后再让工具主类调用该类型的监听接口,然后设置完毕后,在通过该监听接口类的and()方法回到工具类的主类型来,这样在链式调用的时候就有一个起止顺序,不会混乱执行了,而且如果不用设置监听,不调用监听类设置也不会影响主类的执行。

    截取关键代码,以Play方法的监听接口设置为例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    /**
    *工具类的主类
    **/

    public static class AnimatorSetWrap{
    PlayAnimationListener playListener;
    public PlayAnimationListener toAddPlayListener(){
    playListener=new PlayAnimationListener(this);
    return playListener;
    }
    }

    /**
    * Play方法对应的ObjectAnimator的监听实例
    */

    public static class PlayAnimationListener implements IAnimatorListener<PlayAnimationListener>{

    private Animator.AnimatorListener animatorListener;
    private ValueAnimator.AnimatorUpdateListener updateListener;
    private Animator.AnimatorPauseListener pauseListener;

    public AnimatorSetWrap animatorSetWrap;
    public PlayAnimationListener(AnimatorSetWrap animatorSetWrap){
    this.animatorSetWrap=animatorSetWrap;
    }
    @Override
    public PlayAnimationListener setAnimatorListener(Animator.AnimatorListener animatorListener) {
    this.animatorListener=animatorListener;
    return this;
    }

    @Override
    public PlayAnimationListener setUpdateListener(ValueAnimator.AnimatorUpdateListener animatorListener) {
    this.updateListener=animatorListener;
    return this;
    }

    @Override
    public PlayAnimationListener setPauseListener(Animator.AnimatorPauseListener animatorListener) {
    this.pauseListener=animatorListener;
    return this;
    }
    @Override
    public AnimatorSetWrap and(){
    return animatorSetWrap;
    }
    }

    /**
    * 动画监听的公用模板接口
    * @param <T>
    */

    interface IAnimatorListener<T>{
    /**
    * 设置AnimatorListener的方法
    * @param listener
    * @return
    */

    T setAnimatorListener(Animator.AnimatorListener listener);

    /**
    * 设置AnimatorUpdateListener的方法
    * @param listener
    * @return
    */

    T setUpdateListener(ValueAnimator.AnimatorUpdateListener listener);

    /**
    * 设置AnimatorPauseListener的方法
    * @param listener
    * @return
    */

    T setPauseListener(Animator.AnimatorPauseListener listener);

    /**
    * 桥接动画监听与动画工具类的方法
    * @return
    */

    AnimatorSetWrap and();
    }

    具体的使用方法:

    1
    2
    3
    4
    5
    6
    AnimatorUtil.AnimatorSetWrap  animatorSetWrapDemo=new AnimatorSetWrap().toAddPlayListener().setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
    LogUtils.e("数值:"+valueAnimator.getAnimatedValue());
    }
    }).and();

    通过这种链式调用,只要调用到and()方法就又回到了AnimatorSetWrap工具类的实例,剩下就可以继续调用其他动画的方法并播放动画了。

    代码

    说了这么多,就把我的工具类代码分享给大家吧,可能还不完善,有什么问题大家一起探讨:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    import android.animation.Animator;
    import android.animation.AnimatorSet;
    import android.animation.ObjectAnimator;
    import android.animation.TimeInterpolator;
    import android.animation.ValueAnimator;
    import android.os.Build;
    import android.support.annotation.Nullable;
    import android.support.annotation.Size;
    import android.view.View;
    import android.view.animation.LinearInterpolator;

    import java.util.ArrayList;
    import java.util.List;

    /**
    * 动画工具类
    *@package com.dhcc.commonutils
    *@author jasoncool
    *@createDate 2018/11/20 16:16
    *@description
    **/

    public class AnimatorUtils {

    public static final String ALPHA="Alpha";
    public static final String TRANSX="TranslationX";
    public static final String TRANSY="TranslationY";
    public static final String SCALEX="ScaleX";
    public static final String SCALEY="ScaleY";
    public static final String ROTATION="Rotation";
    public static final String ROTATIONX="RotationX";
    public static final String ROTATIONY="RotationY";

    /**
    * 默认的TimeInterpolator,前后减速,中间加速
    */

    private static final TimeInterpolator sDefaultInterpolator =
    new LinearInterpolator();

    public static AnimatorSetWrap createAnimator() {
    return new AnimatorSetWrap();
    }

    /**
    * @param interpolator 默认的TimeInterpolator
    * @return
    */

    public static AnimatorSetWrap createAnimator(TimeInterpolator interpolator) {
    return new AnimatorSetWrap(interpolator);
    }

    /**
    * 属性动画组合
    * 属性动画组合对应的是AnimatorSet类,我们只需要new他就好。
    *
    * 它对应的主要有这四个方法,play,before,with,after。
    * 这四个方法里面全都是填入往后儿们的animator类,
    * 但是先后执行顺序不一样,分别对应着开启,最后,同步,最开始执行。
    * 我们注意到他是先执行的after,然后是play和with同时执行,最后执行的before。
    * 所以大家记住这个顺序,无论怎么写,都是这个执行顺序。
    *
    */

    public static class AnimatorSetWrap{

    private View mView;
    /**
    * 不设置默认插值器时,工具类自带的默认插值器
    */

    private TimeInterpolator mTimeInterpolator;
    /**
    * 判断play方法只允许执行一次的布尔值
    */

    boolean mIsPlaying=false;
    /**
    * 联合动画的动画容器
    */

    private AnimatorSet mAnimatorSet;
    /**
    * 联合动画的动画构造器
    */

    private AnimatorSet.Builder mAnimatorBuilder;
    /**
    * 默认执行时间
    */

    private int mDuration=1000;
    /**
    * play的监听器类
    */

    PlayAnimationListener playListener;
    /**
    * before的监听器类
    */

    BeforeAnimationListener beforeListener;
    /**
    * with的监听器类
    */

    WithAnimationListener withListener;
    /**
    * after的监听器类
    */

    AfterAnimationListener afterListener;
    /**
    * then的监听器类
    */

    ThenAnimationListener thenListener;
    /**
    * 顺序播放或者同时播放时存储动画的列表容器
    */

    List<Animator> mAnimatorList;
    /**
    * 是否已经初始化then动画
    */

    boolean mHasInitThenAnim=false;

    private AnimatorSetWrap(){
    this(sDefaultInterpolator);
    }

    /**
    * 构造方法
    * 主要是负责
    * 1.初始化默认的插值器 mTimeInterpolator
    * 2.初始化联合动画Set mAnimatorSet
    * 3.初始化顺序或同时播放动画容器 mAnimatorList
    * @param interpolator
    */

    private AnimatorSetWrap(TimeInterpolator interpolator) {
    mTimeInterpolator = interpolator;
    mAnimatorSet = new AnimatorSet();
    mAnimatorList=new ArrayList<>(16);
    }

    /**
    * Play动画的监听启动方法
    * 如果要监听play动画先调用这个方法
    * @return
    */

    public PlayAnimationListener toAddPlayListener(){
    playListener=new PlayAnimationListener(this);
    return playListener;
    }
    /**
    * Before动画的监听启动方法
    * 如果要监听Before动画先调用这个方法
    * @return
    */

    public BeforeAnimationListener toAddBeforeListener(){
    beforeListener=new BeforeAnimationListener(this);
    return beforeListener;
    }
    /**
    * With动画的监听启动方法
    * 如果要监听With动画先调用这个方法
    * @return
    */

    public WithAnimationListener toAddWithListener(){
    withListener=new WithAnimationListener(this);
    return withListener;
    }
    /**
    * After动画的监听启动方法
    * 如果要监听After动画先调用这个方法
    * @return
    */

    public AfterAnimationListener toAddAfterListener(){
    afterListener=new AfterAnimationListener(this);
    return afterListener;
    }

    /**
    * 顺序或同时播放动画执行时的监听方法
    * 要先于Then方法进行调用
    * @return
    */

    public ThenAnimationListener toAddThenListener(){
    thenListener=new ThenAnimationListener(this);
    return thenListener;
    }

    /**
    * 顺序或者同时播放动画时的调用方法
    * 在其内部生成一个Animator并将该Animator加入到mAnimatorList中备用
    * @param view 动画执行的主体View
    * @param animName 动画类型
    * @param interpolator 动画插值器 如果不设置就用默认的
    * @param repeatCount 重复次数
    * @param duration 执行时间
    * @param values 动画执行的值
    * @return
    */

    public AnimatorSetWrap then(View view, String animName, @Nullable TimeInterpolator interpolator, @Size(min = 0,max=Integer.MAX_VALUE) int repeatCount, @Size(min = 0,max=Integer.MAX_VALUE) int duration, float... values){
    LogUtils.e("addThen");
    if(view==null){
    throw new RuntimeException("view 不能为空");
    }
    mIsPlaying = true;
    mView = view;
    ObjectAnimator thenAnimator = ObjectAnimator.ofFloat(view,animName,values);
    thenAnimator.setInterpolator(interpolator==null?mTimeInterpolator:interpolator);
    thenAnimator.setRepeatCount(repeatCount<0?0:repeatCount);
    thenAnimator.setDuration(duration<0?mDuration:duration);
    if (thenListener!=null&&thenListener.animatorListener != null) {
    thenAnimator.addListener(thenListener.animatorListener);
    }
    if(thenListener!=null&&thenListener.updateListener!=null){
    thenAnimator.addUpdateListener(thenListener.updateListener);
    }
    if(thenListener!=null&&thenListener.pauseListener!=null){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    thenAnimator.addPauseListener(thenListener.pauseListener);
    }else{
    throw new RuntimeException("SDK最小要求19");
    }
    }
    mAnimatorList.add(thenAnimator);
    return this;
    }

    public AnimatorSetWrap then(Animator animator) {
    mAnimatorList.add(animator);
    return this;
    }

    public AnimatorSetWrap then(AnimatorSetWrap animator) {
    mAnimatorList.add(animator.getAnimatorSet());
    return this;
    }

    /**
    * AnimatorSet的Play方法,整个动画过程只能调用一次
    * 并且一旦执行play方法将会清除掉mAnimatorList中存储的顺序或同时执行的方法实例
    * @param view 方法主体
    * @param animName 动画类型
    * @param interpolator 插值器
    * @param repeatCount 重复次数
    * @param duration 动画时长
    * @param values 动画执行值
    * @return
    */

    public AnimatorSetWrap play(View view, String animName, @Nullable TimeInterpolator interpolator, @Size(min = 0,max=Integer.MAX_VALUE) int repeatCount, @Size(min = 0,max=Integer.MAX_VALUE) int duration, float... values){
    LogUtils.e("play");
    if(mIsPlaying){
    throw new RuntimeException("AnimatorSetWrap.play()方法只能调用一次");
    }
    if(view==null){
    throw new RuntimeException("view 不能为空");
    }
    mIsPlaying = true;
    mView = view;
    ObjectAnimator playAnimator = ObjectAnimator.ofFloat(view,animName,values);
    playAnimator.setInterpolator(interpolator==null?mTimeInterpolator:interpolator);
    playAnimator.setRepeatCount(repeatCount<0?0:repeatCount);
    playAnimator.setDuration(duration<0?mDuration:duration);
    if (playListener!=null&&playListener.animatorListener != null) {
    playAnimator.addListener(playListener.animatorListener);
    }
    if(playListener!=null&&playListener.updateListener!=null){
    playAnimator.addUpdateListener(playListener.updateListener);
    }
    if(playListener!=null&&playListener.pauseListener!=null){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    playAnimator.addPauseListener(playListener.pauseListener);
    }else{
    throw new RuntimeException("SDK最小要求19");
    }
    }
    mAnimatorList.clear();
    mAnimatorBuilder=mAnimatorSet.play(playAnimator);
    return this;
    }

    public AnimatorSetWrap play(Animator animator) {
    mAnimatorList.clear();
    mAnimatorBuilder = mAnimatorSet.play(animator);
    return this;
    }

    public AnimatorSetWrap play(AnimatorSetWrap animator) {
    mAnimatorList.clear();
    mAnimatorBuilder = mAnimatorSet.play(animator.getAnimatorSet());
    return this;
    }

    /**
    * AnimatorSet的Before方法
    * @param view 动画执行的主体View
    * @param animName 动画类型
    * @param interpolator 插值器
    * @param repeatCount 重复次数
    * @param duration 动画执行时长
    * @param values 动画执行数值
    * @return
    */

    public AnimatorSetWrap before(View view, String animName,@Nullable TimeInterpolator interpolator, @Size(min = 0,max=Integer.MAX_VALUE) int repeatCount,@Size(min = 0,max=Integer.MAX_VALUE)int duration, float... values){
    LogUtils.e("before");
    if(view==null){
    throw new RuntimeException("view 不能为空");
    }
    ObjectAnimator beforeAnimator = ObjectAnimator.ofFloat(view,
    animName, values).setDuration(duration);
    beforeAnimator.setInterpolator(interpolator==null?mTimeInterpolator:interpolator);
    beforeAnimator.setRepeatCount(repeatCount<0?0:repeatCount);
    beforeAnimator.setDuration(duration<0?mDuration:duration);
    if (beforeListener!=null&&beforeListener.animatorListener != null) {
    beforeAnimator.addListener(beforeListener.animatorListener);
    }
    if(beforeListener!=null&&beforeListener.updateListener!=null){
    beforeAnimator.addUpdateListener(beforeListener.updateListener);
    }
    if(beforeListener!=null&&beforeListener.pauseListener!=null){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    beforeAnimator.addPauseListener(beforeListener.pauseListener);
    }else{
    throw new RuntimeException("SDK最小要求19");
    }
    }
    mAnimatorBuilder = mAnimatorBuilder.before(beforeAnimator);
    return this;
    }

    public AnimatorSetWrap before(Animator animator) {
    mAnimatorBuilder = mAnimatorBuilder.before(animator);
    return this;
    }

    public AnimatorSetWrap before(AnimatorSetWrap animator) {
    mAnimatorBuilder = mAnimatorBuilder.before(animator.getAnimatorSet());
    return this;
    }


    public AnimatorSetWrap with(View view, String animName,@Nullable TimeInterpolator interpolator,@Size(min = 0,max=Integer.MAX_VALUE) int repeatCount,@Size(min = 0,max=Integer.MAX_VALUE)int duration, float... values){
    LogUtils.e("with");
    if(view==null){
    throw new RuntimeException("view 不能为空");
    }
    ObjectAnimator withAnimator = ObjectAnimator.ofFloat(view,
    animName, values).setDuration(duration);
    withAnimator.setInterpolator(interpolator==null?mTimeInterpolator:interpolator);
    withAnimator.setRepeatCount(repeatCount<0?0:repeatCount);
    withAnimator.setDuration(duration<0?mDuration:duration);
    if (withListener!=null&&withListener.animatorListener != null) {
    withAnimator.addListener(withListener.animatorListener);
    }
    if(withListener!=null&&withListener.updateListener!=null){
    withAnimator.addUpdateListener(withListener.updateListener);
    }
    if(withListener!=null&&withListener.pauseListener!=null){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    withAnimator.addPauseListener(withListener.pauseListener);
    }else{
    throw new RuntimeException("SDK最小要求19");
    }
    }
    mAnimatorBuilder = mAnimatorBuilder.with(withAnimator);
    return this;
    }

    public AnimatorSetWrap with(Animator animator) {
    mAnimatorBuilder = mAnimatorBuilder.with(animator);
    return this;
    }

    public AnimatorSetWrap with(AnimatorSetWrap animator) {
    mAnimatorBuilder = mAnimatorBuilder.with(animator.getAnimatorSet());
    return this;
    }



    public AnimatorSetWrap after(View view, String animName,@Nullable TimeInterpolator interpolator,@Size(min = 0,max=Integer.MAX_VALUE) int repeatCount,@Size(min = 0,max=Integer.MAX_VALUE) int duration, float... values){
    LogUtils.e("after");
    if(view==null){
    throw new RuntimeException("view 不能为空");
    }
    ObjectAnimator afterAnimator = ObjectAnimator.ofFloat(view,
    animName, values).setDuration(duration);
    afterAnimator.setInterpolator(interpolator==null?mTimeInterpolator:interpolator);
    afterAnimator.setRepeatCount(repeatCount<0?0:repeatCount);
    afterAnimator.setDuration(duration<0?mDuration:duration);
    if (afterListener!=null&&afterListener.animatorListener != null) {
    afterAnimator.addListener(afterListener.animatorListener);
    }
    if(afterListener!=null&&afterListener.updateListener!=null){
    afterAnimator.addUpdateListener(afterListener.updateListener);
    }
    if(afterListener!=null&&afterListener.pauseListener!=null){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    afterAnimator.addPauseListener(afterListener.pauseListener);
    }else{
    throw new RuntimeException("SDK最小要求19");
    }
    }
    mAnimatorBuilder = mAnimatorBuilder.after(afterAnimator);
    return this;
    }

    public AnimatorSetWrap after(Animator animator) {
    mAnimatorBuilder = mAnimatorBuilder.after(animator);
    return this;
    }

    public AnimatorSetWrap after(AnimatorSetWrap animator) {
    mAnimatorBuilder = mAnimatorBuilder.after(animator.getAnimatorSet());
    return this;
    }


    public AnimatorSetWrap after(long delay) {
    mAnimatorBuilder.after(delay);
    return this;
    }

    /**
    * 直接执行动画,该动画操作主要用作执行AnimatorSet的组合动画
    * 如果mAnimatorList不为0 则执行逐一播放动画
    */

    public void playAnim() {
    if(mAnimatorList.size()>0){
    readyThen(true);
    }
    mAnimatorSet.start();
    }

    /**
    * 在一定时长内运行完该组合动画
    * 如果mAnimatorList不为0 则执行逐一播放动画
    * @param duration 动画时长
    */

    public void playAnim(long duration) {
    if(mAnimatorList.size()>0){
    readyThen(true);
    }
    mAnimatorSet.setDuration(duration);
    mAnimatorSet.start();
    }

    /**
    * 延迟一定时长播放动画
    * 如果mAnimatorList不为0 则执行逐一播放动画
    * @param delay 延迟时长
    */

    public void playAnimDelay(long delay) {
    if(mAnimatorList.size()>0){
    readyThen(true);
    }
    mAnimatorSet.setStartDelay(delay);
    mAnimatorSet.start();
    }

    /**
    * 直接执行动画,该动画操作主要用作执行AnimatorSet的组合动画
    */

    public void playAnim(boolean isSequentially) {
    readyThen(isSequentially);
    mAnimatorSet.start();
    }

    /**
    * 在一定时长内运行完该组合动画
    * @param duration 动画时长
    */

    public void playAnim(boolean isSequentially,long duration) {
    readyThen(isSequentially);
    mAnimatorSet.setDuration(duration);
    mAnimatorSet.start();
    }

    /**
    * 延迟一定时长播放动画
    * @param delay 延迟时长
    */

    public void playAnimDelay(boolean isSequentially,long delay) {
    readyThen(isSequentially);
    mAnimatorSet.setStartDelay(delay);
    mAnimatorSet.start();
    }

    /**
    * 顺序播放动画
    * @param isSequentially 是逐一播放还是同时播放
    */

    private void readyThen(boolean isSequentially){
    // 只在第一次启动时初始化
    if (mHasInitThenAnim) {
    return;
    }
    mHasInitThenAnim = true;
    if (mAnimatorList.size() > 0) {
    AnimatorSet set = new AnimatorSet();
    if(isSequentially){
    set.playSequentially(mAnimatorList);
    }else{
    set.playTogether(mAnimatorList);
    }
    mAnimatorBuilder.before(set);
    }
    }
    /**
    * 取消动画
    */

    public void cancel() {
    mAnimatorSet.cancel();
    mAnimatorList.clear();
    }

    /**
    * 获取AnimatorSet的实例
    * @return
    */

    private AnimatorSet getAnimatorSet() {
    return mAnimatorSet;
    }

    /**
    * 为AnimatorSet设置监听
    * @param listener
    * @return
    */

    public AnimatorSetWrap setAnimatorSetListener(Animator.AnimatorListener listener) {
    mAnimatorSet.addListener(listener);
    return this;
    }

    /**
    * 取消AnimatorSet的监听
    * @param listener
    */

    public void removeSetListner(Animator.AnimatorListener listener) {
    mAnimatorSet.removeListener(listener);
    }

    /**
    * 取消全部AnimatorSet的监听
    */

    public void removeAllLSetisteners() {
    mAnimatorSet.removeAllListeners();
    }

    /**
    * 判断一个View是否在当前的屏幕中可见(肉眼真实可见)
    * @param mView
    * @return 返回true则可见
    */

    public static boolean isVisibleOnScreen(View mView) {
    if (mView == null) {
    return false;
    }
    return mView.getWindowVisibility() == View.VISIBLE
    && mView.getVisibility() == View.VISIBLE && mView.isShown();
    }
    }

    /**
    * Play方法对应的ObjectAnimator的监听实例
    */

    public static class PlayAnimationListener implements IAnimatorListener<PlayAnimationListener>{

    private Animator.AnimatorListener animatorListener;
    private ValueAnimator.AnimatorUpdateListener updateListener;
    private Animator.AnimatorPauseListener pauseListener;

    public AnimatorSetWrap animatorSetWrap;
    public PlayAnimationListener(AnimatorSetWrap animatorSetWrap){
    this.animatorSetWrap=animatorSetWrap;
    }
    @Override
    public PlayAnimationListener setAnimatorListener(Animator.AnimatorListener animatorListener) {
    this.animatorListener=animatorListener;
    return this;
    }

    @Override
    public PlayAnimationListener setUpdateListener(ValueAnimator.AnimatorUpdateListener animatorListener) {
    this.updateListener=animatorListener;
    return this;
    }

    @Override
    public PlayAnimationListener setPauseListener(Animator.AnimatorPauseListener animatorListener) {
    this.pauseListener=animatorListener;
    return this;
    }
    @Override
    public AnimatorSetWrap and(){
    return animatorSetWrap;
    }
    }


    public static class BeforeAnimationListener implements IAnimatorListener<BeforeAnimationListener>{

    private Animator.AnimatorListener animatorListener;
    private ValueAnimator.AnimatorUpdateListener updateListener;
    private Animator.AnimatorPauseListener pauseListener;

    public AnimatorSetWrap animatorSetWrap;
    public BeforeAnimationListener(AnimatorSetWrap animatorSetWrap){
    this.animatorSetWrap=animatorSetWrap;
    }
    @Override
    public AnimatorSetWrap and() {
    return animatorSetWrap;
    }

    @Override
    public BeforeAnimationListener setAnimatorListener(Animator.AnimatorListener listener) {
    this.animatorListener=listener;
    return this;
    }

    @Override
    public BeforeAnimationListener setUpdateListener(ValueAnimator.AnimatorUpdateListener listener) {
    this.updateListener=listener;
    return this;
    }

    @Override
    public BeforeAnimationListener setPauseListener(Animator.AnimatorPauseListener listener) {
    this.pauseListener=listener;
    return this;
    }
    }


    public static class WithAnimationListener implements IAnimatorListener<WithAnimationListener>{

    private Animator.AnimatorListener animatorListener;
    private ValueAnimator.AnimatorUpdateListener updateListener;
    private Animator.AnimatorPauseListener pauseListener;

    public AnimatorSetWrap animatorSetWrap;
    public WithAnimationListener(AnimatorSetWrap animatorSetWrap){
    this.animatorSetWrap=animatorSetWrap;
    }
    @Override
    public AnimatorSetWrap and() {
    return animatorSetWrap;
    }

    @Override
    public WithAnimationListener setAnimatorListener(Animator.AnimatorListener listener) {
    this.animatorListener=listener;
    return this;
    }

    @Override
    public WithAnimationListener setUpdateListener(ValueAnimator.AnimatorUpdateListener listener) {
    this.updateListener=listener;
    return this;
    }

    @Override
    public WithAnimationListener setPauseListener(Animator.AnimatorPauseListener listener) {
    this.pauseListener=listener;
    return this;
    }
    }

    public static class AfterAnimationListener implements IAnimatorListener<AfterAnimationListener>{

    private Animator.AnimatorListener animatorListener;
    private ValueAnimator.AnimatorUpdateListener updateListener;
    private Animator.AnimatorPauseListener pauseListener;

    public AnimatorSetWrap animatorSetWrap;
    public AfterAnimationListener(AnimatorSetWrap animatorSetWrap){
    this.animatorSetWrap=animatorSetWrap;
    }
    @Override
    public AnimatorSetWrap and() {
    return animatorSetWrap;
    }

    @Override
    public AfterAnimationListener setAnimatorListener(Animator.AnimatorListener listener) {
    this.animatorListener=listener;
    return this;
    }

    @Override
    public AfterAnimationListener setUpdateListener(ValueAnimator.AnimatorUpdateListener listener) {
    this.updateListener=listener;
    return this;
    }

    @Override
    public AfterAnimationListener setPauseListener(Animator.AnimatorPauseListener listener) {
    this.pauseListener=listener;
    return this;
    }
    }


    public static class ThenAnimationListener implements IAnimatorListener<ThenAnimationListener>{

    private Animator.AnimatorListener animatorListener;
    private ValueAnimator.AnimatorUpdateListener updateListener;
    private Animator.AnimatorPauseListener pauseListener;

    public AnimatorSetWrap animatorSetWrap;
    public ThenAnimationListener(AnimatorSetWrap animatorSetWrap){
    this.animatorSetWrap=animatorSetWrap;
    }
    @Override
    public AnimatorSetWrap and() {
    return animatorSetWrap;
    }

    @Override
    public ThenAnimationListener setAnimatorListener(Animator.AnimatorListener listener) {
    this.animatorListener=listener;
    return this;
    }

    @Override
    public ThenAnimationListener setUpdateListener(ValueAnimator.AnimatorUpdateListener listener) {
    this.updateListener=listener;
    return this;
    }

    @Override
    public ThenAnimationListener setPauseListener(Animator.AnimatorPauseListener listener) {
    this.pauseListener=listener;
    return this;
    }
    }

    /**
    * 动画监听的公用模板接口
    * @param <T>
    */

    interface IAnimatorListener<T>{
    /**
    * 设置AnimatorListener的方法
    * @param listener
    * @return
    */

    T setAnimatorListener(Animator.AnimatorListener listener);

    /**
    * 设置AnimatorUpdateListener的方法
    * @param listener
    * @return
    */

    T setUpdateListener(ValueAnimator.AnimatorUpdateListener listener);

    /**
    * 设置AnimatorPauseListener的方法
    * @param listener
    * @return
    */

    T setPauseListener(Animator.AnimatorPauseListener listener);

    /**
    * 桥接动画监听与动画工具类的方法
    * @return
    */

    AnimatorSetWrap and();
    }

    }

    使用方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    AnimatorUtils.createAnimator().play(viewAnimator,AnimatorUtils.ROTATIONY,null,0,1000,0,360).toAddThenListener().setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
    LogUtils.e("then1:"+valueAnimator.getAnimatedValue());
    }
    }).and().then(viewAnimator, AnimatorUtils.TRANSY, null, -1, -2, 0, 100, 200, 300, 200, 100, 0).toAddThenListener().setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
    LogUtils.e("then2:"+valueAnimator.getAnimatedValue());
    }
    }).and().then(viewAnimator, AnimatorUtils.SCALEX, new LinearInterpolator(), 0, 1000, 0, 10).toAddWithListener().setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
    LogUtils.e("with1:"+valueAnimator.getAnimatedValue());
    }
    }).and().with(viewAnimator, AnimatorUtils.SCALEY, new LinearInterpolator(), 0, 1000, 0, 10).toAddWithListener().setAnimatorListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animator) {
    LogUtils.e("with2:onAnimationStart");
    }

    @Override
    public void onAnimationEnd(Animator animator) {

    }

    @Override
    public void onAnimationCancel(Animator animator) {

    }

    @Override
    public void onAnimationRepeat(Animator animator) {

    }
    }).and().with(viewAnimator, AnimatorUtils.ALPHA, new LinearInterpolator(), 0, 1000, 1, 0,1)
    //.playSequentially(2000);
    .playAnim();

    上面的动画调用方法是我乱写的,具体就是为了演示工具类的使用方法,你可以改成自己的调用方法。

热评文章