Android控制View绘制顺序的关键方法——setChildrenDrawingOrderEnabled

本文讲述了在华数TVAPP的Viewgroup(RecyclerView)中,如何处理获取焦点的item高亮、放大和阴影效果,避免因间距不足导致被后添加的child遮挡的问题,方法是设置childrenDrawingOrderEnabled为true并重写getChildDrawingOrder方法以确保焦点项最后绘制。
摘要由CSDN通过智能技术生成

问题:

华数TV APP上Viewgroup(这里是recyclerview)添加item的childview,获取焦点的item要求高亮、放大、阴影,但是item之间的间距如果不足,单个item被放大会导致该focusView右边一部分被后面添加的child遮盖。

原因:

造成这种结果的原因是,groupview的child绘制顺序是从左到右顺序来摆放绘制的,后面绘制的child与前面添加的child有重叠时,会盖在前面child上面,需要修改绘制顺序,获取焦点的child最后绘制。

修改绘制顺序原理

如下图,子view的绘制在ViewGroup#dispatchDraw()循环中,

    @Override
    protected void dispatchDraw(Canvas canvas) {
        final int childrenCount = mChildrenCount;
        final View[] children = mChildren;
  		// ......
        // Only use the preordered list if not HW accelerated, since the HW pipeline will do the
        // draw reordering internally
        final ArrayList<View> preorderedList = drawsWithRenderNode(canvas)
                ? null : buildOrderedChildList();
        final boolean customOrder = preorderedList == null
                && isChildrenDrawingOrderEnabled();
        for (int i = 0; i < childrenCount; i++) {
            while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) {
                final View transientChild = mTransientViews.get(transientIndex);
                if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
                        transientChild.getAnimation() != null) {
                    more |= drawChild(canvas, transientChild, drawingTime);
                }
                transientIndex++;
                if (transientIndex >= transientCount) {
                    transientIndex = -1;
                }
            }

            final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
            final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
            if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
                more |= drawChild(canvas, child, drawingTime);
            }
        }
      	// ......
        if (preorderedList != null) preorderedList.clear();

        // Draw any disappearing views that have animations
        if (mDisappearingChildren != null) {
            final ArrayList<View> disappearingChildren = mDisappearingChildren;
            final int disappearingCount = disappearingChildren.size() - 1;
            // Go backwards -- we may delete as animations finish
            for (int i = disappearingCount; i >= 0; i--) {
                final View child = disappearingChildren.get(i);
                more |= drawChild(canvas, child, drawingTime);
            }
        }
        canvas.disableZ();

        if (isShowingLayoutBounds()) {
            onDebugDraw(canvas);
        }

        if (clipToPadding) {
            canvas.restoreToCount(clipSaveCount);
        }

        // mGroupFlags might have been updated by drawChild()
        flags = mGroupFlags;

        if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) {
            invalidate(true);
        }

        if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 &&
                mLayoutAnimationController.isDone() && !more) {
            // We want to erase the drawing cache and notify the listener after the
            // next frame is drawn because one extra invalidate() is caused by
            // drawChild() after the animation is over
            mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER;
            final Runnable end = new Runnable() {
               @Override
               public void run() {
                   notifyAnimationListener();
               }
            };
            post(end);
        }
    }

可以看出,child是由getAndVerifyPreorderedView方法中返回的。

    private static View getAndVerifyPreorderedView(ArrayList<View> preorderedList, View[] children,
            int childIndex) {
        final View child;
        if (preorderedList != null) {
            child = preorderedList.get(childIndex);
            if (child == null) {
                throw new RuntimeException("Invalid preorderedList contained null child at index "
                        + childIndex);
            }
        } else {
            child = children[childIndex];
        }
        return child;
    }

可以看出,当preorderedList不为空,子view是从preorderedList取的,该preorderedList列表是通过buildOrderedChildList方法拿到的。

   ArrayList<View> buildOrderedChildList() {
        final int childrenCount = mChildrenCount;
        if (childrenCount <= 1 || !hasChildWithZ()) return null;

        if (mPreSortedChildren == null) {
            mPreSortedChildren = new ArrayList<>(childrenCount);
        } else {
            // callers should clear, so clear shouldn't be necessary, but for safety...
            mPreSortedChildren.clear();
            mPreSortedChildren.ensureCapacity(childrenCount);
        }

        final boolean customOrder = isChildrenDrawingOrderEnabled();
        for (int i = 0; i < childrenCount; i++) {
            // add next child (in child order) to end of list
            final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
            final View nextChild = mChildren[childIndex];
            final float currentZ = nextChild.getZ();

            // insert ahead of any Views with greater Z
            int insertIndex = i;
            while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) {
                insertIndex--;
            }
            mPreSortedChildren.add(insertIndex, nextChild);
        }
        return mPreSortedChildren;
    }

buildOrderedChildList方法中通过getAndVerifyPreorderedIndex方法返回子child的索引,该方法需要传入一个customOrder的boolean值。

  private int getAndVerifyPreorderedIndex(int childrenCount, int i, boolean customOrder) {
        final int childIndex;
        if (customOrder) {
            final int childIndex1 = getChildDrawingOrder(childrenCount, i);
            if (childIndex1 >= childrenCount) {
                throw new IndexOutOfBoundsException("getChildDrawingOrder() "
                        + "returned invalid index " + childIndex1
                        + " (child count is " + childrenCount + ")");
            }
            childIndex = childIndex1;
        } else {
            childIndex = i;
        }
        return childIndex;
    }

可以看到,getAndVerifyPreorderedIndex方法中需要customOrder值来判断开启自定义绘制子view的顺序。customOrder为true,通过getChildDrawingOrder方法来控制返回的索引。customOrder 的Boolean值通过setChildrenDrawingOrderEnabled来设置的。

protected int getChildDrawingOrder(int childCount, int drawingPosition) {
        return drawingPosition;
    }

getChildDrawingOrder方法默认返回i,默认从0到childrenCount顺序角标来从mChildren取子view并进行绘制。需要重写getChildDrawingOrder方法来改变角标索引顺序,来控制子view绘制顺序。

解决方案

总结来说,需要两步来实现控制Recyclerview中子view的绘制顺序:
(1)recyclerview#setChildrenDrawingOrderEnabled设置为true。

recyclerview.setChildrenDrawingOrderEnabled(true);

(2)重写getChildDrawingOrder的方法,将获取焦点的view对应返回最后一个索引。

  @Override
    protected int getChildDrawingOrder(int childCount, int i) {
        View focusedChild = this.getFocusedChild();
        if (null != focusedChild) {
            int position = this.indexOfChild(focusedChild);
            if (i == childCount - 1) {
                if (position > i) {
                    position = i;
                }
                return position;
            }
            
            if (position == i) {
                return childCount -1;
            }
        }
        return i;
    }
  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值