本文主要是介绍呼吸灯效果动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近做项目,产品设计稿上说要做一个一闪一闪的效果,那简单啊,开个定时器显示不显示,一分钟搞定,但是到演示时,又说我要的不是这个效果(可能是我想的简单了)难道这不是一闪一闪么,亮瞎眼了,然后他就从ipad上找了个效果给我看,尼玛,这是一闪一闪么,这是呼吸灯效果。就上网搜了下,根据自己使用情况写了这个工具类。
主要使用了ObjectAnimator 动画类通过对默认view大小的改变实现呼吸灯效果,代码很简单,只要传入一个view就可以了。
public class AnimationUtils {private final int BREATH_INTERVAL_TIME = 2000; //设置呼吸灯时间间隔boolean isStopAnimation = false;ObjectAnimator anim1;ObjectAnimator anim2;ObjectAnimator anim3;ObjectAnimator anim4;AnimatorSet animSet;private boolean isUp = true;public AnimationUtils() {isStopAnimation = false;}/*** 停止动画*/public void stopAnimation() {isStopAnimation = true;if (animSet != null) {animSet.cancel();animSet = null;}}/*** 开始动画** @param view*/public void startAnimation(final View view) {if (animSet == null) {isStopAnimation = false;startPlay(view, false);}}private void setAnimOut(final View view) {anim1 = ObjectAnimator.ofFloat(view, "scaleX",0.9f, 1.2f);anim2 = ObjectAnimator.ofFloat(view, "scaleY",0.9f, 1.2f);}private void setAnimIn(final View view) {anim3 = ObjectAnimator.ofFloat(view, "scaleX",1.2f, 0.9f);anim4 = ObjectAnimator.ofFloat(view, "scaleY",1.2f, 0.9f);}private void startPlay(final View view, boolean isPoll) {if (isStopAnimation) {return;}animSet = new AnimatorSet();setAnimOut(view);setAnimIn(view);animSet.setDuration(BREATH_INTERVAL_TIME);animSet.setInterpolator(new LinearInterpolator());if (isUp) {animSet.playTogether(anim1, anim2);isUp = false;} else {animSet.playTogether(anim3, anim4);isUp = true;}animSet.start();animSet.addListener(new Animator.AnimatorListener() {@Overridepublic void onAnimationStart(Animator animator) {}@Overridepublic void onAnimationEnd(Animator animator) {startAnimation(view, true);}@Overridepublic void onAnimationCancel(Animator animator) {}@Overridepublic void onAnimationRepeat(Animator animator) {}});}private void startAnimation(final View view, boolean isPoll) {startPlay(view, isPoll);}}
这篇关于呼吸灯效果动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!