前情提要:项目需要动效让一个view移动到屏幕边缘附近就消失,并在animationend的时候设置了当前View布局Gone。但是需要在进入下级页面如果打开展示开关时继续展示。
问题描述:从下级页面返回回来在onResume中获取状态并设置view展示,但是view都没有展示出来
问题原因:
动画导致对象view的位置变换,由于当前页面并没有销毁,所以动画的作用仍旧存在,导致即使设置view VISIBLE,仍旧无法展示出来。
解决方案:
方案一 animator.reverse():
对于简单的位移或者透明度等变化,可以直接使用reverse方法,但是由于项目动效较为复杂,需要对前一个动画进行监听并在其结束后进行后续动画操作,所以改方法不适用,这样会导致reverse之后,动画不断的循环往复的之类的问题,达不到预期效果。
原因在于reverse方法其实就是把该动画对象从end到start反过来再播放一遍动效
/** * Plays the ValueAnimator in reverse. If the animation is already running, * it will stop itself and play backwards from the point reached when reverse was called. * If the animation is not currently running, then it will start from the end and * play backwards. This behavior is only set for the current animation; future playing * of the animation will use the default behavior of playing forward. */ @Override public void reverse() { if (isPulsingInternal()) { long currentTime = AnimationUtils.currentAnimationTimeMillis(); long currentPlayTime = currentTime - mStartTime; long timeLeft = getScaledDuration() - currentPlayTime; mStartTime = currentTime - timeLeft; mStartTimeCommitted = true; // do not allow start time to be compensated for jank mReversing = !mReversing; } else if (mStarted) { mReversing = !mReversing; end(); } else { start(true); } }
方案二:
类似TranslateAnimation,AnimationSet属于Animation子类的这样的动画,可以直接使用setFillBefore或者setFillAfter这样的方法设置动画结束后,需要在那里展示。但是由于项目需要的不是在动画后立刻生效,所以并不适用。
方案三:
直接对目标view对象进行简单的位置,scale,alpha等进行设置。问题解决。