Example usage for android.graphics.drawable GradientDrawable GradientDrawable

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

Introduction

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

Prototype

public GradientDrawable() 

Source Link

Usage

From source file:Main.java

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

    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);/* ww w  . jav  a2  s  .c  o m*/
    bg.setStroke(borderwidth, bordercolor);
    bg.setColor(bgColor);

    return bg;
}

From source file:com.example.android.materialme.SportsAdapter.java

/**
 * Constructor that passes in the sports data and the context
 * @param sportsData ArrayList containing the sports data
 * @param context Context of the application
 *///  w w w .ja v  a 2 s .  c o m
SportsAdapter(Context context, ArrayList<Sport> sportsData) {
    this.mSportsData = sportsData;
    this.mContext = context;

    //Prepare gray placeholder
    mGradientDrawable = new GradientDrawable();
    mGradientDrawable.setColor(Color.GRAY);

    //Make the placeholder same size as the images
    Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.img_badminton);
    if (drawable != null) {
        mGradientDrawable.setSize(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    }
}

From source file:com.example.android.materialme.DetailActivity.java

/**
 * Initializes the activity, filling in the data from the Intent.
 * @param savedInstanceState Contains information about the saved state of the activity
 *///www . j a  v a 2  s .c  o  m
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);

    //Initialize the views
    TextView sportsTitle = (TextView) findViewById(R.id.titleDetail);
    ImageView sportsImage = (ImageView) findViewById(R.id.sportsImageDetail);

    //Get the drawable
    Drawable drawable = ContextCompat.getDrawable(this, getIntent().getIntExtra(Sport.IMAGE_KEY, 0));

    //Create a placeholder gray scrim while the image loads
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setColor(Color.GRAY);

    //Make it the same size as the image
    if (drawable != null) {
        gradientDrawable.setSize(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    }

    //Set the text from the Intent extra
    sportsTitle.setText(getIntent().getStringExtra(Sport.TITLE_KEY));

    //Load the image using the glide library and the Intent extra
    Glide.with(this).load(getIntent().getIntExtra(Sport.IMAGE_KEY, 0)).placeholder(gradientDrawable)
            .into(sportsImage);
}

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 .ja v a  2s .  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   w  ww.j  a  v a  2s  .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 = 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;
}

From source file:com.fuzz.emptyhusk.prefab.ProportionalImageCellGenerator.java

@Override
public void onBindChild(@NonNull View child, @NonNull CutoutViewLayoutParams lp, @Nullable View originator) {
    child.setBackgroundResource(lp.cellBackgroundId);
    if (originator != null) {
        rvChildLength = originator.getHeight();
        if (originator.getParent() instanceof ViewGroup) {
            rvLength = ((ViewGroup) originator.getParent()).getHeight();
        }/*  w  w  w.  j a v  a  2 s. c  o m*/
    }
    if (child instanceof ImageView) {
        GradientDrawable elongated = new GradientDrawable();
        elongated.setShape(GradientDrawable.RECTANGLE);

        int accent = ContextCompat.getColor(child.getContext(), R.color.transparentColorAccent);

        float fractionOfParent = rvLength * 1.0f / rvChildLength;

        elongated.setColor(accent);
        float proposedLength = fractionOfParent * lp.perpendicularLength;
        elongated.setSize(lp.perpendicularLength, (int) proposedLength);

        ((ImageView) child).setImageDrawable(elongated);
    }
}

From source file:im.vector.view.UnreadCounterBadgeView.java

/**
 * Update the badge value and its status
 *
 * @param text   the new text value/*  ww w . j  a  va 2s. co  m*/
 * @param status the new status
 */
public void updateText(String text, @Status int status) {
    if (!TextUtils.isEmpty(text)) {
        mCounterTextView.setText(text);

        setVisibility(View.VISIBLE);
        GradientDrawable shape = new GradientDrawable();
        shape.setShape(GradientDrawable.RECTANGLE);
        shape.setCornerRadius(100);
        if (status == HIGHLIGHTED) {
            shape.setColor(ContextCompat.getColor(getContext(), R.color.vector_fuchsia_color));
        } else if (status == NOTIFIED) {
            shape.setColor(ContextCompat.getColor(getContext(), R.color.vector_green_color));
        } else { //if (status == DEFAULT)
            shape.setColor(ContextCompat.getColor(getContext(), R.color.vector_silver_color));
        }
        mParentView.setBackground(shape);
    } else {
        setVisibility(View.GONE);
    }
}

From source file:im.vector.adapters.GroupViewHolder.java

/**
 * Refresh the holder layout//from   w  w w .j  a  v a  2s . c  o m
 *
 * @param context                 the context
 * @param group                   the group
 * @param isInvitation            true if it is an invitation
 * @param moreGroupActionListener the more actions listener
 */
public void populateViews(final Context context, final MXSession session, final Group group,
        final AbsAdapter.GroupInvitationListener invitationListener, final boolean isInvitation,
        final AbsAdapter.MoreGroupActionListener moreGroupActionListener) {
    // sanity check
    if (null == group) {
        Log.e(LOG_TAG, "## populateViews() : null group");
        return;
    }

    if (isInvitation) {
        vGroupMembersCount.setText("!");
        vGroupMembersCount.setTypeface(null, Typeface.BOLD);
        GradientDrawable shape = new GradientDrawable();
        shape.setShape(GradientDrawable.RECTANGLE);
        shape.setCornerRadius(100);
        shape.setColor(ContextCompat.getColor(context, R.color.vector_fuchsia_color));
        vGroupMembersCount.setBackground(shape);
        vGroupMembersCount.setVisibility(View.VISIBLE);
    } else {
        vGroupMembersCount.setVisibility(View.GONE);
    }

    vGroupName.setText(group.getDisplayName());
    vGroupName.setTypeface(null, Typeface.NORMAL);

    VectorUtils.loadGroupAvatar(context, session, vGroupAvatar, group);

    vGroupTopic.setText(group.getShortDescription());

    if (vGroupMoreActionClickArea != null && vGroupMoreActionAnchor != null) {
        vGroupMoreActionClickArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != moreGroupActionListener) {
                    moreGroupActionListener.onMoreActionClick(vGroupMoreActionAnchor, group);
                }
            }
        });
    }
}

From source file:com.intirix.cloudpasswordmanager.pages.settings.SavePasswordOptionsViewHolder.java

private void addBorder(View view) {
    //use a GradientDrawable with only one color set, to make it a solid color
    GradientDrawable border = new GradientDrawable();
    border.setColor(0xFFFFFFFF); //white background
    border.setStroke(1, 0xFF000000); //black border with full opacity
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        view.setBackgroundDrawable(border);
    } else {//from   w  w  w  .j a  v  a2  s .c o  m
        view.setBackground(border);
    }
}