Java Code Examples for android.graphics.drawable.GradientDrawable#setShape()

The following examples show how to use android.graphics.drawable.GradientDrawable#setShape() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RoundCornerProgressBar.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public void setBackgroundColor(int color) {
	backgroundColor = color;
	GradientDrawable gradient = new GradientDrawable();
	gradient.setShape(GradientDrawable.RECTANGLE);
	gradient.setColor(backgroundColor);
	gradient.setCornerRadius(radius);
	if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
		layoutBackground.setBackgroundDrawable(gradient);
   	} else {
		layoutBackground.setBackground(gradient);
   	}
	
	if(!isProgressBarCreated) {
		isBackgroundColorSetBeforeDraw = true;
	}
}
 
Example 2
Source File: SegmentedButtonGroup.java    From SegmentedButton with Apache License 2.0 6 votes vote down vote up
/**
 * Set divider between buttons with a specified solid color, width, radius and padding
 *
 * @param color   color of the divider
 * @param width   width of the divider drawable, in pixels
 * @param radius  corner radius of the divider drawable to round the corners, in pixels
 * @param padding space above and below the divider drawable within the button group, in pixels
 */
public void setDivider(@ColorInt int color, int width, int radius, int padding)
{
    // Create GradientDrawable of the specified color
    // This is used to specify the corner radius, unlike ColorDrawable that does not have that feature
    GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP,
        new int[] {color, color});

    drawable.setCornerRadius(radius);
    drawable.setShape(GradientDrawable.RECTANGLE);
    drawable.setSize(width, 0);

    dividerLayout.setDividerDrawable(drawable);
    dividerLayout.setDividerPadding(padding);
    dividerLayout.setShowDividers(SHOW_DIVIDER_MIDDLE);

    // Loop through and update the divider width for each of the dummy divider views
    for (int i = 0; i < dividerLayout.getChildCount(); ++i)
    {
        final ButtonActor view = (ButtonActor)dividerLayout.getChildAt(i);
        view.setDividerWidth(width);
    }
    dividerLayout.requestLayout();
}
 
Example 3
Source File: PaymentMethodFragment.java    From px-android with MIT License 6 votes vote down vote up
protected void tintBackground(@NonNull final ImageView background, @NonNull final String color) {
    final int backgroundColor = Color.parseColor(color);

    final int alpha = Color.alpha(backgroundColor);
    final int blue = Color.blue(backgroundColor);
    final int green = Color.green(backgroundColor);
    final int red = Color.red(backgroundColor);

    final int lighterBackgroundColor =
        Color.argb((int) (alpha * 0.7f), (int) (red * 0.8f), (int) (green * 0.8f), (int) (blue * 0.8f));
    Color.argb(0, 0, 0, 0);
    final int[] ints = { backgroundColor, lighterBackgroundColor };
    final GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.BL_TR,
        ints);

    gradientDrawable.setCornerRadius(getResources().getDimensionPixelSize(R.dimen.px_xs_margin));
    gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
    gradientDrawable.setShape(GradientDrawable.RECTANGLE);
    gradientDrawable.setDither(true);

    background.setImageDrawable(gradientDrawable);
}
 
Example 4
Source File: IconRoundCornerProgressBar.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public void setHeaderColor(int color) {
	headerColor = color;
	int radius = this.radius - (padding / 2);
	GradientDrawable gradient = new GradientDrawable();
	gradient.setShape(GradientDrawable.RECTANGLE);
	gradient.setColor(headerColor);
	gradient.setCornerRadii(new float [] { radius, radius, 0, 0, 0, 0, radius, radius});
	if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
		layoutHeader.setBackgroundDrawable(gradient);
   	} else {
   		layoutHeader.setBackground(gradient);
   	}
	
	if(!isProgressBarCreated) {
		isHeaderColorSetBeforeDraw = true;
	}
}
 
Example 5
Source File: FloatingToolbar.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
private ViewGroup createContentContainer(Context context) {
    RelativeLayout contentContainer = new RelativeLayout(context);
    ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.bottomMargin = layoutParams.leftMargin = layoutParams.topMargin = layoutParams.rightMargin = AndroidUtilities.dp(20);
    contentContainer.setLayoutParams(layoutParams);
    contentContainer.setElevation(AndroidUtilities.dp(2));
    contentContainer.setFocusable(true);
    contentContainer.setFocusableInTouchMode(true);
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.RECTANGLE);
    int r = AndroidUtilities.dp(6);
    shape.setCornerRadii(new float[] { r, r, r, r, r, r, r, r });
    if (currentStyle == STYLE_DIALOG) {
        shape.setColor(Theme.getColor(Theme.key_dialogBackground));
    } else if (currentStyle == STYLE_BLACK) {
        shape.setColor(0xf9222222);
    } else if (currentStyle == STYLE_THEME) {
        shape.setColor(Theme.getColor(Theme.key_windowBackgroundWhite));
    }
    contentContainer.setBackgroundDrawable(shape);
    contentContainer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    contentContainer.setClipToOutline(true);
    return contentContainer;
}
 
Example 6
Source File: FloorView.java    From mapwize-ui-android with MIT License 6 votes vote down vote up
private void initialize(@NonNull Context context) {
    animationView = new View(context);
    animationView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    GradientDrawable drawable = new GradientDrawable();
    drawable.setShape(GradientDrawable.OVAL);
    drawable.setColor(ContextCompat.getColor(context, io.mapwize.mapwizesdk.R.color.mapwize_main_color));
    animationView.setBackground(drawable);
    animationView.setVisibility(INVISIBLE);
    this.addView(animationView);

    textView = new TextView(context);
    oldTvColors = textView.getTextColors();
    textView.setText(floor.getName());
    textView.setGravity(Gravity.CENTER);
    textView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    this.addView(textView);
}
 
Example 7
Source File: BaseTransientBottomBar.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@NonNull
private Drawable createThemedBackground() {
  float cornerRadius =
      getResources().getDimension(R.dimen.mtrl_snackbar_background_corner_radius);

  GradientDrawable background = new GradientDrawable();
  background.setShape(GradientDrawable.RECTANGLE);
  background.setCornerRadius(cornerRadius);

  int backgroundColor =
      MaterialColors.layer(
          this, R.attr.colorSurface, R.attr.colorOnSurface, getBackgroundOverlayColorAlpha());
  background.setColor(backgroundColor);
  if (backgroundTint != null) {
    Drawable wrappedDrawable = DrawableCompat.wrap(background);
    DrawableCompat.setTintList(wrappedDrawable, backgroundTint);
    return wrappedDrawable;
  } else {
    return DrawableCompat.wrap(background);
  }
}
 
Example 8
Source File: LEDColorListPreference.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
private void setPreviewColor(@NonNull String value) {
  int color;

  switch (value) {
    case "green":   color = getContext().getResources().getColor(R.color.green_500);   break;
    case "red":     color = getContext().getResources().getColor(R.color.red_500);     break;
    case "blue":    color = getContext().getResources().getColor(R.color.blue_500);    break;
    case "yellow":  color = getContext().getResources().getColor(R.color.yellow_500);  break;
    case "cyan":    color = getContext().getResources().getColor(R.color.cyan_500);    break;
    case "magenta": color = getContext().getResources().getColor(R.color.pink_500);    break;
    case "white":   color = getContext().getResources().getColor(R.color.white);       break;
    default:        color = getContext().getResources().getColor(R.color.transparent); break;
  }

  if (colorImageView != null) {
    GradientDrawable drawable = new GradientDrawable();
    drawable.setShape(GradientDrawable.OVAL);
    drawable.setColor(color);

    colorImageView.setImageDrawable(drawable);
  }
}
 
Example 9
Source File: RoundHelper.java    From SegmentedButton with Apache License 2.0 5 votes vote down vote up
private static GradientDrawable getGradientDrawable(int dividerColor, int dividerRadius, int dividerSize) {
    GradientDrawable gradient =
            new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]{dividerColor, dividerColor});
    gradient.setShape(GradientDrawable.RECTANGLE);
    gradient.setCornerRadius(dividerRadius);
    gradient.setSize(dividerSize, 0);
    return gradient;
}
 
Example 10
Source File: ThemeUtil.java    From NMSAlphabetAndroidApp with MIT License 5 votes vote down vote up
public static Drawable getHeaderControlDrawable(Context context){
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.RECTANGLE);
    shape.setColor(getPrimaryDarkColor(context));
    shape.setAlpha(254);
    shape.setCornerRadius(250);
    return shape;
}
 
Example 11
Source File: DefaultIconGenerator.java    From google-maps-clustering with Apache License 2.0 5 votes vote down vote up
@NonNull
private Drawable createClusterBackground() {
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setShape(GradientDrawable.OVAL);
    gradientDrawable.setColor(mIconStyle.getClusterBackgroundColor());
    gradientDrawable.setStroke(mIconStyle.getClusterStrokeWidth(),
            mIconStyle.getClusterStrokeColor());
    return gradientDrawable;
}
 
Example 12
Source File: RectIndicator.java    From ViewPagerHelper with Apache License 2.0 5 votes vote down vote up
/**
 * 添加数据
 * @param viewPager
 */
public void addPagerData(int count, ViewPager viewPager) {

    removeAllViews();
    if (count == 0) {
        return;
    }
    mCount = count;
    mViewPager = viewPager;
    GradientDrawable drawable = new GradientDrawable();
    drawable.setShape(GradientDrawable.RECTANGLE);
    drawable.setSize(mRectWidth, mRectHeight);
    drawable.setCornerRadius(mRoundSize);
    drawable.setColor(mDefaultColor);
    for (int i = 0; i < mCount; i++) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        if (i == mCount - 1) {
            params.setMargins(mMargin, 0, mMargin, 0);
        } else {
            params.setMargins(mMargin, 0, 0, 0);
        }

        ImageView imageView = new ImageView(getContext());

        imageView.setBackground(drawable);
        imageView.setLayoutParams(params);
        addView(imageView);
    }
    if (viewPager != null) {
        viewPager.addOnPageChangeListener(new PagerListener());
    }
}
 
Example 13
Source File: ItemDecorationDivider.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
public ItemDecorationDivider(int dividerWidthPx, @ColorInt int dividerColor, int spanCount) {
    GradientDrawable shapeDrawable = new GradientDrawable();
    shapeDrawable.setColor(dividerColor);
    shapeDrawable.setShape(GradientDrawable.RECTANGLE);
    shapeDrawable.setSize(dividerWidthPx, dividerWidthPx);

    this.mCount = spanCount;
    this.mLineWidth = dividerWidthPx;
    this.mDivider = shapeDrawable;
}
 
Example 14
Source File: GosDeploy.java    From GOpenSource_AppKit_Android_AS with MIT License 5 votes vote down vote up
/**
 * 设置导航栏背景颜色
 *
 * @return
 */
public static Drawable setNavigationBarColor() {
	GradientDrawable drawable = new GradientDrawable();
	drawable.setShape(GradientDrawable.RECTANGLE);

	int navigationBarColor = context.getResources().getColor(R.color.yellow);

	String NavigationBarColor_FromMap = infoMap.get(NavigationBarColor_Key).toString();
	if (!TextUtils.isEmpty(NavigationBarColor_FromMap)) {
		navigationBarColor = Color.parseColor("#" + NavigationBarColor_FromMap);
	}
	drawable.setColor(navigationBarColor);

	return drawable;
}
 
Example 15
Source File: BaseRoundCornerProgressBar.java    From RoundCornerProgressBar with Apache License 2.0 5 votes vote down vote up
protected GradientDrawable createGradientDrawable(int[] colors) {
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setShape(GradientDrawable.RECTANGLE);
    gradientDrawable.setOrientation(!isReverse() ? GradientDrawable.Orientation.LEFT_RIGHT : GradientDrawable.Orientation.RIGHT_LEFT);
    gradientDrawable.setColors(colors);
    return gradientDrawable;
}
 
Example 16
Source File: GosDeploy.java    From gokit-android with MIT License 5 votes vote down vote up
/**
 * 设置Button背景颜色
 * 
 * @return
 */
public static Drawable setButtonBackgroundColor() {
	GradientDrawable drawable = new GradientDrawable();
	drawable.setShape(GradientDrawable.RECTANGLE);
	drawable.setCornerRadius(100);

	String ButtonColor_FromMap = infoMap.get(ButtonColor_Key).toString();
	if (!TextUtils.isEmpty(ButtonColor_FromMap)) {
		drawable.setColor(Color.parseColor("#" + ButtonColor_FromMap));
	} else {
		drawable.setColor(context.getResources().getColor(R.color.yellow));
	}

	return drawable;
}
 
Example 17
Source File: BackgroundHelper.java    From monero-wallet-android-app with MIT License 5 votes vote down vote up
public static Drawable getRedDotDrawable(Context context) {
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setShape(GradientDrawable.OVAL);
    gradientDrawable.setColor(ContextCompat.getColor(context, R.color.color_FF3A5C));
    gradientDrawable.setSize(DisplayHelper.dpToPx(9), DisplayHelper.dpToPx(9));
    return gradientDrawable;
}
 
Example 18
Source File: Selector.java    From a with GNU General Public License v3.0 5 votes vote down vote up
private GradientDrawable getItemShape(int shape, int cornerRadius,
                                      int solidColor, int strokeWidth, int strokeColor) {
    GradientDrawable drawable = new GradientDrawable();
    drawable.setShape(shape);
    drawable.setStroke(strokeWidth, strokeColor);
    drawable.setCornerRadius(cornerRadius);
    drawable.setColor(solidColor);
    return drawable;
}
 
Example 19
Source File: ColorChooserPreferenceX.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
Drawable createSelector(int color) {
    int position = -1;
    for (int i = 0; i < mColors.length; i++) {
        if (mColors[i] == color) {
            position = i;
            break;
        }
    }

    String applicationTheme = ApplicationPreferences.applicationTheme(context, true);

    GradientDrawable coloredCircle = new GradientDrawable();
    coloredCircle.setColor(color);
    coloredCircle.setShape(GradientDrawable.OVAL);
    if (applicationTheme.equals("dark")) {
        if (position == 2) // dark gray color
            coloredCircle.setStroke(2, Color.parseColor("#6E6E6E"));
    }
    else {
        if (position == 0) // white color
            coloredCircle.setStroke(2, Color.parseColor("#AEAEAE"));
    }
    GradientDrawable darkerCircle = new GradientDrawable();
    darkerCircle.setColor(shiftColor(color));
    darkerCircle.setShape(GradientDrawable.OVAL);
    if (applicationTheme.equals("dark")) {
        if (position == 2) // dark gray color
            coloredCircle.setStroke(2, Color.parseColor("#6E6E6E"));
    }
    else {
        if (position == 0) // white color
            darkerCircle.setStroke(2, Color.parseColor("#AEAEAE"));
    }

    StateListDrawable stateListDrawable = new StateListDrawable();
    stateListDrawable.addState(new int[]{-android.R.attr.state_pressed}, coloredCircle);
    stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, darkerCircle);
    return stateListDrawable;
}
 
Example 20
Source File: MaterialImageView.java    From MaterialImageView with MIT License 4 votes vote down vote up
public void init(Context context, AttributeSet attrs, Resources resources, int defStyleAttr){

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MaterialImageView,
                defStyleAttr, 0);

        int shadowSize = a.getInt(R.styleable.MaterialImageView_shadow_size, 8);
        radius = a.getInt(R.styleable.MaterialImageView_radius_size, 15);//radius size
        mUseWhiteBackground = a.getBoolean(R.styleable.MaterialImageView_use_white_bg, false);

        a.recycle();

        //遮罩  用于画出圆角图片
        mMaskDrawable = new GradientDrawable();
        mMaskDrawable.setShape(GradientDrawable.RECTANGLE);
        mMaskDrawable.setColor(0xff000000);
        mMaskDrawable.setCornerRadius(radius);

        mMaskedPaint = new Paint();
        mMaskedPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        mMaskedPaint.setAntiAlias(true);

        mCacheBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);

        mPaint = new Paint(Paint.DITHER_FLAG | Paint.HINTING_ON );
        mPaint.setColor(0xffffffff);

        mShadowStartColor = resources.getColor(R.color.shadow_start_color);
        mShadowEndColor = resources.getColor(R.color.shadow_end_color);

        mInsetShadow = resources.getDimensionPixelSize(R.dimen.cardview_compat_inset_shadow);
        mCornerRadius = ((int) (radius + 0.5F));

        mCornerShadowPaint = new Paint(Paint.DITHER_FLAG | Paint.HINTING_ON);
        mCornerShadowPaint.setStyle(Paint.Style.FILL);
        mCornerShadowPaint.setAntiAlias(true);

        mEdgeShadowPaint = new Paint(mCornerShadowPaint);
        mEdgeShadowPaint.setAntiAlias(true);

        setShadowSize(shadowSize, 20);
    }