美文网首页Bugs
layer-list被拉伸问题

layer-list被拉伸问题

作者: act262 | 来源:发表于2017-09-15 18:24 被阅读755次

    在 layer-list 中的自定义 shape设定的大小无效

    最近在做自定义 shape 效果时发现,自定义shape的大小在layer-list中无效了,较小的shape被拉伸至最大shape的大小了。(API23+没有此问题)
    把自定义的 shape 单独拿出来用却没有问题,唯独放在 LayerDrawable 出现问题.

    code

    原本想实现一个圆环中包含一个实心圆的 drawable, 写出来类似下面的代码,在 IDE 预览时也没啥问题,到了真机就有问题了.
    shape_layer.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:gravity="center">
            <shape
                android:innerRadius="15px"
                android:shape="ring"
                android:thickness="10px"
                android:useLevel="false">
                <size
                    android:width="50px"
                    android:height="50px" />
    
                <solid android:color="@color/colorAccent" />
            </shape>
        </item>
    
        <item android:gravity="center">
            <shape android:shape="oval">
                <size
                    android:width="20px"
                    android:height="20px" />
    
                <solid android:color="@color/colorPrimary" />
            </shape>
        </item>
    
    </layer-list>
    

    shape_layer_fixed. xml
    后面将这2层的顺序换了下,但是实心和环之间的空白还是没有,所以给设置了位置来限定大小.

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:gravity="center">
            <shape
                android:innerRadius="15px"
                android:shape="ring"
                android:thickness="10px"
                android:useLevel="false">
                <size
                    android:width="50px"
                    android:height="50px" />
    
                <solid android:color="@color/colorAccent" />
            </shape>
        </item>
    
        // 这里通过计算设置了其4个方向的 inset 值来保证不会覆盖上层的环
        <item
            android:bottom="20px"
            android:gravity="center"
            android:left="20px"
            android:right="20px"
            android:top="20px">
            <shape android:shape="oval">
                <size
                    android:width="20px"
                    android:height="20px" />
    
                <solid android:color="@color/colorPrimary" />
            </shape>
        </item>
    
    </layer-list>
    
    Anroid L MR1 以前 预览布局效果

    问题跟踪:

    LayerDrawable#onBoundsChange

        // API22以前 在Bound更新时直接把所有的子Drawable的bound设置成LayerDrawable的bound了
        @Override
        protected void onBoundsChange(Rect bounds) {
            int padL = 0;
            int padT = 0;
            int padR = 0;
            int padB = 0;
    
            final boolean nest = mLayerState.mPaddingMode == PADDING_MODE_NEST;
            final ChildDrawable[] array = mLayerState.mChildren;
            final int N = mLayerState.mNum;
            for (int i = 0; i < N; i++) {
                final ChildDrawable r = array[i];
                r.mDrawable.setBounds(bounds.left + r.mInsetL + padL, bounds.top + r.mInsetT + padT,
                        bounds.right - r.mInsetR - padR, bounds.bottom - r.mInsetB - padB);
    
                if (nest) {
                    padL += mPaddingL[i];
                    padR += mPaddingR[i];
                    padT += mPaddingT[i];
                    padB += mPaddingB[i];
                }
            }
        }
    

    LayerDrawable#onBoundsChange

    // API 23+ 不会直接使用容器的bound,而是通过相对位置得到
     private void updateLayerBoundsInternal(Rect bounds) {
            int paddingL = 0;
            int paddingT = 0;
            int paddingR = 0;
            int paddingB = 0;
    
            final Rect outRect = mTmpOutRect;
            final int layoutDirection = getLayoutDirection();
            final boolean isLayoutRtl = layoutDirection == LayoutDirection.RTL;
            final boolean isPaddingNested = mLayerState.mPaddingMode == PADDING_MODE_NEST;
            final ChildDrawable[] array = mLayerState.mChildren;
    
            for (int i = 0, count = mLayerState.mNum; i < count; i++) {
                final ChildDrawable r = array[i];
                final Drawable d = r.mDrawable;
                if (d == null) {
                    continue;
                }
    
                final int insetT = r.mInsetT;
                final int insetB = r.mInsetB;
    
                // Resolve insets for RTL. Relative insets override absolute
                // insets.
                final int insetRtlL = isLayoutRtl ? r.mInsetE : r.mInsetS;
                final int insetRtlR = isLayoutRtl ? r.mInsetS : r.mInsetE;
                final int insetL = insetRtlL == INSET_UNDEFINED ? r.mInsetL : insetRtlL;
                final int insetR = insetRtlR == INSET_UNDEFINED ? r.mInsetR : insetRtlR;
    
                // Establish containing region based on aggregate padding and
                // requested insets for the current layer.
                final Rect container = mTmpContainer;
                container.set(bounds.left + insetL + paddingL, bounds.top + insetT + paddingT,
                        bounds.right - insetR - paddingR, bounds.bottom - insetB - paddingB);
    
                // Compute a reasonable default gravity based on the intrinsic and
                // explicit dimensions, if specified.
                final int intrinsicW = d.getIntrinsicWidth();
                final int intrinsicH = d.getIntrinsicHeight();
                final int layerW = r.mWidth;
                final int layerH = r.mHeight;
                final int gravity = resolveGravity(r.mGravity, layerW, layerH, intrinsicW, intrinsicH);
    
                // Explicit dimensions override intrinsic dimensions.
                final int resolvedW = layerW < 0 ? intrinsicW : layerW;
                final int resolvedH = layerH < 0 ? intrinsicH : layerH;
                Gravity.apply(gravity, resolvedW, resolvedH, container, outRect, layoutDirection);
                d.setBounds(outRect);
    
                if (isPaddingNested) {
                    paddingL += mPaddingL[i];
                    paddingR += mPaddingR[i];
                    paddingT += mPaddingT[i];
                    paddingB += mPaddingB[i];
                }
            }
        }
    

    解决方案:

    旧版本不支持gravity 属性
    可以设置inset left,top,right,bottom 来指定偏移位置,从而得到层叠效果的Drawable

    相关文章

      网友评论

        本文标题:layer-list被拉伸问题

        本文链接:https://www.haomeiwen.com/subject/vufesxtx.html