在开发中属性动画是很常用的功能,下面我把属性动画的基本用法记录一下,供他人学习,也逐渐积累自己的知识。

单个动画效果:

//创建动画对象,后面的参数依次为:动画效果的目标组件,需要改变的该组建的属性(必须有对应的get和set方法就可以),后面三个参数写变化过程对应数值。ObjectAnimatoranimator=ObjectAnimator.ofFloat(textView,"TextSize",15,50,15);//动画过程所用时间,会按这个世界自动平滑执行animator.setDuration(6000);//动画开始animator.start();

组合动画效果:

//after(Animatoranim)将现有动画插入到传入的动画之后执行//after(longdelay)将现有动画延迟指定毫秒后执行//before(Animatoranim)将现有动画插入到传入的动画之前执行//with(Animatoranim)将现有动画和传入的动画同时执行//创建动画对象,后面的参数依次为:动画效果的目标组件,需要改变的该组建的属性(必须有对应的get和set方法就可以),后面三个参数写变化过程对应数值。ObjectAnimatoranimator1=ObjectAnimator.ofFloat(textView,"TextSize",15,50,15);//这里每次先获取目标View的角度floatinit=textView.getRotation();//旋转,道理同上ObjectAnimatoranimator2=ObjectAnimator.ofFloat(textView,"rotation",init,init+180f);//平移,道理同上ObjectAnimatoranimator3=ObjectAnimator.ofFloat(textView,"TranslationX",curTranslationX,-500f,curTranslationX);//设置动画组合的类AnimatorSetanimatorSet=newAnimatorSet();//设置3个动画如何组合搭配animatorSet.play(animator2).with(animator1).after(animator3);//动画过程所用时间,会按这个世界自动平滑执行animatorSet.setDuration(6000);//动画开始animatorSet.start();

为动画增加监听:

//这里是为动画添加的监听,具体实现哪个方法根据需求选择即可,例如:动画执行完毕、动画执行开始、动画执行取消、动画执行重复动作等。animatorSet.addListener(newAnimatorListenerAdapter(){//这里根据需要实现具体的想要执行的内容@OverridepublicvoidonAnimationEnd(Animatoranimation){super.onAnimationEnd(animation);}});