补间动画和View手势事件

动画类介绍
// 补间动画类
android.view.animation#Animation
// 属性动画类
android.animation#Animator

补间动画无清除动画导致的手势问题

情景:子View设置了OnTouchListener且返回true,当子View执行了fillAfter为true的补间动画且后,在结束的时候没有执行View#clearAnimation(),注意调用Animation#cancle()是无效的,那么即便子View的可见性设置为View#GONE,在子View所处范围内的手势依旧会被子View拦截。

源码
ViewGroup#canViewReceivePointerEvents(28版本还有这个方法,29好像没有这个方法了)
ViewGroup#dispatchTouchEvent()的方法中会执行canViewReceivePointerEvents()方法

   /**
     * Returns true if a child view can receive pointer events.
     * 返回true则子view可以接收手势事件
     * @hide
     */
    private static boolean canViewReceivePointerEvents(@NonNull View child) {
        return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE
                // 判断子view的属性动画是否不为null
                || child.getAnimation() != null;
    }

ViewGroup#finishAnimatingView
 /**
     * Cleanup a view when its animation is done. This may mean removing it from
     * the list of disappearing views.
     *
     *  view执行属性动画结束后会调用
     *
     * @param view The view whose animation has finished
     * @param animation The animation, cannot be null
     */
    void finishAnimatingView(final View view, Animation animation) {
        final ArrayList disappearingChildren = mDisappearingChildren;
        if (disappearingChildren != null) {
            if (disappearingChildren.contains(view)) {
                disappearingChildren.remove(view);

                if (view.mAttachInfo != null) {
                    view.dispatchDetachedFromWindow();
                }

                view.clearAnimation();
                mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
            }
        }

        if (animation != null && !animation.getFillAfter()) {
            // 当fillAfter为false的时候会自动清除动画
            view.clearAnimation();
        }

        if ((view.mPrivateFlags & PFLAG_ANIMATION_STARTED) == PFLAG_ANIMATION_STARTED) {
            view.onAnimationEnd();
            // Should be performed by onAnimationEnd() but this avoid an infinite loop,
            // so we'd rather be safe than sorry
            view.mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
            // Draw one more frame after the animation is done
            mGroupFlags |= FLAG_INVALIDATE_REQUIRED;
        }
    }

总结:

  • 假如子View执行了属性动画,而且在动画结束了时候设置子View的可见性为GONE,那么此时正常的逻辑是不允许子View再获取到手势事件,那么你应该执行View#clearAnimation()
  • 也可以不执行View#clearAnimation(),而在子View的OnTouchListener的返回值处判断当前view是否可见,不可见就不消费事件,返回值设置为false即可。
  • 上文中说到属性动画的fillAfter为true,设置为false也可以让子View不获取到手势事件。因为当设置了false,在动画结束的时候会自动调用view#clearAnimation();
  • 如果你不希望子View不放弃消费手势事件,那么你不执行上述三个方案即可。但这种一般都在子View的可见性为可见的情况下才符合程序员的惯性理解。(不可见还消费个毛线,可见当然要尝试消费)

Android使用补间动画的setFillAfter引发的问题

你可能感兴趣的:(补间动画和View手势事件)