欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android动画工具类的封装实战记录

程序员文章站 2022-06-14 17:41:18
起因 最近在做一个组件化框架的封装,现在开发到一些常用工具类的封装了,突然意识到好像还没有做动画的工具类,于是开始着手开发之。 思路 既然要做动画,肯定是...

起因

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

思路

既然要做动画,肯定是要做属性动画的工具类的封装了,由于补间动画和逐帧动画并不能改变目标动画主题的实际属性,在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)方法来链式调用动画

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):添加一组动画,播放顺序为逐一播放
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):添加一组动画,播放顺序为一起播放
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  对整个动画生命周期的监听

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 对于该动画逐帧的监听

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 对于该动画的暂停和播放的监听

new animator.animatorpauselistener() {
 @override
 public void onanimationpause(animator animator) {
 
 }

 @override
 public void onanimationresume(animator animator) {

 }
 }

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

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

/**
*工具类的主类
**/
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();
 }

具体的使用方法:

animatorutil.animatorsetwrap animatorsetwrapdemo=new animatorsetwrap().toaddplaylistener().setupdatelistener(new valueanimator.animatorupdatelistener() {
 @override
 public void onanimationupdate(valueanimator valueanimator) {
 logutils.e("数值:"+valueanimator.getanimatedvalue());
 }
 }).and();

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

代码

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

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();
 }

}

使用方法:

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();

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。