您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Android中怎么封裝一個(gè)動(dòng)畫(huà)工具類(lèi),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
1.使用AnimatorSet的Builder來(lái)組合播放
AnimatorSet.Builder是一個(gè)使用的動(dòng)畫(huà)工具類(lèi),用于方便向AnimatorSet添加動(dòng)畫(huà)以及設(shè)置各種動(dòng)畫(huà)之間的關(guān)系。在 AnimatorSet.Builder中,共聲明了after(long)、after(Animator)、before(Animator)、with(Animator)等四個(gè)方法。
after(delay) 設(shè)置動(dòng)畫(huà)延遲delay時(shí)間后播放
after(anim) 設(shè)置在anim動(dòng)畫(huà)結(jié)束后播放此動(dòng)畫(huà)
before(anim) 設(shè)置此動(dòng)畫(huà)早于anim播放
with(anim) 設(shè)置此動(dòng)畫(huà)與anim一起播放
然后再調(diào)用paly(anim)方法來(lái)鏈?zhǔn)秸{(diào)用動(dòng)畫(huà)
AnimatorSet set=new AnimatorSet(); set.play(anim1).before(anim2).with(anim3).after(anim4);
我們注意到他是先執(zhí)行的after,然后是play和with同時(shí)執(zhí)行,最后執(zhí)行的before。所以大家記住這個(gè)順序,無(wú)論怎么寫(xiě),都是這個(gè)執(zhí)行順序。
2.使用AnimatorSet的playSequentially
API
playSequentially(List items):添加一組動(dòng)畫(huà),播放順序?yàn)橹鹨徊シ?/p>
playSequentially(Animator… items):添加一組動(dòng)畫(huà),播放順序?yàn)橹鹨徊シ?/p>
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):添加一組動(dòng)畫(huà),播放順序?yàn)橐黄鸩シ?/p>
playTogether(Animator… items):添加一組動(dòng)畫(huà),播放順序?yàn)橐黄鸩シ?/p>
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();
掌握以上的知識(shí)點(diǎn)后,我的思路是,其實(shí)最后就是對(duì)執(zhí)行方式的封裝,所謂的執(zhí)行方式就是如何正常的調(diào)用play,playSequentially和playTogether三個(gè)方法,這里需要合理的封裝。
還有就是對(duì)于監(jiān)聽(tīng)接口的封裝,每個(gè)ObjectAnimator都有三個(gè)接口:
Animator.AnimatorListener 對(duì)整個(gè)動(dòng)畫(huà)生命周期的監(jiān)聽(tīng)
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 對(duì)于該動(dòng)畫(huà)逐幀的監(jiān)聽(tīng)
ValueAnimator vanim = ValueAnimator.ofInt(0,10,20); vanim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { //如果之前的ValueAnimtor指定的是Int的i話(huà),那么返回的Value就是int類(lèi)型, 也就是返回值類(lèi)型與你創(chuàng)建的類(lèi)型一致 int value = (int) valueAnimator.getAnimatedValue(); } });
Animator.AnimatorPauseListener 對(duì)于該動(dòng)畫(huà)的暫停和播放的監(jiān)聽(tīng)
new Animator.AnimatorPauseListener() { @Override public void onAnimationPause(Animator animator) { } @Override public void onAnimationResume(Animator animator) { } }
由于我的初步構(gòu)想是使用建造者模式的鏈?zhǔn)秸{(diào)用模式來(lái)設(shè)計(jì)我的工具類(lèi),如果按照普通的寫(xiě)法,那么整個(gè)監(jiān)聽(tīng)接口的設(shè)置將會(huì)是災(zāi)難性的,因?yàn)樗械谋O(jiān)聽(tīng)接口的設(shè)置都是混亂的,所以這里必須處理,我的思路是,學(xué)習(xí)SpringSecurity的鏈?zhǔn)秸{(diào)用設(shè)計(jì),為每個(gè)類(lèi)型的監(jiān)聽(tīng)設(shè)置自己的類(lèi),然后再讓工具主類(lèi)調(diào)用該類(lèi)型的監(jiān)聽(tīng)接口,然后設(shè)置完畢后,在通過(guò)該監(jiān)聽(tīng)接口類(lèi)的and()方法回到工具類(lèi)的主類(lèi)型來(lái),這樣在鏈?zhǔn)秸{(diào)用的時(shí)候就有一個(gè)起止順序,不會(huì)混亂執(zhí)行了,而且如果不用設(shè)置監(jiān)聽(tīng),不調(diào)用監(jiān)聽(tīng)類(lèi)設(shè)置也不會(huì)影響主類(lèi)的執(zhí)行。
截取關(guān)鍵代碼,以Play方法的監(jiān)聽(tīng)接口設(shè)置為例:
/** *工具類(lèi)的主類(lèi) **/ public static class AnimatorSetWrap{ PlayAnimationListener playListener; public PlayAnimationListener toAddPlayListener(){ playListener=new PlayAnimationListener(this); return playListener; } } /** * Play方法對(duì)應(yīng)的ObjectAnimator的監(jiān)聽(tīng)實(shí)例 */ 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; } } /** * 動(dòng)畫(huà)監(jiān)聽(tīng)的公用模板接口 * @param <T> */ interface IAnimatorListener<T>{ /** * 設(shè)置AnimatorListener的方法 * @param listener * @return */ T setAnimatorListener(Animator.AnimatorListener listener); /** * 設(shè)置AnimatorUpdateListener的方法 * @param listener * @return */ T setUpdateListener(ValueAnimator.AnimatorUpdateListener listener); /** * 設(shè)置AnimatorPauseListener的方法 * @param listener * @return */ T setPauseListener(Animator.AnimatorPauseListener listener); /** * 橋接動(dòng)畫(huà)監(jiān)聽(tīng)與動(dòng)畫(huà)工具類(lèi)的方法 * @return */ AnimatorSetWrap and(); }
具體的使用方法:
AnimatorUtil.AnimatorSetWrap animatorSetWrapDemo=new AnimatorSetWrap().toAddPlayListener().setUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { LogUtils.e("數(shù)值:"+valueAnimator.getAnimatedValue()); } }).and();
通過(guò)這種鏈?zhǔn)秸{(diào)用,只要調(diào)用到and()方法就又回到了AnimatorSetWrap工具類(lèi)的實(shí)例,剩下就可以繼續(xù)調(diào)用其他動(dòng)畫(huà)的方法并播放動(dòng)畫(huà)了。
代碼
說(shuō)了這么多,就把我的工具類(lèi)代碼分享給大家吧,可能還不完善,有什么問(wèn)題大家一起探討:
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; /** * 動(dòng)畫(huà)工具類(lèi) *@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"; /** * 默認(rèn)的TimeInterpolator,前后減速,中間加速 */ private static final TimeInterpolator sDefaultInterpolator = new LinearInterpolator(); public static AnimatorSetWrap createAnimator() { return new AnimatorSetWrap(); } /** * @param interpolator 默認(rèn)的TimeInterpolator * @return */ public static AnimatorSetWrap createAnimator(TimeInterpolator interpolator) { return new AnimatorSetWrap(interpolator); } /** * 屬性動(dòng)畫(huà)組合 * 屬性動(dòng)畫(huà)組合對(duì)應(yīng)的是AnimatorSet類(lèi),我們只需要new他就好。 * * 它對(duì)應(yīng)的主要有這四個(gè)方法,play,before,with,after。 * 這四個(gè)方法里面全都是填入往后兒們的animator類(lèi), * 但是先后執(zhí)行順序不一樣,分別對(duì)應(yīng)著開(kāi)啟,最后,同步,最開(kāi)始執(zhí)行。 * 我們注意到他是先執(zhí)行的after,然后是play和with同時(shí)執(zhí)行,最后執(zhí)行的before。 * 所以大家記住這個(gè)順序,無(wú)論怎么寫(xiě),都是這個(gè)執(zhí)行順序。 * */ public static class AnimatorSetWrap{ private View mView; /** * 不設(shè)置默認(rèn)插值器時(shí),工具類(lèi)自帶的默認(rèn)插值器 */ private TimeInterpolator mTimeInterpolator; /** * 判斷play方法只允許執(zhí)行一次的布爾值 */ boolean mIsPlaying=false; /** * 聯(lián)合動(dòng)畫(huà)的動(dòng)畫(huà)容器 */ private AnimatorSet mAnimatorSet; /** * 聯(lián)合動(dòng)畫(huà)的動(dòng)畫(huà)構(gòu)造器 */ private AnimatorSet.Builder mAnimatorBuilder; /** * 默認(rèn)執(zhí)行時(shí)間 */ private int mDuration=1000; /** * play的監(jiān)聽(tīng)器類(lèi) */ PlayAnimationListener playListener; /** * before的監(jiān)聽(tīng)器類(lèi) */ BeforeAnimationListener beforeListener; /** * with的監(jiān)聽(tīng)器類(lèi) */ WithAnimationListener withListener; /** * after的監(jiān)聽(tīng)器類(lèi) */ AfterAnimationListener afterListener; /** * then的監(jiān)聽(tīng)器類(lèi) */ ThenAnimationListener thenListener; /** * 順序播放或者同時(shí)播放時(shí)存儲(chǔ)動(dòng)畫(huà)的列表容器 */ List<Animator> mAnimatorList; /** * 是否已經(jīng)初始化then動(dòng)畫(huà) */ boolean mHasInitThenAnim=false; private AnimatorSetWrap(){ this(sDefaultInterpolator); } /** * 構(gòu)造方法 * 主要是負(fù)責(zé) * 1.初始化默認(rèn)的插值器 mTimeInterpolator * 2.初始化聯(lián)合動(dòng)畫(huà)Set mAnimatorSet * 3.初始化順序或同時(shí)播放動(dòng)畫(huà)容器 mAnimatorList * @param interpolator */ private AnimatorSetWrap(TimeInterpolator interpolator) { mTimeInterpolator = interpolator; mAnimatorSet = new AnimatorSet(); mAnimatorList=new ArrayList<>(16); } /** * Play動(dòng)畫(huà)的監(jiān)聽(tīng)啟動(dòng)方法 * 如果要監(jiān)聽(tīng)play動(dòng)畫(huà)先調(diào)用這個(gè)方法 * @return */ public PlayAnimationListener toAddPlayListener(){ playListener=new PlayAnimationListener(this); return playListener; } /** * Before動(dòng)畫(huà)的監(jiān)聽(tīng)啟動(dòng)方法 * 如果要監(jiān)聽(tīng)Before動(dòng)畫(huà)先調(diào)用這個(gè)方法 * @return */ public BeforeAnimationListener toAddBeforeListener(){ beforeListener=new BeforeAnimationListener(this); return beforeListener; } /** * With動(dòng)畫(huà)的監(jiān)聽(tīng)啟動(dòng)方法 * 如果要監(jiān)聽(tīng)With動(dòng)畫(huà)先調(diào)用這個(gè)方法 * @return */ public WithAnimationListener toAddWithListener(){ withListener=new WithAnimationListener(this); return withListener; } /** * After動(dòng)畫(huà)的監(jiān)聽(tīng)啟動(dòng)方法 * 如果要監(jiān)聽(tīng)After動(dòng)畫(huà)先調(diào)用這個(gè)方法 * @return */ public AfterAnimationListener toAddAfterListener(){ afterListener=new AfterAnimationListener(this); return afterListener; } /** * 順序或同時(shí)播放動(dòng)畫(huà)執(zhí)行時(shí)的監(jiān)聽(tīng)方法 * 要先于Then方法進(jìn)行調(diào)用 * @return */ public ThenAnimationListener toAddThenListener(){ thenListener=new ThenAnimationListener(this); return thenListener; } /** * 順序或者同時(shí)播放動(dòng)畫(huà)時(shí)的調(diào)用方法 * 在其內(nèi)部生成一個(gè)Animator并將該Animator加入到mAnimatorList中備用 * @param view 動(dòng)畫(huà)執(zhí)行的主體View * @param animName 動(dòng)畫(huà)類(lèi)型 * @param interpolator 動(dòng)畫(huà)插值器 如果不設(shè)置就用默認(rèn)的 * @param repeatCount 重復(fù)次數(shù) * @param duration 執(zhí)行時(shí)間 * @param values 動(dòng)畫(huà)執(zhí)行的值 * @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方法,整個(gè)動(dòng)畫(huà)過(guò)程只能調(diào)用一次 * 并且一旦執(zhí)行play方法將會(huì)清除掉mAnimatorList中存儲(chǔ)的順序或同時(shí)執(zhí)行的方法實(shí)例 * @param view 方法主體 * @param animName 動(dòng)畫(huà)類(lèi)型 * @param interpolator 插值器 * @param repeatCount 重復(fù)次數(shù) * @param duration 動(dòng)畫(huà)時(shí)長(zhǎng) * @param values 動(dòng)畫(huà)執(zhí)行值 * @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()方法只能調(diào)用一次"); } 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 動(dòng)畫(huà)執(zhí)行的主體View * @param animName 動(dòng)畫(huà)類(lèi)型 * @param interpolator 插值器 * @param repeatCount 重復(fù)次數(shù) * @param duration 動(dòng)畫(huà)執(zhí)行時(shí)長(zhǎng) * @param values 動(dòng)畫(huà)執(zhí)行數(shù)值 * @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; } /** * 直接執(zhí)行動(dòng)畫(huà),該動(dòng)畫(huà)操作主要用作執(zhí)行AnimatorSet的組合動(dòng)畫(huà) * 如果mAnimatorList不為0 則執(zhí)行逐一播放動(dòng)畫(huà) */ public void playAnim() { if(mAnimatorList.size()>0){ readyThen(true); } mAnimatorSet.start(); } /** * 在一定時(shí)長(zhǎng)內(nèi)運(yùn)行完該組合動(dòng)畫(huà) * 如果mAnimatorList不為0 則執(zhí)行逐一播放動(dòng)畫(huà) * @param duration 動(dòng)畫(huà)時(shí)長(zhǎng) */ public void playAnim(long duration) { if(mAnimatorList.size()>0){ readyThen(true); } mAnimatorSet.setDuration(duration); mAnimatorSet.start(); } /** * 延遲一定時(shí)長(zhǎng)播放動(dòng)畫(huà) * 如果mAnimatorList不為0 則執(zhí)行逐一播放動(dòng)畫(huà) * @param delay 延遲時(shí)長(zhǎng) */ public void playAnimDelay(long delay) { if(mAnimatorList.size()>0){ readyThen(true); } mAnimatorSet.setStartDelay(delay); mAnimatorSet.start(); } /** * 直接執(zhí)行動(dòng)畫(huà),該動(dòng)畫(huà)操作主要用作執(zhí)行AnimatorSet的組合動(dòng)畫(huà) */ public void playAnim(boolean isSequentially) { readyThen(isSequentially); mAnimatorSet.start(); } /** * 在一定時(shí)長(zhǎng)內(nèi)運(yùn)行完該組合動(dòng)畫(huà) * @param duration 動(dòng)畫(huà)時(shí)長(zhǎng) */ public void playAnim(boolean isSequentially,long duration) { readyThen(isSequentially); mAnimatorSet.setDuration(duration); mAnimatorSet.start(); } /** * 延遲一定時(shí)長(zhǎng)播放動(dòng)畫(huà) * @param delay 延遲時(shí)長(zhǎng) */ public void playAnimDelay(boolean isSequentially,long delay) { readyThen(isSequentially); mAnimatorSet.setStartDelay(delay); mAnimatorSet.start(); } /** * 順序播放動(dòng)畫(huà) * @param isSequentially 是逐一播放還是同時(shí)播放 */ private void readyThen(boolean isSequentially){ // 只在第一次啟動(dòng)時(shí)初始化 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); } } /** * 取消動(dòng)畫(huà) */ public void cancel() { mAnimatorSet.cancel(); mAnimatorList.clear(); } /** * 獲取AnimatorSet的實(shí)例 * @return */ private AnimatorSet getAnimatorSet() { return mAnimatorSet; } /** * 為AnimatorSet設(shè)置監(jiān)聽(tīng) * @param listener * @return */ public AnimatorSetWrap setAnimatorSetListener(Animator.AnimatorListener listener) { mAnimatorSet.addListener(listener); return this; } /** * 取消AnimatorSet的監(jiān)聽(tīng) * @param listener */ public void removeSetListner(Animator.AnimatorListener listener) { mAnimatorSet.removeListener(listener); } /** * 取消全部AnimatorSet的監(jiān)聽(tīng) */ public void removeAllLSetisteners() { mAnimatorSet.removeAllListeners(); } /** * 判斷一個(gè)View是否在當(dāng)前的屏幕中可見(jiàn)(肉眼真實(shí)可見(jiàn)) * @param mView * @return 返回true則可見(jiàn) */ public static boolean isVisibleOnScreen(View mView) { if (mView == null) { return false; } return mView.getWindowVisibility() == View.VISIBLE && mView.getVisibility() == View.VISIBLE && mView.isShown(); } } /** * Play方法對(duì)應(yīng)的ObjectAnimator的監(jiān)聽(tīng)實(shí)例 */ 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; } } /** * 動(dòng)畫(huà)監(jiān)聽(tīng)的公用模板接口 * @param <T> */ interface IAnimatorListener<T>{ /** * 設(shè)置AnimatorListener的方法 * @param listener * @return */ T setAnimatorListener(Animator.AnimatorListener listener); /** * 設(shè)置AnimatorUpdateListener的方法 * @param listener * @return */ T setUpdateListener(ValueAnimator.AnimatorUpdateListener listener); /** * 設(shè)置AnimatorPauseListener的方法 * @param listener * @return */ T setPauseListener(Animator.AnimatorPauseListener listener); /** * 橋接動(dòng)畫(huà)監(jiān)聽(tīng)與動(dòng)畫(huà)工具類(lèi)的方法 * @return */ AnimatorSetWrap and(); } }
使用方法:
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("with2:"+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("with3: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();
關(guān)于Android中怎么封裝一個(gè)動(dòng)畫(huà)工具類(lèi)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。