下面列出了android.graphics.drawable.GradientDrawable#setCornerRadius() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sensors_analytics_debug_mode_dialog_content);
initView();
Window window = getWindow();
if (window != null) {
WindowManager.LayoutParams p = window.getAttributes();
p.width = dip2px(getContext(), 270);
p.height = dip2px(getContext(), 240);
window.setAttributes(p);
//设置弹框圆角
GradientDrawable bg = new GradientDrawable();
bg.setShape(GradientDrawable.RECTANGLE);
bg.setColor(Color.WHITE);
bg.setCornerRadius(dip2px(getContext(), 7));
window.setBackgroundDrawable(bg);
}
}
private void drawUnselect(Canvas canvas, int count, int verticalOffset, int horizontalOffset) {
for (int i = 0; i < count; i++) {
Rect rect = unselectRects.get(i);
rect.left = horizontalOffset + (indicatorWidth + indicatorGap) * i;
rect.top = verticalOffset;
rect.right = rect.left + indicatorWidth;
rect.bottom = rect.top + indicatorHeight;
GradientDrawable unselectDrawable = unselectDrawables.get(i);
unselectDrawable.setCornerRadius(cornerRadius);
unselectDrawable.setColor(unselectColor);
unselectDrawable.setStroke(strokeWidth, strokeColor);
unselectDrawable.setBounds(rect);
unselectDrawable.draw(canvas);
}
}
private void initInputBg(int accentC, int vicBC) {
GradientDrawable gd = new GradientDrawable();
gd.setColor(vicBC);
gd.setCornerRadius(10);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
gd.setStroke(3, new ColorStateList(
new int[][]{
{android.R.attr.state_focused},
{}},
new int[]{
accentC,
android.support.v4.graphics.ColorUtils.setAlphaComponent(accentC, 200)
}
));
} else {
gd.setStroke(3, accentC);
}
input.setBackground(gd);
}
/**
* Sets the popup colors, when QuickScroll.TYPE_POPUP is selected as type.
* <p/>
*
* @param backgroundcolor
* the background color of the TextView
* @param bordercolor
* the background color of the border surrounding the TextView
* @param textcolor
* the color of the text
*/
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public void setPopupColor(final int backgroundcolor, final int bordercolor, final int borderwidthDPI, final int textcolor, float cornerradiusDPI)
{
final GradientDrawable popupbackground = new GradientDrawable();
popupbackground.setCornerRadius(cornerradiusDPI * getResources().getDisplayMetrics().density);
popupbackground.setStroke((int) (borderwidthDPI * getResources().getDisplayMetrics().density), bordercolor);
popupbackground.setColor(backgroundcolor);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
mScrollIndicatorText.setBackgroundDrawable(popupbackground);
else
mScrollIndicatorText.setBackground(popupbackground);
mScrollIndicatorText.setTextColor(textcolor);
}
/**
* Updates the color of the color picker selected button with the actual color (a gradient from it).
*/
private void updateColorOfColorPickerButton() {
View view = findViewById(R.id.colorPickerButton);
// takes the actually selected color
int color = Library.getInstance().getSelectedColor();
int[] gradientColors = ColoringUtils.colorSelectionButtonBackgroundGradient(color);
if (Build.VERSION.SDK_INT < 16) {
GradientDrawable newGradientDrawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, gradientColors);
newGradientDrawable.setStroke(1, Color.parseColor("#bbbbbb"));
newGradientDrawable.setCornerRadius(ColoringActivity.this.getResources().getDimension(R.dimen.color_selection_button_corner_radius));
//noinspection deprecation
view.setBackgroundDrawable(newGradientDrawable);
} else {
GradientDrawable drawable = (GradientDrawable) view.getBackground();
drawable.mutate();
drawable.setColors(gradientColors);
}
}
/**
* Set the border for the perimeter of the button group.
*
* @param width Width of the border in pixels (default value is 0px or no border)
* @param color Color of the border (default color is black)
* @param dashWidth Width of the dash for border, in pixels. Value of 0px means solid line (default is 0px)
* @param dashGap Width of the gap for border, in pixels.
*/
public void setBorder(int width, @ColorInt int color, int dashWidth, int dashGap)
{
borderWidth = width;
borderColor = color;
borderDashWidth = dashWidth;
borderDashGap = dashGap;
// Border width of 0 indicates to hide borders
if (width > 0)
{
GradientDrawable borderDrawable = new GradientDrawable();
// Set background color to be transparent so that buttons and everything underneath the border view is
// still visible. This was an issue on API 16 Android where it would default to a black background
borderDrawable.setColor(Color.TRANSPARENT);
// Corner radius is the radius minus half of the border width because the drawable will draw the stroke
// from the center, so the actual corner radius is reduced
// If the half border width is left out, the border radius does not follow the curve of the background
borderDrawable.setCornerRadius(radius - width / 2.0f);
borderDrawable.setStroke(width, color, dashWidth, dashGap);
borderView.setBackground(borderDrawable);
}
else
{
borderView.setBackground(null);
}
}
public static Drawable cornerDrawable(final int bgColor, float cornerradius) {
final GradientDrawable bg = new GradientDrawable();
bg.setCornerRadius(cornerradius);
bg.setColor(bgColor);
return bg;
}
/**
* Set drawable as divider between buttons with a specified width, corner radius and padding
*
* If the drawable is null, then the divider will be removed and hidden
*
* @param drawable divider drawable that will be displayed between buttons
* @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(@Nullable Drawable drawable, int width, int radius, int padding)
{
// Drawable of null indicates that we want to hide dividers
if (drawable == null)
{
dividerLayout.setDividerDrawable(null);
dividerLayout.setShowDividers(SHOW_DIVIDER_NONE);
return;
}
// Set the corner radius and size if the drawable is a GradientDrawable
// Otherwise just set the divider drawable like normal because we cant set the parameters
if (drawable instanceof GradientDrawable)
{
GradientDrawable gradient = (GradientDrawable)drawable;
gradient.setSize(width, 0);
gradient.setCornerRadius(radius);
dividerLayout.setDividerDrawable(gradient);
}
else
{
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();
}
public static GradientDrawable createDrawable(int color, int cornerColor, int cornerSize, int cornerRadius) {
GradientDrawable out = new GradientDrawable();
out.setColor(color);
out.setStroke(cornerSize, cornerColor);
out.setCornerRadius(cornerRadius);
return out;
}
private void initBackground() {
GradientDrawable checked = new GradientDrawable();
checked.setColor(mMarkerColor);
checked.setCornerRadius(dpToPx(25));
checked.setStroke(1, mMarkerColor);
mIvBg.setImageDrawable(checked);
GradientDrawable unchecked = new GradientDrawable();
unchecked.setColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
unchecked.setCornerRadius(dpToPx(25));
unchecked.setStroke((int) dpToPx(1), mMarkerColor);
mTvText.setBackgroundDrawable(unchecked);
}
private GradientDrawable getDrawable(int color, float raduis) {
GradientDrawable drawable = new GradientDrawable();
drawable.setCornerRadius(raduis);
drawable.setStroke(strokeWidth, strokeColor);
drawable.setColor(color);
return drawable;
}
private StrokeGradientDrawable createDrawable(int color) {
GradientDrawable drawable = (GradientDrawable) getResources().getDrawable(R.drawable.cpb_background).mutate();
drawable.setColor(color);
drawable.setCornerRadius(mCornerRadius);
StrokeGradientDrawable strokeGradientDrawable = new StrokeGradientDrawable(drawable);
strokeGradientDrawable.setStrokeColor(color);
strokeGradientDrawable.setStrokeWidth(mStrokeWidth);
return strokeGradientDrawable;
}
public GradientDrawable getShape(Context context) {
GradientDrawable bgShape = new GradientDrawable();
bgShape.setShape(GradientDrawable.RECTANGLE);
bgShape.setColor(Color.parseColor(messageTemplate.getBackGroundColor()));
bgShape.setCornerRadius(dpToPixels(context, messageTemplate.getCornerRadius()));
bgShape.setStroke(dpToPixels(context, 2), Color.parseColor(messageTemplate.getBorderColor()));
return bgShape;
}
/**
* 默认指示器是一系列直径为6dp的小圆点
*/
private GradientDrawable generateDefaultDrawable(int color) {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setSize(dp2px(6), dp2px(6));
gradientDrawable.setCornerRadius(dp2px(6));
gradientDrawable.setColor(color);
return gradientDrawable;
}
public CodeSpan(int backgroundColor, int textColor) {
GradientDrawable d = new GradientDrawable();
d.setColor(backgroundColor);
d.setCornerRadius(RADIUS);
mBackground = d;
mTextColor = textColor;
}
public CodeSpan(int color) {
GradientDrawable d = new GradientDrawable();
d.setColor(color);
d.setCornerRadius(radius);
drawable = d;
}
public static Drawable cornerDrawable(final int bgColor, float cornerradius) {
final GradientDrawable bg = new GradientDrawable();
bg.setCornerRadius(cornerradius);
bg.setColor(bgColor);
return bg;
}
/**
* 获取一个新的 View Toast
* @param style Toast 样式 {@link IToast.Style}
* @param view Toast 显示的 View
* @param duration Toast 显示时长 {@link Toast#LENGTH_SHORT}、{@link Toast#LENGTH_LONG}
* @return {@link Toast}
*/
private Toast newToastView(final IToast.Style style, final View view, final int duration) {
if (style == null) {
return null;
} else if (view == null) { // 防止显示的 View 为 null
return null;
}
try {
// 关闭旧的 Toast
if (mToast != null) {
mToast.cancel();
mToast = null;
}
// 获取背景图片
Drawable backgroundDrawable = style.getBackground();
// 如果等于 null
if (backgroundDrawable != null) {
// 设置背景
ViewUtils.setBackground(view, backgroundDrawable);
} else {
if (style.getBackgroundTintColor() != 0) {
GradientDrawable drawable = new GradientDrawable();
// 设置背景色
drawable.setColor(style.getBackgroundTintColor());
// 设置圆角大小
drawable.setCornerRadius(style.getCornerRadius());
// 设置背景
ViewUtils.setBackground(view, drawable);
}
}
// 创建 Toast
mToast = ToastFactory.create(DevUtils.getContext());
mToast.setView(view);
// 设置属性配置
if (style.getGravity() != 0) {
// 设置 Toast 的重心、X、Y 轴偏移
mToast.setGravity(style.getGravity(), style.getXOffset(), style.getYOffset());
}
// 设置边距
mToast.setMargin(style.getHorizontalMargin(), style.getVerticalMargin());
// 设置显示时间
mToast.setDuration(duration);
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "newToastView");
}
return mToast;
}
/**
* Creates and returns a {@link GradientDrawable} shape with custom radius and colors.
* @param context Current context
* @param backgroundColors 2 or 3 int array with the shape background colors
* @param strokeColor Color of the shape stroke
* @param cornerRadius Radius of the shape in dp
* @param strokeWidth Stroke width in dp
* @return A {@link GradientDrawable} with the set arguments
*/
public static GradientDrawable createSquareDrawable(Context context, int[] backgroundColors, int strokeColor, int cornerRadius, float strokeWidth) {
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, backgroundColors);
gradientDrawable.setStroke(ThemeUtils.dpToPix(context, strokeWidth), strokeColor);
gradientDrawable.setCornerRadius(ThemeUtils.dpToPix(context, cornerRadius));
return gradientDrawable;
}
/**
* 获取构造的纯色背景 -> 具有相同圆角的场景
* @param shape 表示背景形状,取值为:
* GradientDrawable.RECTANGLE: 表示矩形
* GradientDrawable.OVAL: 表示圆形
* GradientDrawable.LINE: 表示线条
* @param bgColor 背景颜色
* @param borderColor 边框颜色
* @param borderWidth 边框宽度
* @param radius 圆角大小
* @return
*/
public static Drawable getDrawable(int shape, int bgColor, int borderColor,
int borderWidth, int radius) {
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(shape);
drawable.setColor(bgColor);
drawable.setStroke(borderWidth, borderColor);
drawable.setCornerRadius(radius);
return drawable;
}