欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Android中怎么封裝一個動畫工具類

這篇文章將為大家詳細(xì)講解有關(guān)Android中怎么封裝一個動畫工具類,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

公司主營業(yè)務(wù):網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)建站是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出榮昌免費(fèi)做網(wǎng)站回饋大家。

1.使用AnimatorSet的Builder來組合播放

AnimatorSet.Builder是一個使用的動畫工具類,用于方便向AnimatorSet添加動畫以及設(shè)置各種動畫之間的關(guān)系。在    AnimatorSet.Builder中,共聲明了after(long)、after(Animator)、before(Animator)、with(Animator)等四個方法。

  • after(delay) 設(shè)置動畫延遲delay時間后播放

  • after(anim) 設(shè)置在anim動畫結(jié)束后播放此動畫

  • before(anim) 設(shè)置此動畫早于anim播放

  • with(anim) 設(shè)置此動畫與anim一起播放

然后再調(diào)用paly(anim)方法來鏈?zhǔn)秸{(diào)用動畫

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

我們注意到他是先執(zhí)行的after,然后是play和with同時執(zhí)行,最后執(zhí)行的before。所以大家記住這個順序,無論怎么寫,都是這個執(zhí)行順序。

2.使用AnimatorSet的playSequentially

API

  • playSequentially(List items):添加一組動畫,播放順序?yàn)橹鹨徊シ?/p>

  • playSequentially(Animator… items):添加一組動畫,播放順序?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):添加一組動畫,播放順序?yàn)橐黄鸩シ?/p>

  • playTogether(Animator… items):添加一組動畫,播放順序?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();

掌握以上的知識點(diǎn)后,我的思路是,其實(shí)最后就是對執(zhí)行方式的封裝,所謂的執(zhí)行方式就是如何正常的調(diào)用play,playSequentially和playTogether三個方法,這里需要合理的封裝。

還有就是對于監(jiān)聽接口的封裝,每個ObjectAnimator都有三個接口:

Animator.AnimatorListener  對整個動畫生命周期的監(jiān)聽

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 對于該動畫逐幀的監(jiān)聽

ValueAnimator vanim = ValueAnimator.ofInt(0,10,20);
 vanim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
 @Override
 public void onAnimationUpdate(ValueAnimator valueAnimator) {

 //如果之前的ValueAnimtor指定的是Int的i話,那么返回的Value就是int類型,
 也就是返回值類型與你創(chuàng)建的類型一致
 int value = (int) valueAnimator.getAnimatedValue();
 }
 });

Animator.AnimatorPauseListener 對于該動畫的暫停和播放的監(jiān)聽

new Animator.AnimatorPauseListener() {
 @Override
 public void onAnimationPause(Animator animator) {
 
 }

 @Override
 public void onAnimationResume(Animator animator) {

 }
 }

由于我的初步構(gòu)想是使用建造者模式的鏈?zhǔn)秸{(diào)用模式來設(shè)計(jì)我的工具類,如果按照普通的寫法,那么整個監(jiān)聽接口的設(shè)置將會是災(zāi)難性的,因?yàn)樗械谋O(jiān)聽接口的設(shè)置都是混亂的,所以這里必須處理,我的思路是,學(xué)習(xí)SpringSecurity的鏈?zhǔn)秸{(diào)用設(shè)計(jì),為每個類型的監(jiān)聽設(shè)置自己的類,然后再讓工具主類調(diào)用該類型的監(jiān)聽接口,然后設(shè)置完畢后,在通過該監(jiān)聽接口類的and()方法回到工具類的主類型來,這樣在鏈?zhǔn)秸{(diào)用的時候就有一個起止順序,不會混亂執(zhí)行了,而且如果不用設(shè)置監(jiān)聽,不調(diào)用監(jiān)聽類設(shè)置也不會影響主類的執(zhí)行。

截取關(guān)鍵代碼,以Play方法的監(jiān)聽接口設(shè)置為例:

/**
*工具類的主類
**/
public static class AnimatorSetWrap{
 PlayAnimationListener playListener;
 public PlayAnimationListener toAddPlayListener(){
 playListener=new PlayAnimationListener(this);
 return playListener;
 }
 }

/**
 * Play方法對應(yīng)的ObjectAnimator的監(jiān)聽實(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;
 }
 }

/**
 * 動畫監(jiān)聽的公用模板接口
 * @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);

 /**
 * 橋接動畫監(jiān)聽與動畫工具類的方法
 * @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();

通過這種鏈?zhǔn)秸{(diào)用,只要調(diào)用到and()方法就又回到了AnimatorSetWrap工具類的實(shí)例,剩下就可以繼續(xù)調(diào)用其他動畫的方法并播放動畫了。

代碼

說了這么多,就把我的工具類代碼分享給大家吧,可能還不完善,有什么問題大家一起探討:

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";

 /**
 * 默認(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);
 }

 /**
 * 屬性動畫組合
 * 屬性動畫組合對應(yīng)的是AnimatorSet類,我們只需要new他就好。
 *
 * 它對應(yīng)的主要有這四個方法,play,before,with,after。
 * 這四個方法里面全都是填入往后兒們的animator類,
 * 但是先后執(zhí)行順序不一樣,分別對應(yīng)著開啟,最后,同步,最開始執(zhí)行。
 * 我們注意到他是先執(zhí)行的after,然后是play和with同時執(zhí)行,最后執(zhí)行的before。
 * 所以大家記住這個順序,無論怎么寫,都是這個執(zhí)行順序。
 *
 */
 public static class AnimatorSetWrap{

 private View mView;
 /**
 * 不設(shè)置默認(rèn)插值器時,工具類自帶的默認(rèn)插值器
 */
 private TimeInterpolator mTimeInterpolator;
 /**
 * 判斷play方法只允許執(zhí)行一次的布爾值
 */
 boolean mIsPlaying=false;
 /**
 * 聯(lián)合動畫的動畫容器
 */
 private AnimatorSet mAnimatorSet;
 /**
 * 聯(lián)合動畫的動畫構(gòu)造器
 */
 private AnimatorSet.Builder mAnimatorBuilder;
 /**
 * 默認(rèn)執(zhí)行時間
 */
 private int mDuration=1000;
 /**
 * play的監(jiān)聽器類
 */
 PlayAnimationListener playListener;
 /**
 * before的監(jiān)聽器類
 */
 BeforeAnimationListener beforeListener;
 /**
 * with的監(jiān)聽器類
 */
 WithAnimationListener withListener;
 /**
 * after的監(jiān)聽器類
 */
 AfterAnimationListener afterListener;
 /**
 * then的監(jiān)聽器類
 */
 ThenAnimationListener thenListener;
 /**
 * 順序播放或者同時播放時存儲動畫的列表容器
 */
 List<Animator> mAnimatorList;
 /**
 * 是否已經(jīng)初始化then動畫
 */
 boolean mHasInitThenAnim=false;

 private AnimatorSetWrap(){
 this(sDefaultInterpolator);
 }

 /**
 * 構(gòu)造方法
 * 主要是負(fù)責(zé)
 * 1.初始化默認(rèn)的插值器 mTimeInterpolator
 * 2.初始化聯(lián)合動畫Set mAnimatorSet
 * 3.初始化順序或同時播放動畫容器 mAnimatorList
 * @param interpolator
 */
 private AnimatorSetWrap(TimeInterpolator interpolator) {
 mTimeInterpolator = interpolator;
 mAnimatorSet = new AnimatorSet();
 mAnimatorList=new ArrayList<>(16);
 }

 /**
 * Play動畫的監(jiān)聽啟動方法
 * 如果要監(jiān)聽play動畫先調(diào)用這個方法
 * @return
 */
 public PlayAnimationListener toAddPlayListener(){
 playListener=new PlayAnimationListener(this);
 return playListener;
 }
 /**
 * Before動畫的監(jiān)聽啟動方法
 * 如果要監(jiān)聽Before動畫先調(diào)用這個方法
 * @return
 */
 public BeforeAnimationListener toAddBeforeListener(){
 beforeListener=new BeforeAnimationListener(this);
 return beforeListener;
 }
 /**
 * With動畫的監(jiān)聽啟動方法
 * 如果要監(jiān)聽With動畫先調(diào)用這個方法
 * @return
 */
 public WithAnimationListener toAddWithListener(){
 withListener=new WithAnimationListener(this);
 return withListener;
 }
 /**
 * After動畫的監(jiān)聽啟動方法
 * 如果要監(jiān)聽After動畫先調(diào)用這個方法
 * @return
 */
 public AfterAnimationListener toAddAfterListener(){
 afterListener=new AfterAnimationListener(this);
 return afterListener;
 }

 /**
 * 順序或同時播放動畫執(zhí)行時的監(jiān)聽方法
 * 要先于Then方法進(jìn)行調(diào)用
 * @return
 */
 public ThenAnimationListener toAddThenListener(){
 thenListener=new ThenAnimationListener(this);
 return thenListener;
 }

 /**
 * 順序或者同時播放動畫時的調(diào)用方法
 * 在其內(nèi)部生成一個Animator并將該Animator加入到mAnimatorList中備用
 * @param view 動畫執(zhí)行的主體View
 * @param animName 動畫類型
 * @param interpolator 動畫插值器 如果不設(shè)置就用默認(rèn)的
 * @param repeatCount 重復(fù)次數(shù)
 * @param duration 執(zhí)行時間
 * @param values 動畫執(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方法,整個動畫過程只能調(diào)用一次
 * 并且一旦執(zhí)行play方法將會清除掉mAnimatorList中存儲的順序或同時執(zhí)行的方法實(shí)例
 * @param view 方法主體
 * @param animName 動畫類型
 * @param interpolator 插值器
 * @param repeatCount 重復(fù)次數(shù)
 * @param duration 動畫時長
 * @param values 動畫執(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 動畫執(zhí)行的主體View
 * @param animName 動畫類型
 * @param interpolator 插值器
 * @param repeatCount 重復(fù)次數(shù)
 * @param duration 動畫執(zhí)行時長
 * @param values 動畫執(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í)行動畫,該動畫操作主要用作執(zhí)行AnimatorSet的組合動畫
 * 如果mAnimatorList不為0 則執(zhí)行逐一播放動畫
 */
 public void playAnim() {
 if(mAnimatorList.size()>0){
 readyThen(true);
 }
 mAnimatorSet.start();
 }

 /**
 * 在一定時長內(nèi)運(yùn)行完該組合動畫
 * 如果mAnimatorList不為0 則執(zhí)行逐一播放動畫
 * @param duration 動畫時長
 */
 public void playAnim(long duration) {
 if(mAnimatorList.size()>0){
 readyThen(true);
 }
 mAnimatorSet.setDuration(duration);
 mAnimatorSet.start();
 }

 /**
 * 延遲一定時長播放動畫
 * 如果mAnimatorList不為0 則執(zhí)行逐一播放動畫
 * @param delay 延遲時長
 */
 public void playAnimDelay(long delay) {
 if(mAnimatorList.size()>0){
 readyThen(true);
 }
 mAnimatorSet.setStartDelay(delay);
 mAnimatorSet.start();
 }

 /**
 * 直接執(zhí)行動畫,該動畫操作主要用作執(zhí)行AnimatorSet的組合動畫
 */
 public void playAnim(boolean isSequentially) {
 readyThen(isSequentially);
 mAnimatorSet.start();
 }

 /**
 * 在一定時長內(nèi)運(yùn)行完該組合動畫
 * @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的實(shí)例
 * @return
 */
 private AnimatorSet getAnimatorSet() {
 return mAnimatorSet;
 }

 /**
 * 為AnimatorSet設(shè)置監(jiān)聽
 * @param listener
 * @return
 */
 public AnimatorSetWrap setAnimatorSetListener(Animator.AnimatorListener listener) {
 mAnimatorSet.addListener(listener);
 return this;
 }

 /**
 * 取消AnimatorSet的監(jiān)聽
 * @param listener
 */
 public void removeSetListner(Animator.AnimatorListener listener) {
 mAnimatorSet.removeListener(listener);
 }

 /**
 * 取消全部AnimatorSet的監(jiān)聽
 */
 public void removeAllLSetisteners() {
 mAnimatorSet.removeAllListeners();
 }

 /**
 * 判斷一個View是否在當(dāng)前的屏幕中可見(肉眼真實(shí)可見)
 * @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方法對應(yīng)的ObjectAnimator的監(jiān)聽實(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;
 }
 }

 /**
 * 動畫監(jiān)聽的公用模板接口
 * @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);

 /**
 * 橋接動畫監(jiān)聽與動畫工具類的方法
 * @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中怎么封裝一個動畫工具類就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享文章:Android中怎么封裝一個動畫工具類
文章鏈接:http://chinadenli.net/article4/joisoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)網(wǎng)站制作、網(wǎng)站營銷網(wǎng)站導(dǎo)航、定制開發(fā)企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司
欧美成人免费一级特黄| 欧美偷拍一区二区三区四区| 国产成人高清精品尤物| 欧美国产日产综合精品| 麻豆国产精品一区二区| 日韩1区二区三区麻豆| 国产精品成人一区二区在线| 粉嫩国产一区二区三区在线| 在线免费观看一二区视频| 中文字幕精品人妻一区| 欧美一区二区在线日韩| 亚洲中文字幕高清乱码毛片| 亚洲av专区在线观看| 夫妻性生活一级黄色录像| 欧美精品久久99九九| 国产又色又爽又黄又大| 一区二区三区人妻在线| 极品少妇嫩草视频在线观看| 精产国品一二三区麻豆| 欧美国产日本免费不卡| 搡老熟女老女人一区二区| 美女黄色三级深夜福利| 亚洲欧洲一区二区综合精品| 欧美一本在线免费观看| 沐浴偷拍一区二区视频| 久久女同精品一区二区| 最近中文字幕高清中文字幕无| 日本在线不卡高清欧美 | 国产成人一区二区三区久久| 国产一级不卡视频在线观看| 99香蕉精品视频国产版| 国产麻豆视频一二三区| 视频在线免费观看你懂的| 99热九九在线中文字幕| 午夜午夜精品一区二区| 中文字幕亚洲精品乱码加勒比| 人妻偷人精品一区二区三区不卡| 青青久久亚洲婷婷中文网| 午夜国产福利在线播放| 日本免费一本一二区三区| 中文字幕精品一区二区三|