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)方法来链式调用动画
AnimatorSetset=newAnimatorSet(); set.play(anim1).before(anim2).with(anim3).after(anim4);
我们注意到他是先执行的after,然后是play和with同时执行,最后执行的before。所以大家记住这个顺序,无论怎么写,都是这个执行顺序。
2.使用AnimatorSet的playSequentially
API
- playSequentially(Listitems):添加一组动画,播放顺序为逐一播放
- playSequentially(Animator…items):添加一组动画,播放顺序为逐一播放
AnimatorSetbouncer=newAnimatorSet(); ObjectAnimatorobjectAnimatorA=ObjectAnimator.ofFloat(btnProperty,PropertyConstant.PROPERTY_TRANSLATION_X,0f,300f); ObjectAnimatorobjectAnimatorB=ObjectAnimator.ofFloat(btnProperty,PropertyConstant.PROPERTY_TRANSLATION_Y,0f,300f); ObjectAnimatorobjectAnimatorC=ObjectAnimator.ofFloat(btnProperty,PropertyConstant.PROPERTY_ROTATION,0f,360f); bouncer.playSequentially(objectAnimatorA,objectAnimatorB,objectAnimatorC); bouncer.setDuration(6000); bouncer.start();
3.使用AnimatorSet的palyTogether
API
- playTogether(Collectionitems):添加一组动画,播放顺序为一起播放
- playTogether(Animator…items):添加一组动画,播放顺序为一起播放
AnimatorSetbouncer=newAnimatorSet(); ObjectAnimatorobjectAnimatorA=ObjectAnimator.ofFloat(btnProperty,PropertyConstant.PROPERTY_TRANSLATION_X,0f,300f); ObjectAnimatorobjectAnimatorB=ObjectAnimator.ofFloat(btnProperty,PropertyConstant.PROPERTY_TRANSLATION_Y,0f,300f); ObjectAnimatorobjectAnimatorC=ObjectAnimator.ofFloat(btnProperty,PropertyConstant.PROPERTY_ROTATION,0f,360f); bouncer.playSequentially(objectAnimatorA,objectAnimatorB,objectAnimatorC); bouncer.setDuration(6000); bouncer.start();
掌握以上的知识点后,我的思路是,其实最后就是对执行方式的封装,所谓的执行方式就是如何正常的调用play,playSequentially和playTogether三个方法,这里需要合理的封装。
还有就是对于监听接口的封装,每个ObjectAnimator都有三个接口:
Animator.AnimatorListener 对整个动画生命周期的监听
anim.addListener(newAnimator.AnimatorListener(){
@Override
publicvoidonAnimationStart(Animatoranimator){
Toast.makeText(MainActivity.this,"start",Toast.LENGTH_LONG).show();
}
@Override
publicvoidonAnimationEnd(Animatoranimator){
Toast.makeText(MainActivity.this,"End",Toast.LENGTH_LONG).show();
}
@Override
publicvoidonAnimationCancel(Animatoranimator){
Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_LONG).show();
}
@Override
publicvoidonAnimationRepeat(Animatoranimator){
Toast.makeText(MainActivity.this,"rapeat",Toast.LENGTH_LONG).show();
}
});
anim.start();
ValueAnimator.AnimatorUpdateListener对于该动画逐帧的监听
ValueAnimatorvanim=ValueAnimator.ofInt(0,10,20);
vanim.addUpdateListener(newValueAnimator.AnimatorUpdateListener(){
@Override
publicvoidonAnimationUpdate(ValueAnimatorvalueAnimator){
//如果之前的ValueAnimtor指定的是Int的i话,那么返回的Value就是int类型,
也就是返回值类型与你创建的类型一致
intvalue=(int)valueAnimator.getAnimatedValue();
}
});
Animator.AnimatorPauseListener对于该动画的暂停和播放的监听
newAnimator.AnimatorPauseListener(){
@Override
publicvoidonAnimationPause(Animatoranimator){
}
@Override
publicvoidonAnimationResume(Animatoranimator){
}
}
由于我的初步构想是使用建造者模式的链式调用模式来设计我的工具类,如果按照普通的写法,那么整个监听接口的设置将会是灾难性的,因为所有的监听接口的设置都是混乱的,所以这里必须处理,我的思路是,学习SpringSecurity的链式调用设计,为每个类型的监听设置自己的类,然后再让工具主类调用该类型的监听接口,然后设置完毕后,在通过该监听接口类的and()方法回到工具类的主类型来,这样在链式调用的时候就有一个起止顺序,不会混乱执行了,而且如果不用设置监听,不调用监听类设置也不会影响主类的执行。
截取关键代码,以Play方法的监听接口设置为例:
/**
*工具类的主类
**/
publicstaticclassAnimatorSetWrap{
PlayAnimationListenerplayListener;
publicPlayAnimationListenertoAddPlayListener(){
playListener=newPlayAnimationListener(this);
returnplayListener;
}
}
/**
*Play方法对应的ObjectAnimator的监听实例
*/
publicstaticclassPlayAnimationListenerimplementsIAnimatorListener{
privateAnimator.AnimatorListeneranimatorListener;
privateValueAnimator.AnimatorUpdateListenerupdateListener;
privateAnimator.AnimatorPauseListenerpauseListener;
publicAnimatorSetWrapanimatorSetWrap;
publicPlayAnimationListener(AnimatorSetWrapanimatorSetWrap){
this.animatorSetWrap=animatorSetWrap;
}
@Override
publicPlayAnimationListenersetAnimatorListener(Animator.AnimatorListeneranimatorListener){
this.animatorListener=animatorListener;
returnthis;
}
@Override
publicPlayAnimationListenersetUpdateListener(ValueAnimator.AnimatorUpdateListeneranimatorListener){
this.updateListener=animatorListener;
returnthis;
}
@Override
publicPlayAnimationListenersetPauseListener(Animator.AnimatorPauseListeneranimatorListener){
this.pauseListener=animatorListener;
returnthis;
}
@Override
publicAnimatorSetWrapand(){
returnanimatorSetWrap;
}
}
/**
*动画监听的公用模板接口
*@param
*/
interfaceIAnimatorListener{
/**
*设置AnimatorListener的方法
*@paramlistener
*@return
*/
TsetAnimatorListener(Animator.AnimatorListenerlistener);
/**
*设置AnimatorUpdateListener的方法
*@paramlistener
*@return
*/
TsetUpdateListener(ValueAnimator.AnimatorUpdateListenerlistener);
/**
*设置AnimatorPauseListener的方法
*@paramlistener
*@return
*/
TsetPauseListener(Animator.AnimatorPauseListenerlistener);
/**
*桥接动画监听与动画工具类的方法
*@return
*/
AnimatorSetWrapand();
}
具体的使用方法:
AnimatorUtil.AnimatorSetWrapanimatorSetWrapDemo=newAnimatorSetWrap().toAddPlayListener().setUpdateListener(newValueAnimator.AnimatorUpdateListener(){
@Override
publicvoidonAnimationUpdate(ValueAnimatorvalueAnimator){
LogUtils.e("数值:"+valueAnimator.getAnimatedValue());
}
}).and();
通过这种链式调用,只要调用到and()方法就又回到了AnimatorSetWrap工具类的实例,剩下就可以继续调用其他动画的方法并播放动画了。
代码
说了这么多,就把我的工具类代码分享给大家吧,可能还不完善,有什么问题大家一起探讨:
importandroid.animation.Animator;
importandroid.animation.AnimatorSet;
importandroid.animation.ObjectAnimator;
importandroid.animation.TimeInterpolator;
importandroid.animation.ValueAnimator;
importandroid.os.Build;
importandroid.support.annotation.Nullable;
importandroid.support.annotation.Size;
importandroid.view.View;
importandroid.view.animation.LinearInterpolator;
importjava.util.ArrayList;
importjava.util.List;
/**
*动画工具类
*@packagecom.dhcc.commonutils
*@authorjasoncool
*@createDate2018/11/2016:16
*@description
**/
publicclassAnimatorUtils{
publicstaticfinalStringALPHA="Alpha";
publicstaticfinalStringTRANSX="TranslationX";
publicstaticfinalStringTRANSY="TranslationY";
publicstaticfinalStringSCALEX="ScaleX";
publicstaticfinalStringSCALEY="ScaleY";
publicstaticfinalStringROTATION="Rotation";
publicstaticfinalStringROTATIONX="RotationX";
publicstaticfinalStringROTATIONY="RotationY";
/**
*默认的TimeInterpolator,前后减速,中间加速
*/
privatestaticfinalTimeInterpolatorsDefaultInterpolator=
newLinearInterpolator();
publicstaticAnimatorSetWrapcreateAnimator(){
returnnewAnimatorSetWrap();
}
/**
*@paraminterpolator默认的TimeInterpolator
*@return
*/
publicstaticAnimatorSetWrapcreateAnimator(TimeInterpolatorinterpolator){
returnnewAnimatorSetWrap(interpolator);
}
/**
*属性动画组合
*属性动画组合对应的是AnimatorSet类,我们只需要new他就好。
*
*它对应的主要有这四个方法,play,before,with,after。
*这四个方法里面全都是填入往后儿们的animator类,
*但是先后执行顺序不一样,分别对应着开启,最后,同步,最开始执行。
*我们注意到他是先执行的after,然后是play和with同时执行,最后执行的before。
*所以大家记住这个顺序,无论怎么写,都是这个执行顺序。
*
*/
publicstaticclassAnimatorSetWrap{
privateViewmView;
/**
*不设置默认插值器时,工具类自带的默认插值器
*/
privateTimeInterpolatormTimeInterpolator;
/**
*判断play方法只允许执行一次的布尔值
*/
booleanmIsPlaying=false;
/**
*联合动画的动画容器
*/
privateAnimatorSetmAnimatorSet;
/**
*联合动画的动画构造器
*/
privateAnimatorSet.BuildermAnimatorBuilder;
/**
*默认执行时间
*/
privateintmDuration=1000;
/**
*play的监听器类
*/
PlayAnimationListenerplayListener;
/**
*before的监听器类
*/
BeforeAnimationListenerbeforeListener;
/**
*with的监听器类
*/
WithAnimationListenerwithListener;
/**
*after的监听器类
*/
AfterAnimationListenerafterListener;
/**
*then的监听器类
*/
ThenAnimationListenerthenListener;
/**
*顺序播放或者同时播放时存储动画的列表容器
*/
ListmAnimatorList;
/**
*是否已经初始化then动画
*/
booleanmHasInitThenAnim=false;
privateAnimatorSetWrap(){
this(sDefaultInterpolator);
}
/**
*构造方法
*主要是负责
*1.初始化默认的插值器mTimeInterpolator
*2.初始化联合动画SetmAnimatorSet
*3.初始化顺序或同时播放动画容器mAnimatorList
*@paraminterpolator
*/
privateAnimatorSetWrap(TimeInterpolatorinterpolator){
mTimeInterpolator=interpolator;
mAnimatorSet=newAnimatorSet();
mAnimatorList=newArrayList<>(16);
}
/**
*Play动画的监听启动方法
*如果要监听play动画先调用这个方法
*@return
*/
publicPlayAnimationListenertoAddPlayListener(){
playListener=newPlayAnimationListener(this);
returnplayListener;
}
/**
*Before动画的监听启动方法
*如果要监听Before动画先调用这个方法
*@return
*/
publicBeforeAnimationListenertoAddBeforeListener(){
beforeListener=newBeforeAnimationListener(this);
returnbeforeListener;
}
/**
*With动画的监听启动方法
*如果要监听With动画先调用这个方法
*@return
*/
publicWithAnimationListenertoAddWithListener(){
withListener=newWithAnimationListener(this);
returnwithListener;
}
/**
*After动画的监听启动方法
*如果要监听After动画先调用这个方法
*@return
*/
publicAfterAnimationListenertoAddAfterListener(){
afterListener=newAfterAnimationListener(this);
returnafterListener;
}
/**
*顺序或同时播放动画执行时的监听方法
*要先于Then方法进行调用
*@return
*/
publicThenAnimationListenertoAddThenListener(){
thenListener=newThenAnimationListener(this);
returnthenListener;
}
/**
*顺序或者同时播放动画时的调用方法
*在其内部生成一个Animator并将该Animator加入到mAnimatorList中备用
*@paramview动画执行的主体View
*@paramanimName动画类型
*@paraminterpolator动画插值器如果不设置就用默认的
*@paramrepeatCount重复次数
*@paramduration执行时间
*@paramvalues动画执行的值
*@return
*/
publicAnimatorSetWrapthen(Viewview,StringanimName,@NullableTimeInterpolatorinterpolator,@Size(min=0,max=Integer.MAX_VALUE)intrepeatCount,@Size(min=0,max=Integer.MAX_VALUE)intduration,float...values){
LogUtils.e("addThen");
if(view==null){
thrownewRuntimeException("view不能为空");
}
mIsPlaying=true;
mView=view;
ObjectAnimatorthenAnimator=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{
thrownewRuntimeException("SDK最小要求19");
}
}
mAnimatorList.add(thenAnimator);
returnthis;
}
publicAnimatorSetWrapthen(Animatoranimator){
mAnimatorList.add(animator);
returnthis;
}
publicAnimatorSetWrapthen(AnimatorSetWrapanimator){
mAnimatorList.add(animator.getAnimatorSet());
returnthis;
}
/**
*AnimatorSet的Play方法,整个动画过程只能调用一次
*并且一旦执行play方法将会清除掉mAnimatorList中存储的顺序或同时执行的方法实例
*@paramview方法主体
*@paramanimName动画类型
*@paraminterpolator插值器
*@paramrepeatCount重复次数
*@paramduration动画时长
*@paramvalues动画执行值
*@return
*/
publicAnimatorSetWrapplay(Viewview,StringanimName,@NullableTimeInterpolatorinterpolator,@Size(min=0,max=Integer.MAX_VALUE)intrepeatCount,@Size(min=0,max=Integer.MAX_VALUE)intduration,float...values){
LogUtils.e("play");
if(mIsPlaying){
thrownewRuntimeException("AnimatorSetWrap.play()方法只能调用一次");
}
if(view==null){
thrownewRuntimeException("view不能为空");
}
mIsPlaying=true;
mView=view;
ObjectAnimatorplayAnimator=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{
thrownewRuntimeException("SDK最小要求19");
}
}
mAnimatorList.clear();
mAnimatorBuilder=mAnimatorSet.play(playAnimator);
returnthis;
}
publicAnimatorSetWrapplay(Animatoranimator){
mAnimatorList.clear();
mAnimatorBuilder=mAnimatorSet.play(animator);
returnthis;
}
publicAnimatorSetWrapplay(AnimatorSetWrapanimator){
mAnimatorList.clear();
mAnimatorBuilder=mAnimatorSet.play(animator.getAnimatorSet());
returnthis;
}
/**
*AnimatorSet的Before方法
*@paramview动画执行的主体View
*@paramanimName动画类型
*@paraminterpolator插值器
*@paramrepeatCount重复次数
*@paramduration动画执行时长
*@paramvalues动画执行数值
*@return
*/
publicAnimatorSetWrapbefore(Viewview,StringanimName,@NullableTimeInterpolatorinterpolator,@Size(min=0,max=Integer.MAX_VALUE)intrepeatCount,@Size(min=0,max=Integer.MAX_VALUE)intduration,float...values){
LogUtils.e("before");
if(view==null){
thrownewRuntimeException("view不能为空");
}
ObjectAnimatorbeforeAnimator=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{
thrownewRuntimeException("SDK最小要求19");
}
}
mAnimatorBuilder=mAnimatorBuilder.before(beforeAnimator);
returnthis;
}
publicAnimatorSetWrapbefore(Animatoranimator){
mAnimatorBuilder=mAnimatorBuilder.before(animator);
returnthis;
}
publicAnimatorSetWrapbefore(AnimatorSetWrapanimator){
mAnimatorBuilder=mAnimatorBuilder.before(animator.getAnimatorSet());
returnthis;
}
publicAnimatorSetWrapwith(Viewview,StringanimName,@NullableTimeInterpolatorinterpolator,@Size(min=0,max=Integer.MAX_VALUE)intrepeatCount,@Size(min=0,max=Integer.MAX_VALUE)intduration,float...values){
LogUtils.e("with");
if(view==null){
thrownewRuntimeException("view不能为空");
}
ObjectAnimatorwithAnimator=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{
thrownewRuntimeException("SDK最小要求19");
}
}
mAnimatorBuilder=mAnimatorBuilder.with(withAnimator);
returnthis;
}
publicAnimatorSetWrapwith(Animatoranimator){
mAnimatorBuilder=mAnimatorBuilder.with(animator);
returnthis;
}
publicAnimatorSetWrapwith(AnimatorSetWrapanimator){
mAnimatorBuilder=mAnimatorBuilder.with(animator.getAnimatorSet());
returnthis;
}
publicAnimatorSetWrapafter(Viewview,StringanimName,@NullableTimeInterpolatorinterpolator,@Size(min=0,max=Integer.MAX_VALUE)intrepeatCount,@Size(min=0,max=Integer.MAX_VALUE)intduration,float...values){
LogUtils.e("after");
if(view==null){
thrownewRuntimeException("view不能为空");
}
ObjectAnimatorafterAnimator=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{
thrownewRuntimeException("SDK最小要求19");
}
}
mAnimatorBuilder=mAnimatorBuilder.after(afterAnimator);
returnthis;
}
publicAnimatorSetWrapafter(Animatoranimator){
mAnimatorBuilder=mAnimatorBuilder.after(animator);
returnthis;
}
publicAnimatorSetWrapafter(AnimatorSetWrapanimator){
mAnimatorBuilder=mAnimatorBuilder.after(animator.getAnimatorSet());
returnthis;
}
publicAnimatorSetWrapafter(longdelay){
mAnimatorBuilder.after(delay);
returnthis;
}
/**
*直接执行动画,该动画操作主要用作执行AnimatorSet的组合动画
*如果mAnimatorList不为0则执行逐一播放动画
*/
publicvoidplayAnim(){
if(mAnimatorList.size()>0){
readyThen(true);
}
mAnimatorSet.start();
}
/**
*在一定时长内运行完该组合动画
*如果mAnimatorList不为0则执行逐一播放动画
*@paramduration动画时长
*/
publicvoidplayAnim(longduration){
if(mAnimatorList.size()>0){
readyThen(true);
}
mAnimatorSet.setDuration(duration);
mAnimatorSet.start();
}
/**
*延迟一定时长播放动画
*如果mAnimatorList不为0则执行逐一播放动画
*@paramdelay延迟时长
*/
publicvoidplayAnimDelay(longdelay){
if(mAnimatorList.size()>0){
readyThen(true);
}
mAnimatorSet.setStartDelay(delay);
mAnimatorSet.start();
}
/**
*直接执行动画,该动画操作主要用作执行AnimatorSet的组合动画
*/
publicvoidplayAnim(booleanisSequentially){
readyThen(isSequentially);
mAnimatorSet.start();
}
/**
*在一定时长内运行完该组合动画
*@paramduration动画时长
*/
publicvoidplayAnim(booleanisSequentially,longduration){
readyThen(isSequentially);
mAnimatorSet.setDuration(duration);
mAnimatorSet.start();
}
/**
*延迟一定时长播放动画
*@paramdelay延迟时长
*/
publicvoidplayAnimDelay(booleanisSequentially,longdelay){
readyThen(isSequentially);
mAnimatorSet.setStartDelay(delay);
mAnimatorSet.start();
}
/**
*顺序播放动画
*@paramisSequentially是逐一播放还是同时播放
*/
privatevoidreadyThen(booleanisSequentially){
//只在第一次启动时初始化
if(mHasInitThenAnim){
return;
}
mHasInitThenAnim=true;
if(mAnimatorList.size()>0){
AnimatorSetset=newAnimatorSet();
if(isSequentially){
set.playSequentially(mAnimatorList);
}else{
set.playTogether(mAnimatorList);
}
mAnimatorBuilder.before(set);
}
}
/**
*取消动画
*/
publicvoidcancel(){
mAnimatorSet.cancel();
mAnimatorList.clear();
}
/**
*获取AnimatorSet的实例
*@return
*/
privateAnimatorSetgetAnimatorSet(){
returnmAnimatorSet;
}
/**
*为AnimatorSet设置监听
*@paramlistener
*@return
*/
publicAnimatorSetWrapsetAnimatorSetListener(Animator.AnimatorListenerlistener){
mAnimatorSet.addListener(listener);
returnthis;
}
/**
*取消AnimatorSet的监听
*@paramlistener
*/
publicvoidremoveSetListner(Animator.AnimatorListenerlistener){
mAnimatorSet.removeListener(listener);
}
/**
*取消全部AnimatorSet的监听
*/
publicvoidremoveAllLSetisteners(){
mAnimatorSet.removeAllListeners();
}
/**
*判断一个View是否在当前的屏幕中可见(肉眼真实可见)
*@parammView
*@return返回true则可见
*/
publicstaticbooleanisVisibleOnScreen(ViewmView){
if(mView==null){
returnfalse;
}
returnmView.getWindowVisibility()==View.VISIBLE
&&mView.getVisibility()==View.VISIBLE&&mView.isShown();
}
}
/**
*Play方法对应的ObjectAnimator的监听实例
*/
publicstaticclassPlayAnimationListenerimplementsIAnimatorListener{
privateAnimator.AnimatorListeneranimatorListener;
privateValueAnimator.AnimatorUpdateListenerupdateListener;
privateAnimator.AnimatorPauseListenerpauseListener;
publicAnimatorSetWrapanimatorSetWrap;
publicPlayAnimationListener(AnimatorSetWrapanimatorSetWrap){
this.animatorSetWrap=animatorSetWrap;
}
@Override
publicPlayAnimationListenersetAnimatorListener(Animator.AnimatorListeneranimatorListener){
this.animatorListener=animatorListener;
returnthis;
}
@Override
publicPlayAnimationListenersetUpdateListener(ValueAnimator.AnimatorUpdateListeneranimatorListener){
this.updateListener=animatorListener;
returnthis;
}
@Override
publicPlayAnimationListenersetPauseListener(Animator.AnimatorPauseListeneranimatorListener){
this.pauseListener=animatorListener;
returnthis;
}
@Override
publicAnimatorSetWrapand(){
returnanimatorSetWrap;
}
}
publicstaticclassBeforeAnimationListenerimplementsIAnimatorListener{
privateAnimator.AnimatorListeneranimatorListener;
privateValueAnimator.AnimatorUpdateListenerupdateListener;
privateAnimator.AnimatorPauseListenerpauseListener;
publicAnimatorSetWrapanimatorSetWrap;
publicBeforeAnimationListener(AnimatorSetWrapanimatorSetWrap){
this.animatorSetWrap=animatorSetWrap;
}
@Override
publicAnimatorSetWrapand(){
returnanimatorSetWrap;
}
@Override
publicBeforeAnimationListenersetAnimatorListener(Animator.AnimatorListenerlistener){
this.animatorListener=listener;
returnthis;
}
@Override
publicBeforeAnimationListenersetUpdateListener(ValueAnimator.AnimatorUpdateListenerlistener){
this.updateListener=listener;
returnthis;
}
@Override
publicBeforeAnimationListenersetPauseListener(Animator.AnimatorPauseListenerlistener){
this.pauseListener=listener;
returnthis;
}
}
publicstaticclassWithAnimationListenerimplementsIAnimatorListener{
privateAnimator.AnimatorListeneranimatorListener;
privateValueAnimator.AnimatorUpdateListenerupdateListener;
privateAnimator.AnimatorPauseListenerpauseListener;
publicAnimatorSetWrapanimatorSetWrap;
publicWithAnimationListener(AnimatorSetWrapanimatorSetWrap){
this.animatorSetWrap=animatorSetWrap;
}
@Override
publicAnimatorSetWrapand(){
returnanimatorSetWrap;
}
@Override
publicWithAnimationListenersetAnimatorListener(Animator.AnimatorListenerlistener){
this.animatorListener=listener;
returnthis;
}
@Override
publicWithAnimationListenersetUpdateListener(ValueAnimator.AnimatorUpdateListenerlistener){
this.updateListener=listener;
returnthis;
}
@Override
publicWithAnimationListenersetPauseListener(Animator.AnimatorPauseListenerlistener){
this.pauseListener=listener;
returnthis;
}
}
publicstaticclassAfterAnimationListenerimplementsIAnimatorListener{
privateAnimator.AnimatorListeneranimatorListener;
privateValueAnimator.AnimatorUpdateListenerupdateListener;
privateAnimator.AnimatorPauseListenerpauseListener;
publicAnimatorSetWrapanimatorSetWrap;
publicAfterAnimationListener(AnimatorSetWrapanimatorSetWrap){
this.animatorSetWrap=animatorSetWrap;
}
@Override
publicAnimatorSetWrapand(){
returnanimatorSetWrap;
}
@Override
publicAfterAnimationListenersetAnimatorListener(Animator.AnimatorListenerlistener){
this.animatorListener=listener;
returnthis;
}
@Override
publicAfterAnimationListenersetUpdateListener(ValueAnimator.AnimatorUpdateListenerlistener){
this.updateListener=listener;
returnthis;
}
@Override
publicAfterAnimationListenersetPauseListener(Animator.AnimatorPauseListenerlistener){
this.pauseListener=listener;
returnthis;
}
}
publicstaticclassThenAnimationListenerimplementsIAnimatorListener{
privateAnimator.AnimatorListeneranimatorListener;
privateValueAnimator.AnimatorUpdateListenerupdateListener;
privateAnimator.AnimatorPauseListenerpauseListener;
publicAnimatorSetWrapanimatorSetWrap;
publicThenAnimationListener(AnimatorSetWrapanimatorSetWrap){
this.animatorSetWrap=animatorSetWrap;
}
@Override
publicAnimatorSetWrapand(){
returnanimatorSetWrap;
}
@Override
publicThenAnimationListenersetAnimatorListener(Animator.AnimatorListenerlistener){
this.animatorListener=listener;
returnthis;
}
@Override
publicThenAnimationListenersetUpdateListener(ValueAnimator.AnimatorUpdateListenerlistener){
this.updateListener=listener;
returnthis;
}
@Override
publicThenAnimationListenersetPauseListener(Animator.AnimatorPauseListenerlistener){
this.pauseListener=listener;
returnthis;
}
}
/**
*动画监听的公用模板接口
*@param
*/
interfaceIAnimatorListener{
/**
*设置AnimatorListener的方法
*@paramlistener
*@return
*/
TsetAnimatorListener(Animator.AnimatorListenerlistener);
/**
*设置AnimatorUpdateListener的方法
*@paramlistener
*@return
*/
TsetUpdateListener(ValueAnimator.AnimatorUpdateListenerlistener);
/**
*设置AnimatorPauseListener的方法
*@paramlistener
*@return
*/
TsetPauseListener(Animator.AnimatorPauseListenerlistener);
/**
*桥接动画监听与动画工具类的方法
*@return
*/
AnimatorSetWrapand();
}
}
使用方法:
AnimatorUtils.createAnimator().play(viewAnimator,AnimatorUtils.ROTATIONY,null,0,1000,0,360).toAddThenListener().setUpdateListener(newValueAnimator.AnimatorUpdateListener(){
@Override
publicvoidonAnimationUpdate(ValueAnimatorvalueAnimator){
LogUtils.e("then1:"+valueAnimator.getAnimatedValue());
}
}).and().then(viewAnimator,AnimatorUtils.TRANSY,null,-1,-2,0,100,200,300,200,100,0).toAddThenListener().setUpdateListener(newValueAnimator.AnimatorUpdateListener(){
@Override
publicvoidonAnimationUpdate(ValueAnimatorvalueAnimator){
LogUtils.e("then2:"+valueAnimator.getAnimatedValue());
}
}).and().then(viewAnimator,AnimatorUtils.SCALEX,newLinearInterpolator(),0,1000,0,10).toAddWithListener().setUpdateListener(newValueAnimator.AnimatorUpdateListener(){
@Override
publicvoidonAnimationUpdate(ValueAnimatorvalueAnimator){
LogUtils.e("with1:"+valueAnimator.getAnimatedValue());
}
}).and().with(viewAnimator,AnimatorUtils.SCALEY,newLinearInterpolator(),0,1000,0,10).toAddWithListener().setAnimatorListener(newAnimator.AnimatorListener(){
@Override
publicvoidonAnimationStart(Animatoranimator){
LogUtils.e("with2:onAnimationStart");
}
@Override
publicvoidonAnimationEnd(Animatoranimator){
}
@Override
publicvoidonAnimationCancel(Animatoranimator){
}
@Override
publicvoidonAnimationRepeat(Animatoranimator){
}
}).and().with(viewAnimator,AnimatorUtils.ALPHA,newLinearInterpolator(),0,1000,1,0,1)
//.playSequentially(2000);
.playAnim();
上面的动画调用方法是我乱写的,具体就是为了演示工具类的使用方法,你可以改成自己的调用方法。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。