下面列出了android.graphics.drawable.GradientDrawable#setStroke() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 给View加上红边~~
*
* @param view the view to be bounded
*/
private static void addViewShape(final View view) {
try {
GradientDrawable gd = new GradientDrawable();
gd.setStroke(4, Color.RED);
final Drawable background = view.getBackground();
view.setBackgroundDrawable(gd);
//Then recover the background.
view.postDelayed(new Runnable() {
@Override
public void run() {
view.setBackgroundDrawable(background);
}
}, 800);
} catch (Throwable ignored) {
}
}
private Drawable getSelector(Tag tag) {
if (tag.background!=null)return tag.background;
StateListDrawable states = new StateListDrawable();
GradientDrawable gd_normal = new GradientDrawable();
gd_normal.setColor(tag.layoutColor);
gd_normal.setCornerRadius(tag.radius);
if (tag.layoutBorderSize>0){
gd_normal.setStroke(Utils.dipToPx(getContext(),tag.layoutBorderSize), tag.layoutBorderColor);
}
GradientDrawable gd_press = new GradientDrawable();
gd_press.setColor(tag.layoutColorPress);
gd_press.setCornerRadius(tag.radius);
states.addState(new int[] { android.R.attr.state_pressed }, gd_press);
//must add state_pressed first,or state_pressed will not take effect
states.addState(new int[] {}, gd_normal);
return states;
}
static Drawable bootstrapThumbnail(Context context,
BootstrapBrand bootstrapBrand,
@ColorInt int outerBorderWidth,
@ColorInt int bg,
boolean rounded) {
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setColor(bg);
drawable.setStroke(outerBorderWidth, bootstrapBrand.defaultEdge(context));
float r = DimenUtils.pixelsFromDpResource(context, R.dimen.bthumbnail_rounded_corner);
if (rounded) {
drawable.setCornerRadii(new float[]{r, r, r, r, r, r, r, r});
}
return drawable;
}
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);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
fragment = new ThreadPublishDialogFragment();
_themeColor = ThemeUtils.getThemeColor(this);
_color_normal = getResources().getColor(R.color.z_txt_c_act_publish_step_n);
_selected_drawable = new GradientDrawable();
_selected_drawable.setStroke(2, _themeColor);
_selected_drawable.setShape(GradientDrawable.OVAL);
super.onCreate(savedInstanceState);
}
/**
* thumb加深色边框并着色
*/
public static GradientDrawable getTintThumb(Context context) {
GradientDrawable thumbDrawable = (GradientDrawable) context.getResources()
.getDrawable(R.drawable.bg_circleseekbar_thumb);
thumbDrawable.setStroke(DensityUtil.dip2px(context, 1),
ColorUtil.shiftColor(ThemeStore.getAccentColor(), 0.8f));
thumbDrawable.setColor(ThemeStore.getAccentColor());
return thumbDrawable;
}
@Override
public View getView(com.kunfei.bookshelf.widget.flowlayout.FlowLayout parent, int position, MyFindKindGroupBean findKindGroupBean) {
TextView tv = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_flow_find_item,
parent, false);
tv.setTag(findKindGroupBean.getGroupTag());
tv.setText(findKindGroupBean.getGroupName());
Random myRandom = new Random();
int ranColor = 0xff000000 | myRandom.nextInt(0x00ffffff);
//tv.setBackgroundColor(ranColor);
GradientDrawable bgShape = (GradientDrawable)tv.getBackground();
bgShape.setStroke(1, ranColor);
/*
int roundRadius = 15; // 8px not dp
int fillColor = Color.parseColor("#DFDFE0");
bgShape.setColor(fillColor);
bgShape.setCornerRadius(roundRadius);
*/
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(null != onItemClickListener){
onItemClickListener.itemClick(v,findKindGroupBean);
}
}
});
return tv;
}
private Drawable createBackgroundColorDrawable() {
GradientDrawable mask = new GradientDrawable();
mask.setShape(GradientDrawable.OVAL);
if (mOutlineWidth != 0) {
int color = mOutlineColor;
if (color == AUTO){
color = isColorDark(mColor) ? Color.WHITE : Color.BLACK;
}
mask.setStroke(mOutlineWidth, color);
}
mask.setColor(mColor);
return mask;
}
public static GradientDrawable getShapeDrawable(@ColorInt int color) {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setStroke(pxToDp(2), color);
gradientDrawable.setColor(color);
gradientDrawable.setCornerRadius(pxToDp(3));
return gradientDrawable;
}
public static GradientDrawable
getDrawable(Context context, int rgb,
float radius, int stroke, int strokeColor) {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(rgb);
gradientDrawable.setCornerRadius(dip2px(context, radius));
gradientDrawable.setStroke(dip2px(context, stroke), strokeColor);
return gradientDrawable;
}
/**
* 设置shape的颜色和描边
*
* @param shape shape对象
* @param color shape对象的背景色
* @param strokeColor shape的描边颜色
* @param strokeWidth shape的描边宽度
*/
private void setShape(GradientDrawable shape, int color, int strokeColor, int strokeWidth, boolean isNormal) {
if (showRipple && !isNormal && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
shape.setColor(normalColor);
} else {
shape.setColor(color);
}
if (strokeColor != DEFAULT_COLOR) {
shape.setStroke(strokeWidth, strokeColor);
}
}
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);
return bg;
}
private void setUpDotBackground(boolean stroke, View dotImageView) {
GradientDrawable dotBackground = (GradientDrawable) dotImageView.getBackground();
if (stroke) {
dotBackground.setStroke(dotsStrokeWidth, dotsStrokeColor);
} else {
dotBackground.setColor(dotIndicatorColor);
}
dotBackground.setCornerRadius(dotsCornerRadius);
}
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);
return bg;
}
/**
* 单个圆角度
* @param strokeWidth
* @param roundRadius
* @param strokeColor
* @return
*/
public static GradientDrawable createGradientDrawable(int strokeWidth, int roundRadius, int strokeColor){
GradientDrawable drawable = new GradientDrawable();
drawable.setCornerRadius(roundRadius);
drawable.setStroke(strokeWidth, strokeColor);
return drawable;
}
public void decorateCardBorder(final int borderColor) {
final GradientDrawable cardShadowRounded =
(GradientDrawable) ContextCompat.getDrawable(context, R.drawable.px_card_shadow_rounded);
cardShadowRounded.setStroke(ScaleUtil.getPxFromDp(6, context), borderColor);
cardBorder.setImageDrawable(cardShadowRounded);
}
private void configView() {
try {
//设置动画
if (mBuilder != null && mBuilder.animationID != 0 && mDialog.getWindow() != null) {
mDialog.getWindow().setWindowAnimations(mBuilder.animationID);
}
} catch (Exception e) {
}
dialog_window_background.setBackgroundColor(mBuilder.backgroundWindowColor);
tvShow.setTextColor(mBuilder.textColor);
GradientDrawable myGrad = (GradientDrawable) dialog_view_bg.getBackground();
myGrad.setColor(mBuilder.backgroundViewColor);
myGrad.setStroke(MSizeUtils.dp2px(mContext, mBuilder.strokeWidth), mBuilder.strokeColor);
myGrad.setCornerRadius(MSizeUtils.dp2px(mContext, mBuilder.cornerRadius));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
dialog_view_bg.setBackground(myGrad);
} else {
dialog_view_bg.setBackgroundDrawable(myGrad);
}
//horizontalProgressBar 配置
//背景
GradientDrawable progressBarBackgroundDrawable = new GradientDrawable();
progressBarBackgroundDrawable.setColor(mBuilder.progressbarBackgroundColor);
progressBarBackgroundDrawable.setCornerRadius(MSizeUtils.dp2px(mContext, mBuilder.progressCornerRadius));
//二级进度条
GradientDrawable secondProgressDrawable = new GradientDrawable();
secondProgressDrawable.setColor(mBuilder.progressbarBackgroundColor);
secondProgressDrawable.setCornerRadius(MSizeUtils.dp2px(mContext, mBuilder.progressCornerRadius));
ClipDrawable hProgressBar02 = new ClipDrawable(secondProgressDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
//一级进度条
GradientDrawable progressDrawable = new GradientDrawable();
progressDrawable.setColor(mBuilder.progressColor);
progressDrawable.setCornerRadius(MSizeUtils.dp2px(mContext, mBuilder.progressCornerRadius));
ClipDrawable hProgressBar03 = new ClipDrawable(progressDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
//组合
Drawable[] layers = {progressBarBackgroundDrawable, hProgressBar02, hProgressBar03};
LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setId(0, android.R.id.background);
layerDrawable.setId(1, android.R.id.secondaryProgress);
layerDrawable.setId(2, android.R.id.progress);
horizontalProgressBar.setProgressDrawable(layerDrawable);
ViewGroup.LayoutParams layoutParams = horizontalProgressBar.getLayoutParams();
layoutParams.height = MSizeUtils.dp2px(mContext, mBuilder.horizontalProgressBarHeight);
horizontalProgressBar.setLayoutParams(layoutParams);
//circularProgressBar 配置
circularProgressBar.setBackgroundColor(mBuilder.progressbarBackgroundColor);
circularProgressBar.setColor(mBuilder.progressColor);
circularProgressBar.setProgressBarWidth(MSizeUtils.dp2px(mContext, mBuilder.circleProgressBarWidth));
circularProgressBar.setBackgroundProgressBarWidth(MSizeUtils.dp2px(mContext, mBuilder.circleProgressBarBackgroundWidth));
}
protected void bindCategory(@NonNull Context context, @NonNull TextView noteCategory, boolean showCategory, @NonNull String category, int mainColor) {
final boolean isDarkThemeActive = NotesApplication.isDarkThemeActive(context);
noteCategory.setVisibility(showCategory && !category.isEmpty() ? View.VISIBLE : View.GONE);
noteCategory.setText(category);
@ColorInt int categoryForeground;
@ColorInt int categoryBackground;
if (isDarkThemeActive) {
if (isColorDark(mainColor)) {
if (contrastRatioIsSufficient(mainColor, Color.BLACK)) {
categoryBackground = mainColor;
categoryForeground = Color.WHITE;
} else {
categoryBackground = Color.WHITE;
categoryForeground = mainColor;
}
} else {
categoryBackground = mainColor;
categoryForeground = Color.BLACK;
}
} else {
categoryForeground = Color.BLACK;
if (isColorDark(mainColor) || contrastRatioIsSufficient(mainColor, Color.WHITE)) {
categoryBackground = mainColor;
} else {
categoryBackground = Color.BLACK;
}
}
noteCategory.setTextColor(categoryForeground);
if (noteCategory instanceof Chip) {
((Chip) noteCategory).setChipStrokeColor(ColorStateList.valueOf(categoryBackground));
((Chip) noteCategory).setChipBackgroundColor(ColorStateList.valueOf(isDarkThemeActive ? categoryBackground : Color.TRANSPARENT));
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
DrawableCompat.setTint(noteCategory.getBackground(), categoryBackground);
} else {
final GradientDrawable drawable = (GradientDrawable) noteCategory.getBackground();
drawable.setStroke(1, categoryBackground);
drawable.setColor(isDarkThemeActive ? categoryBackground : Color.TRANSPARENT);
}
}
}
private void init(AttributeSet attrs) {
if (attributes == null)
attributes = new Attributes(this, getResources());
if (attrs != null) {
// getting android default tags for textColor and textColorHint
hasOwnTextColor = attrs.getAttributeValue(
FlatUI.androidStyleNameSpace, "textColor") != null;
hasOwnHintColor = attrs.getAttributeValue(
FlatUI.androidStyleNameSpace, "textColorHint") != null;
TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.fl_FlatEditText);
// getting common attributes
int customTheme = a.getResourceId(
R.styleable.fl_FlatEditText_fl_theme,
Attributes.DEFAULT_THEME);
attributes.setThemeSilent(customTheme, getResources());
attributes.setFontFamily(a
.getString(R.styleable.fl_FlatEditText_fl_fontFamily));
attributes.setFontWeight(a
.getString(R.styleable.fl_FlatEditText_fl_fontWeight));
attributes.setFontExtension(a
.getString(R.styleable.fl_FlatEditText_fl_fontExtension));
attributes.setTextAppearance(a.getInt(
R.styleable.fl_FlatEditText_fl_textAppearance,
Attributes.DEFAULT_TEXT_APPEARANCE));
attributes.setRadius(a.getDimensionPixelSize(
R.styleable.fl_FlatEditText_fl_cornerRadius,
Attributes.DEFAULT_RADIUS_PX));
attributes.setBorderWidth(a.getDimensionPixelSize(
R.styleable.fl_FlatEditText_fl_borderWidth,
Attributes.DEFAULT_BORDER_WIDTH_PX));
// getting view specific attributes
style = a.getInt(R.styleable.fl_FlatEditText_fl_fieldStyle, 0);
a.recycle();
}
GradientDrawable backgroundDrawable = new GradientDrawable();
backgroundDrawable.setCornerRadius(attributes.getRadius());
if (style == 0) { // flat
if (!hasOwnTextColor)
setTextColor(attributes.getColor(3));
backgroundDrawable.setColor(attributes.getColor(2));
backgroundDrawable.setStroke(0, attributes.getColor(2));
} else if (style == 1) { // box
if (!hasOwnTextColor)
setTextColor(attributes.getColor(2));
backgroundDrawable.setColor(Color.WHITE);
backgroundDrawable.setStroke(attributes.getBorderWidth(),
attributes.getColor(2));
} else if (style == 2) { // transparent
if (!hasOwnTextColor)
setTextColor(attributes.getColor(1));
backgroundDrawable.setColor(Color.TRANSPARENT);
backgroundDrawable.setStroke(attributes.getBorderWidth(),
attributes.getColor(2));
}
setBackgroundDrawable(backgroundDrawable);
if (!hasOwnHintColor)
setHintTextColor(attributes.getColor(3));
if (attributes.getTextAppearance() == 1)
setTextColor(attributes.getColor(0));
else if (attributes.getTextAppearance() == 2)
setTextColor(attributes.getColor(3));
// check for IDE preview render
if (!this.isInEditMode()) {
Typeface typeface = FlatUI.getFont(getContext(), attributes);
if (typeface != null)
setTypeface(typeface);
}
}
@Override
public View getView(Context context, final HintCase hintCase, ViewGroup parent) {
this.hintCase = hintCase;
this.parent = parent;
calculateDataToPutTheArroW(hintCase);
setArrow(context);
FrameLayout.LayoutParams frameLayoutParamsBlock = getParentLayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
gravity, marginLeft, marginTop, marginRight, marginBottom);
RelativeLayout fullBlockLayout = new RelativeLayout(context);
fullBlockLayout.setLayoutParams(frameLayoutParamsBlock);
RelativeLayout.LayoutParams relativeLayoutParamsLinear =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
for (int rule : alignBlockRules) {
relativeLayoutParamsLinear.addRule(rule);
}
relativeLayoutParamsLinear.topMargin = contentTopMargin;
relativeLayoutParamsLinear.bottomMargin = contentBottomMargin;
relativeLayoutParamsLinear.rightMargin = contentRightMargin;
relativeLayoutParamsLinear.leftMargin = contentLeftMargin;
contentLinearLayout = new LinearLayout(context);
contentLinearLayout.setBackgroundResource(R.drawable.bubble_border_background);
LayerDrawable layerDrawable = (LayerDrawable) contentLinearLayout.getBackground().getCurrent();
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.getDrawable(layerDrawable.getNumberOfLayers() - 1);
gradientDrawable.setColor(backgroundColor);
if (useBorder) {
gradientDrawable.setStroke(borderSize, borderColor);
}
contentLinearLayout.setLayoutParams(relativeLayoutParamsLinear);
contentLinearLayout.setGravity(Gravity.CENTER);
int padding = borderSize + shadowSize;
contentLinearLayout.setPadding(padding + contentLeftPadding,
padding + contentTopPadding,
padding + contentRightPadding,
padding + contentBottomPadding);
contentLinearLayout.setOrientation(LinearLayout.VERTICAL);
if (contentTitle != null) {
contentLinearLayout.addView(getTextViewTitle(context));
}
if (existImage()) {
contentLinearLayout.addView(getImage(context, imageView, imageResourceId));
}
if (contentText != null) {
contentLinearLayout.addView(getTextViewDescription(context));
}
fullBlockLayout.addView(contentLinearLayout);
fullBlockLayout.addView(arrow);
return fullBlockLayout;
}