Example usage for android.graphics.drawable GradientDrawable setCornerRadii

List of usage examples for android.graphics.drawable GradientDrawable setCornerRadii

Introduction

In this page you can find the example usage for android.graphics.drawable GradientDrawable setCornerRadii.

Prototype

public void setCornerRadii(@Nullable float[] radii) 

Source Link

Document

Specifies radii for each of the 4 corners.

Usage

From source file:Main.java

public static Drawable cornerDrawable(final int bgColor, float[] cornerradius) {
    final GradientDrawable bg = new GradientDrawable();
    bg.setCornerRadii(cornerradius);
    bg.setColor(bgColor);/*ww w .j a  v  a  2s .c  o  m*/

    return bg;
}

From source file:Main.java

public static Drawable cornerDrawable(final int bgColor, float[] cornerradius, int borderwidth,
        int bordercolor) {
    final GradientDrawable bg = new GradientDrawable();
    bg.setCornerRadii(cornerradius);
    bg.setStroke(borderwidth, bordercolor);
    bg.setColor(bgColor);//from   w w  w.j a va 2  s  . c  o  m

    return bg;
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static void setCorners(GradientDrawable drawable, float tl, float tr, float br, float bl) {

    float[] radii = null;

    radii = new float[] { tl, tl, tr, tr, br, br, bl, bl };

    drawable.setCornerRadii(radii);

}

From source file:org.dalol.orthodoxmezmurmedia.utilities.widgets.RecyclerViewFastIndexer.java

protected void init(Context context) {
    if (isInitialized)
        return;//from ww  w. j  a va2s. c o m
    isInitialized = true;
    setOrientation(HORIZONTAL);
    setClipChildren(false);
    setWillNotDraw(false);

    bubble = new TextView(context);
    bubble.setTextColor(Color.WHITE);
    bubble.setTextSize(48);

    GradientDrawable bubbleDrawable = new GradientDrawable();
    bubbleDrawable.setColor(0xFFce891e);
    bubbleDrawable.setSize(getCustomSize(88), getCustomSize(88));
    bubbleDrawable.setCornerRadii(new float[] { getCustomSize(44), getCustomSize(44), getCustomSize(44),
            getCustomSize(44), 0, 0, getCustomSize(44), getCustomSize(44) });

    bubble.setBackgroundDrawable(bubbleDrawable);

    //bubble.setBackgroundResource(R.drawable.recycler_view_fast_scroller__bubble);
    bubble.setGravity(Gravity.CENTER);

    LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.RIGHT | Gravity.END;

    addView(bubble, params);

    if (bubble != null)
        bubble.setVisibility(INVISIBLE);

    handle = new ImageView(context);
    //handle.setBackgroundResource(R.drawable.recycler_view_fast_scroller__handle);

    GradientDrawable drawable = new GradientDrawable();
    drawable.setShape(GradientDrawable.RECTANGLE);
    drawable.setSize(getCustomSize(4), getCustomSize(35));
    drawable.setColor(0xFFce891e);
    drawable.setCornerRadius(getCustomSize(5));

    GradientDrawable drawable2 = new GradientDrawable();
    drawable2.setShape(GradientDrawable.RECTANGLE);
    drawable2.setSize(getCustomSize(4), getCustomSize(35));
    drawable2.setColor(0xFFf3a124);
    drawable2.setCornerRadius(getCustomSize(5));

    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_selected }, drawable);
    states.addState(new int[] {}, drawable2);

    handle.setBackgroundDrawable(states);

    LayoutParams params2 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    int dimension = getCustomSize(3);

    int doubleDimension = dimension * 2;
    params2.leftMargin = doubleDimension;
    params2.rightMargin = doubleDimension;

    handle.setPadding(dimension, 0, dimension, 0);
    handle.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                v.setBackgroundColor(Color.parseColor("#00891e"));
            } else {
                v.setBackgroundColor(Color.parseColor("#44891e"));
            }
        }
    });

    addView(handle, params2);

}

From source file:com.adithyaupadhya.uimodule.roundcornerprogressbar.BaseRoundCornerProgressBar.java

@SuppressWarnings("deprecation")
private void drawBackgroundProgress() {
    GradientDrawable backgroundDrawable = createGradientDrawable(colorBackground);
    int newRadius = radius - (padding / 2);
    backgroundDrawable.setCornerRadii(new float[] { newRadius, newRadius, newRadius, newRadius, newRadius,
            newRadius, newRadius, newRadius });
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        layoutBackground.setBackground(backgroundDrawable);
    } else {/* w w  w. j a  v  a2 s.  c  om*/
        layoutBackground.setBackgroundDrawable(backgroundDrawable);
    }
}

From source file:com.nickandjerry.dynamiclayoutinflator.DynamicLayoutInflator.java

@SuppressLint("NewApi")
private static void applyAttributes(View view, Map<String, String> attrs, ViewGroup parent) {
    if (viewRunnables == null)
        createViewRunnables();/*from  ww  w  .ja va  2s . co m*/
    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
    int layoutRule;
    int marginLeft = 0, marginRight = 0, marginTop = 0, marginBottom = 0, paddingLeft = 0, paddingRight = 0,
            paddingTop = 0, paddingBottom = 0;
    boolean hasCornerRadius = false, hasCornerRadii = false;
    for (Map.Entry<String, String> entry : attrs.entrySet()) {
        String attr = entry.getKey();
        if (viewRunnables.containsKey(attr)) {
            viewRunnables.get(attr).apply(view, entry.getValue(), parent, attrs);
            continue;
        }
        if (attr.startsWith("cornerRadius")) {
            hasCornerRadius = true;
            hasCornerRadii = !attr.equals("cornerRadius");
            continue;
        }
        layoutRule = NO_LAYOUT_RULE;
        boolean layoutTarget = false;
        switch (attr) {
        case "id":
            String idValue = parseId(entry.getValue());
            if (parent != null) {
                DynamicLayoutInfo info = getDynamicLayoutInfo(parent);
                int newId = highestIdNumberUsed++;
                view.setId(newId);
                info.nameToIdNumber.put(idValue, newId);
            }
            break;
        case "width":
        case "layout_width":
            switch (entry.getValue()) {
            case "wrap_content":
                layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                break;
            case "fill_parent":
            case "match_parent":
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                break;
            default:
                layoutParams.width = DimensionConverter.stringToDimensionPixelSize(entry.getValue(),
                        view.getResources().getDisplayMetrics(), parent, true);
                break;
            }
            break;
        case "height":
        case "layout_height":
            switch (entry.getValue()) {
            case "wrap_content":
                layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                break;
            case "fill_parent":
            case "match_parent":
                layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                break;
            default:
                layoutParams.height = DimensionConverter.stringToDimensionPixelSize(entry.getValue(),
                        view.getResources().getDisplayMetrics(), parent, false);
                break;
            }
            break;
        case "layout_gravity":
            if (parent != null && parent instanceof LinearLayout) {
                ((LinearLayout.LayoutParams) layoutParams).gravity = parseGravity(entry.getValue());
            } else if (parent != null && parent instanceof FrameLayout) {
                ((FrameLayout.LayoutParams) layoutParams).gravity = parseGravity(entry.getValue());
            }
            break;
        case "layout_weight":
            if (parent != null && parent instanceof LinearLayout) {
                ((LinearLayout.LayoutParams) layoutParams).weight = Float.parseFloat(entry.getValue());
            }
            break;
        case "layout_below":
            layoutRule = RelativeLayout.BELOW;
            layoutTarget = true;
            break;
        case "layout_above":
            layoutRule = RelativeLayout.ABOVE;
            layoutTarget = true;
            break;
        case "layout_toLeftOf":
            layoutRule = RelativeLayout.LEFT_OF;
            layoutTarget = true;
            break;
        case "layout_toRightOf":
            layoutRule = RelativeLayout.RIGHT_OF;
            layoutTarget = true;
            break;
        case "layout_alignBottom":
            layoutRule = RelativeLayout.ALIGN_BOTTOM;
            layoutTarget = true;
            break;
        case "layout_alignTop":
            layoutRule = RelativeLayout.ALIGN_TOP;
            layoutTarget = true;
            break;
        case "layout_alignLeft":
        case "layout_alignStart":
            layoutRule = RelativeLayout.ALIGN_LEFT;
            layoutTarget = true;
            break;
        case "layout_alignRight":
        case "layout_alignEnd":
            layoutRule = RelativeLayout.ALIGN_RIGHT;
            layoutTarget = true;
            break;
        case "layout_alignParentBottom":
            layoutRule = RelativeLayout.ALIGN_PARENT_BOTTOM;
            break;
        case "layout_alignParentTop":
            layoutRule = RelativeLayout.ALIGN_PARENT_TOP;
            break;
        case "layout_alignParentLeft":
        case "layout_alignParentStart":
            layoutRule = RelativeLayout.ALIGN_PARENT_LEFT;
            break;
        case "layout_alignParentRight":
        case "layout_alignParentEnd":
            layoutRule = RelativeLayout.ALIGN_PARENT_RIGHT;
            break;
        case "layout_centerHorizontal":
            layoutRule = RelativeLayout.CENTER_HORIZONTAL;
            break;
        case "layout_centerVertical":
            layoutRule = RelativeLayout.CENTER_VERTICAL;
            break;
        case "layout_centerInParent":
            layoutRule = RelativeLayout.CENTER_IN_PARENT;
            break;
        case "layout_margin":
            marginLeft = marginRight = marginTop = marginBottom = DimensionConverter
                    .stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics());
            break;
        case "layout_marginLeft":
            marginLeft = DimensionConverter.stringToDimensionPixelSize(entry.getValue(),
                    view.getResources().getDisplayMetrics(), parent, true);
            break;
        case "layout_marginTop":
            marginTop = DimensionConverter.stringToDimensionPixelSize(entry.getValue(),
                    view.getResources().getDisplayMetrics(), parent, false);
            break;
        case "layout_marginRight":
            marginRight = DimensionConverter.stringToDimensionPixelSize(entry.getValue(),
                    view.getResources().getDisplayMetrics(), parent, true);
            break;
        case "layout_marginBottom":
            marginBottom = DimensionConverter.stringToDimensionPixelSize(entry.getValue(),
                    view.getResources().getDisplayMetrics(), parent, false);
            break;
        case "padding":
            paddingBottom = paddingLeft = paddingRight = paddingTop = DimensionConverter
                    .stringToDimensionPixelSize(entry.getValue(), view.getResources().getDisplayMetrics());
            break;
        case "paddingLeft":
            paddingLeft = DimensionConverter.stringToDimensionPixelSize(entry.getValue(),
                    view.getResources().getDisplayMetrics());
            break;
        case "paddingTop":
            paddingTop = DimensionConverter.stringToDimensionPixelSize(entry.getValue(),
                    view.getResources().getDisplayMetrics());
            break;
        case "paddingRight":
            paddingRight = DimensionConverter.stringToDimensionPixelSize(entry.getValue(),
                    view.getResources().getDisplayMetrics());
            break;
        case "paddingBottom":
            paddingBottom = DimensionConverter.stringToDimensionPixelSize(entry.getValue(),
                    view.getResources().getDisplayMetrics());
            break;

        }
        if (layoutRule != NO_LAYOUT_RULE && parent instanceof RelativeLayout) {
            if (layoutTarget) {
                int anchor = idNumFromIdString(parent, parseId(entry.getValue()));
                ((RelativeLayout.LayoutParams) layoutParams).addRule(layoutRule, anchor);
            } else if (entry.getValue().equals("true")) {
                ((RelativeLayout.LayoutParams) layoutParams).addRule(layoutRule);
            }
        }
    }
    // TODO: this is a giant mess; come up with a simpler way of deciding what to draw for the background
    if (attrs.containsKey("background") || attrs.containsKey("borderColor")) {
        String bgValue = attrs.containsKey("background") ? attrs.get("background") : null;
        if (bgValue != null && bgValue.startsWith("@drawable/")) {
            view.setBackground(getDrawableByName(view, bgValue));
        } else if (bgValue == null || bgValue.startsWith("#") || bgValue.startsWith("@color")) {
            if (view instanceof Button || attrs.containsKey("pressedColor")) {
                int bgColor = parseColor(view, bgValue == null ? "#00000000" : bgValue);
                int pressedColor;
                if (attrs.containsKey("pressedColor")) {
                    pressedColor = parseColor(view, attrs.get("pressedColor"));
                } else {
                    pressedColor = adjustBrightness(bgColor, 0.9f);
                }
                GradientDrawable gd = new GradientDrawable();
                gd.setColor(bgColor);
                GradientDrawable pressedGd = new GradientDrawable();
                pressedGd.setColor(pressedColor);
                if (hasCornerRadii) {
                    float radii[] = new float[8];
                    for (int i = 0; i < CORNERS.length; i++) {
                        String corner = CORNERS[i];
                        if (attrs.containsKey("cornerRadius" + corner)) {
                            radii[i * 2] = radii[i * 2 + 1] = DimensionConverter.stringToDimension(
                                    attrs.get("cornerRadius" + corner),
                                    view.getResources().getDisplayMetrics());
                        }
                        gd.setCornerRadii(radii);
                        pressedGd.setCornerRadii(radii);
                    }
                } else if (hasCornerRadius) {
                    float cornerRadius = DimensionConverter.stringToDimension(attrs.get("cornerRadius"),
                            view.getResources().getDisplayMetrics());
                    gd.setCornerRadius(cornerRadius);
                    pressedGd.setCornerRadius(cornerRadius);
                }
                if (attrs.containsKey("borderColor")) {
                    String borderWidth = "1dp";
                    if (attrs.containsKey("borderWidth")) {
                        borderWidth = attrs.get("borderWidth");
                    }
                    int borderWidthPx = DimensionConverter.stringToDimensionPixelSize(borderWidth,
                            view.getResources().getDisplayMetrics());
                    gd.setStroke(borderWidthPx, parseColor(view, attrs.get("borderColor")));
                    pressedGd.setStroke(borderWidthPx, parseColor(view, attrs.get("borderColor")));
                }
                StateListDrawable selector = new StateListDrawable();
                selector.addState(new int[] { android.R.attr.state_pressed }, pressedGd);
                selector.addState(new int[] {}, gd);
                view.setBackground(selector);
                getDynamicLayoutInfo(view).bgDrawable = gd;
            } else if (hasCornerRadius || attrs.containsKey("borderColor")) {
                GradientDrawable gd = new GradientDrawable();
                int bgColor = parseColor(view, bgValue == null ? "#00000000" : bgValue);
                gd.setColor(bgColor);
                if (hasCornerRadii) {
                    float radii[] = new float[8];
                    for (int i = 0; i < CORNERS.length; i++) {
                        String corner = CORNERS[i];
                        if (attrs.containsKey("cornerRadius" + corner)) {
                            radii[i * 2] = radii[i * 2 + 1] = DimensionConverter.stringToDimension(
                                    attrs.get("cornerRadius" + corner),
                                    view.getResources().getDisplayMetrics());
                        }
                        gd.setCornerRadii(radii);
                    }
                } else if (hasCornerRadius) {
                    float cornerRadius = DimensionConverter.stringToDimension(attrs.get("cornerRadius"),
                            view.getResources().getDisplayMetrics());
                    gd.setCornerRadius(cornerRadius);
                }
                if (attrs.containsKey("borderColor")) {
                    String borderWidth = "1dp";
                    if (attrs.containsKey("borderWidth")) {
                        borderWidth = attrs.get("borderWidth");
                    }
                    gd.setStroke(
                            DimensionConverter.stringToDimensionPixelSize(borderWidth,
                                    view.getResources().getDisplayMetrics()),
                            parseColor(view, attrs.get("borderColor")));
                }
                view.setBackground(gd);
                getDynamicLayoutInfo(view).bgDrawable = gd;
            } else {
                view.setBackgroundColor(parseColor(view, bgValue));
            }
        }
    }

    if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
        ((ViewGroup.MarginLayoutParams) layoutParams).setMargins(marginLeft, marginTop, marginRight,
                marginBottom);
    }
    view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
    view.setLayoutParams(layoutParams);
}

From source file:com.bilibili.magicasakura.utils.GradientDrawableUtils.java

@Override
protected Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs)
        throws XmlPullParserException, IOException {
    GradientDrawable gradientDrawable = new GradientDrawable();
    inflateGradientRootElement(context, attrs, gradientDrawable);

    int type;/*from w w  w .j  a v a2  s. c  o m*/
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }

        if (depth > innerDepth) {
            continue;
        }

        String name = parser.getName();

        if (name.equals("size")) {
            final int width = getAttrDimensionPixelSize(context, attrs, android.R.attr.width);
            final int height = getAttrDimensionPixelSize(context, attrs, android.R.attr.height);
            gradientDrawable.setSize(width, height);
        } else if (name.equals("gradient")
                && Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            final float centerX = getAttrFloatOrFraction(context, attrs, android.R.attr.centerX, 0.5f, 1.0f,
                    1.0f);
            final float centerY = getAttrFloatOrFraction(context, attrs, android.R.attr.centerY, 0.5f, 1.0f,
                    1.0f);
            gradientDrawable.setGradientCenter(centerX, centerY);
            final boolean useLevel = getAttrBoolean(context, attrs, android.R.attr.useLevel, false);
            gradientDrawable.setUseLevel(useLevel);
            final int gradientType = getAttrInt(context, attrs, android.R.attr.type, 0);
            gradientDrawable.setGradientType(gradientType);
            final int startColor = getAttrColor(context, attrs, android.R.attr.startColor, Color.TRANSPARENT);
            final int centerColor = getAttrColor(context, attrs, android.R.attr.centerColor, Color.TRANSPARENT);
            final int endColor = getAttrColor(context, attrs, android.R.attr.endColor, Color.TRANSPARENT);
            if (!getAttrHasValue(context, attrs, android.R.attr.centerColor)) {
                gradientDrawable.setColors(new int[] { startColor, endColor });
            } else {
                gradientDrawable.setColors(new int[] { startColor, centerColor, endColor });
                setStGradientPositions(gradientDrawable.getConstantState(), 0.0f,
                        centerX != 0.5f ? centerX : centerY, 1f);
            }

            if (gradientType == GradientDrawable.LINEAR_GRADIENT) {
                int angle = (int) getAttrFloat(context, attrs, android.R.attr.angle, 0.0f);
                angle %= 360;

                if (angle % 45 != 0) {
                    throw new XmlPullParserException(
                            "<gradient> tag requires" + "'angle' attribute to " + "be a multiple of 45");
                }

                setStGradientAngle(gradientDrawable.getConstantState(), angle);

                switch (angle) {
                case 0:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
                    break;
                case 45:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BL_TR);
                    break;
                case 90:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BOTTOM_TOP);
                    break;
                case 135:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BR_TL);
                    break;
                case 180:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.RIGHT_LEFT);
                    break;
                case 225:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TR_BL);
                    break;
                case 270:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
                    break;
                case 315:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TL_BR);
                    break;
                }
            } else {
                setGradientRadius(context, attrs, gradientDrawable, gradientType);
            }
        } else if (name.equals("solid")) {
            int color = getAttrColor(context, attrs, android.R.attr.color, Color.TRANSPARENT);
            gradientDrawable
                    .setColor(getAlphaColor(color, getAttrFloat(context, attrs, android.R.attr.alpha, 1.0f)));
        } else if (name.equals("stroke")) {
            final float alphaMod = getAttrFloat(context, attrs, android.R.attr.alpha, 1.0f);
            final int strokeColor = getAttrColor(context, attrs, android.R.attr.color, Color.TRANSPARENT);
            final int strokeWidth = getAttrDimensionPixelSize(context, attrs, android.R.attr.width);
            final float dashWidth = getAttrDimension(context, attrs, android.R.attr.dashWidth);
            if (dashWidth != 0.0f) {
                final float dashGap = getAttrDimension(context, attrs, android.R.attr.dashGap);
                gradientDrawable.setStroke(strokeWidth, getAlphaColor(strokeColor, alphaMod), dashWidth,
                        dashGap);
            } else {
                gradientDrawable.setStroke(strokeWidth, getAlphaColor(strokeColor, alphaMod));
            }
        } else if (name.equals("corners")) {
            final int radius = getAttrDimensionPixelSize(context, attrs, android.R.attr.radius);
            gradientDrawable.setCornerRadius(radius);

            final int topLeftRadius = getAttrDimensionPixelSize(context, attrs, android.R.attr.topLeftRadius,
                    radius);
            final int topRightRadius = getAttrDimensionPixelSize(context, attrs, android.R.attr.topRightRadius,
                    radius);
            final int bottomLeftRadius = getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.bottomLeftRadius, radius);
            final int bottomRightRadius = getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.bottomRightRadius, radius);
            if (topLeftRadius != radius || topRightRadius != radius || bottomLeftRadius != radius
                    || bottomRightRadius != radius) {
                // The corner radii are specified in clockwise order (see Path.addRoundRect())
                gradientDrawable.setCornerRadii(
                        new float[] { topLeftRadius, topLeftRadius, topRightRadius, topRightRadius,
                                bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius });
            }
        } else if (name.equals("padding")) {
            final int paddingLeft = getAttrDimensionPixelOffset(context, attrs, android.R.attr.left);
            final int paddingTop = getAttrDimensionPixelOffset(context, attrs, android.R.attr.top);
            final int paddingRight = getAttrDimensionPixelOffset(context, attrs, android.R.attr.right);
            final int paddingBottom = getAttrDimensionPixelOffset(context, attrs, android.R.attr.bottom);
            if (paddingLeft != 0 || paddingTop != 0 || paddingRight != 0 || paddingBottom != 0) {
                final Rect pad = new Rect();
                pad.set(paddingLeft, paddingTop, paddingRight, paddingBottom);
                try {
                    if (sPaddingField == null) {
                        sPaddingField = GradientDrawable.class.getDeclaredField("mPadding");
                        sPaddingField.setAccessible(true);
                    }
                    sPaddingField.set(gradientDrawable, pad);
                    if (sStPaddingField == null) {
                        sStPaddingField = Class
                                .forName("android.graphics.drawable.GradientDrawable$GradientState")
                                .getDeclaredField("mPadding");
                        sStPaddingField.setAccessible(true);
                    }
                    sStPaddingField.set(gradientDrawable.getConstantState(), pad);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } else {
            Log.w("drawable", "Bad element under <shape>: " + name);
        }
    }
    return gradientDrawable;
}

From source file:com.bilibili.magicasakura.utils.GradientDrawableInflateImpl.java

@Override
public Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs)
        throws XmlPullParserException, IOException {
    GradientDrawable gradientDrawable = new GradientDrawable();
    inflateGradientRootElement(context, attrs, gradientDrawable);

    int type;/*from ww  w .j av  a 2 s .c  om*/
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }

        if (depth > innerDepth) {
            continue;
        }

        String name = parser.getName();

        if (name.equals("size")) {
            final int width = DrawableUtils.getAttrDimensionPixelSize(context, attrs, android.R.attr.width);
            final int height = DrawableUtils.getAttrDimensionPixelSize(context, attrs, android.R.attr.height);
            gradientDrawable.setSize(width, height);
        } else if (name.equals("gradient")
                && Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            final float centerX = getAttrFloatOrFraction(context, attrs, android.R.attr.centerX, 0.5f, 1.0f,
                    1.0f);
            final float centerY = getAttrFloatOrFraction(context, attrs, android.R.attr.centerY, 0.5f, 1.0f,
                    1.0f);
            gradientDrawable.setGradientCenter(centerX, centerY);
            final boolean useLevel = DrawableUtils.getAttrBoolean(context, attrs, android.R.attr.useLevel,
                    false);
            gradientDrawable.setUseLevel(useLevel);
            final int gradientType = DrawableUtils.getAttrInt(context, attrs, android.R.attr.type, 0);
            gradientDrawable.setGradientType(gradientType);
            final int startColor = DrawableUtils.getAttrColor(context, attrs, android.R.attr.startColor,
                    Color.TRANSPARENT);
            final int centerColor = DrawableUtils.getAttrColor(context, attrs, android.R.attr.centerColor,
                    Color.TRANSPARENT);
            final int endColor = DrawableUtils.getAttrColor(context, attrs, android.R.attr.endColor,
                    Color.TRANSPARENT);
            if (!DrawableUtils.getAttrHasValue(context, attrs, android.R.attr.centerColor)) {
                gradientDrawable.setColors(new int[] { startColor, endColor });
            } else {
                gradientDrawable.setColors(new int[] { startColor, centerColor, endColor });
                setStGradientPositions(gradientDrawable.getConstantState(), 0.0f,
                        centerX != 0.5f ? centerX : centerY, 1f);
            }

            if (gradientType == GradientDrawable.LINEAR_GRADIENT) {
                int angle = (int) DrawableUtils.getAttrFloat(context, attrs, android.R.attr.angle, 0.0f);
                angle %= 360;

                if (angle % 45 != 0) {
                    throw new XmlPullParserException(
                            "<gradient> tag requires" + "'angle' attribute to " + "be a multiple of 45");
                }

                setStGradientAngle(gradientDrawable.getConstantState(), angle);

                switch (angle) {
                case 0:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
                    break;
                case 45:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BL_TR);
                    break;
                case 90:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BOTTOM_TOP);
                    break;
                case 135:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.BR_TL);
                    break;
                case 180:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.RIGHT_LEFT);
                    break;
                case 225:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TR_BL);
                    break;
                case 270:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
                    break;
                case 315:
                    gradientDrawable.setOrientation(GradientDrawable.Orientation.TL_BR);
                    break;
                }
            } else {
                setGradientRadius(context, attrs, gradientDrawable, gradientType);
            }
        } else if (name.equals("solid")) {
            int color = DrawableUtils.getAttrColor(context, attrs, android.R.attr.color, Color.TRANSPARENT);
            gradientDrawable.setColor(getAlphaColor(color,
                    DrawableUtils.getAttrFloat(context, attrs, android.R.attr.alpha, 1.0f)));
        } else if (name.equals("stroke")) {
            final float alphaMod = DrawableUtils.getAttrFloat(context, attrs, android.R.attr.alpha, 1.0f);
            final int strokeColor = DrawableUtils.getAttrColor(context, attrs, android.R.attr.color,
                    Color.TRANSPARENT);
            final int strokeWidth = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.width);
            final float dashWidth = DrawableUtils.getAttrDimension(context, attrs, android.R.attr.dashWidth);
            if (dashWidth != 0.0f) {
                final float dashGap = DrawableUtils.getAttrDimension(context, attrs, android.R.attr.dashGap);
                gradientDrawable.setStroke(strokeWidth, getAlphaColor(strokeColor, alphaMod), dashWidth,
                        dashGap);
            } else {
                gradientDrawable.setStroke(strokeWidth, getAlphaColor(strokeColor, alphaMod));
            }
        } else if (name.equals("corners")) {
            final int radius = DrawableUtils.getAttrDimensionPixelSize(context, attrs, android.R.attr.radius);
            gradientDrawable.setCornerRadius(radius);

            final int topLeftRadius = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.topLeftRadius, radius);
            final int topRightRadius = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.topRightRadius, radius);
            final int bottomLeftRadius = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.bottomLeftRadius, radius);
            final int bottomRightRadius = DrawableUtils.getAttrDimensionPixelSize(context, attrs,
                    android.R.attr.bottomRightRadius, radius);
            if (topLeftRadius != radius || topRightRadius != radius || bottomLeftRadius != radius
                    || bottomRightRadius != radius) {
                // The corner radii are specified in clockwise order (see Path.addRoundRect())
                gradientDrawable.setCornerRadii(
                        new float[] { topLeftRadius, topLeftRadius, topRightRadius, topRightRadius,
                                bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius });
            }
        } else if (name.equals("padding")) {
            final int paddingLeft = DrawableUtils.getAttrDimensionPixelOffset(context, attrs,
                    android.R.attr.left);
            final int paddingTop = DrawableUtils.getAttrDimensionPixelOffset(context, attrs,
                    android.R.attr.top);
            final int paddingRight = DrawableUtils.getAttrDimensionPixelOffset(context, attrs,
                    android.R.attr.right);
            final int paddingBottom = DrawableUtils.getAttrDimensionPixelOffset(context, attrs,
                    android.R.attr.bottom);
            if (paddingLeft != 0 || paddingTop != 0 || paddingRight != 0 || paddingBottom != 0) {
                final Rect pad = new Rect();
                pad.set(paddingLeft, paddingTop, paddingRight, paddingBottom);
                try {
                    if (sPaddingField == null) {
                        sPaddingField = GradientDrawable.class.getDeclaredField("mPadding");
                        sPaddingField.setAccessible(true);
                    }
                    sPaddingField.set(gradientDrawable, pad);
                    if (sStPaddingField == null) {
                        sStPaddingField = Class
                                .forName("android.graphics.drawable.GradientDrawable$GradientState")
                                .getDeclaredField("mPadding");
                        sStPaddingField.setAccessible(true);
                    }
                    sStPaddingField.set(gradientDrawable.getConstantState(), pad);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } else {
            Log.w("drawable", "Bad element under <shape>: " + name);
        }
    }
    return gradientDrawable;
}