您的位置:首页 > 移动开发 > Android开发

android 5.0 支持的z轴实现机制

2015-05-24 13:05 330 查看
android 5.0 要支持 z 轴效果,view 的先后顺序就得随着 z 轴上的位置可以动态的调整,view 先后位置的确定是怎么计算的呢,可以看看

ViewGroup 里面的 buildOrderedChildList 这个方法

[code]    /**
     * Populates (and returns) mPreSortedChildren with a pre-ordered list of the View's children, sorted first by Z, then by child drawing order (if applicable). This list must be cleared after use to avoid leaking child Views. Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated children.
     */
    ArrayList<View> buildOrderedChildList() {
        final int count = mChildrenCount;
        if (count <= 1 || !hasChildWithZ()) return null;

        if (mPreSortedChildren == null) {
            mPreSortedChildren = new ArrayList<View>(count); 
        } else {
            mPreSortedChildren.ensureCapacity(count);
        }

        final boolean useCustomOrder = isChildrenDrawingOrderEnabled();
        for (int i = 0; i < mChildrenCount; i++) {
            // add next child (in child order) to end of list
            int childIndex = useCustomOrder ? getChildDrawingOrder(mChildrenCount, i) : i;
            View nextChild = mChildren[childIndex];
            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;
    }


该方法核心思想是按照 view 在 z 轴上的距离,使用插入法重新排序,放入新的列表中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: