public static Drawable cornerDrawable(final int bgColor, float[] cornerradius) { final GradientDrawable bg = new GradientDrawable(); bg.setCornerRadii(cornerradius); bg.setColor(bgColor); return bg; }
public void setCornerRadii(float[] cornerRadii) { this.cornerRadii = cornerRadii; getGradientDrawable().setCornerRadii(cornerRadii); }
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; }
/** * This method applies radius to the drawable corners * Specify radius for each corner if radius attribute is not defined * * @param drawable Drawable */ private void applyRadius(GradientDrawable drawable) { if (mRadius > 0) { drawable.setCornerRadius(mRadius); } else { drawable.setCornerRadii(new float[]{mRadiusTopLeft, mRadiusTopLeft, mRadiusTopRight, mRadiusTopRight, mRadiusBottomRight, mRadiusBottomRight, mRadiusBottomLeft, mRadiusBottomLeft}); } }
/** * 只有类型是矩形的时候设置圆角半径才有效 */ private void setRadius() { if (cornersRadius != 0) { gradientDrawable.setCornerRadius(cornersRadius);//设置圆角的半径 } else { //1、2两个参数表示左上角,3、4表示右上角,5、6表示右下角,7、8表示左下角 gradientDrawable.setCornerRadii( new float[] { cornersTopLeftRadius, cornersTopLeftRadius, cornersTopRightRadius, cornersTopRightRadius, cornersBottomRightRadius, cornersBottomRightRadius, cornersBottomLeftRadius, cornersBottomLeftRadius } ); } }
public static void customView(View v, int backgroundColor, int borderColor) { GradientDrawable shape = new GradientDrawable(); shape.setShape(GradientDrawable.RECTANGLE); shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 }); shape.setColor(backgroundColor); shape.setStroke(3, borderColor); v.setBackgroundDrawable(shape); }
/** * 只有类型是矩形的时候设置圆角半径才有效 */ private void setRadius() { if (shapeType == GradientDrawable.RECTANGLE) { if (cornersRadius != 0) { gradientDrawable.setCornerRadius(cornersRadius);//设置圆角的半径 } else { //1、2两个参数表示左上角,3、4表示右上角,5、6表示右下角,7、8表示左下角 gradientDrawable.setCornerRadii( new float[] { cornersTopLeftRadius, cornersTopLeftRadius, cornersTopRightRadius, cornersTopRightRadius, cornersBottomRightRadius, cornersBottomRightRadius, cornersBottomLeftRadius, cornersBottomLeftRadius } ); } } }
defaultGd.setCornerRadii(radii); activeGd.setCornerRadii(radii); disabledGd.setCornerRadii(radii);
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 Drawable getShape(){ GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TL_BR, new int[] { startColor, centerColor, endColor}); gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT); gradientDrawable.setCornerRadii(getRandomFloatArray()); gradientDrawable.setGradientCenter(0.0f, 0.45f); return gradientDrawable; } private float [] getRandomFloatArray(){ Random rnd = new Random(); float[] floats = new float[8]; for (int i =0; i < floats.length; i++){ floats[i] = rnd.nextInt(45); } return floats; }
@SuppressWarnings("deprecation") @SuppressLint("NewApi") public void setProgressColor(int color) { progressColor = color; int radius = this.radius - padding / 2; GradientDrawable gradient = new GradientDrawable(); gradient.setShape(GradientDrawable.RECTANGLE); gradient.setColor(progressColor); gradient.setCornerRadii(new float [] { radius, radius, radius, radius, radius, radius, radius, radius}); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { layoutProgress.setBackground(gradient); } else { layoutProgress.setBackgroundDrawable(gradient); } if(!isProgressBarCreated) { isProgressColorSetBeforeDraw = true; } }
@SuppressWarnings("deprecation") @SuppressLint("NewApi") public void setProgressColor(int color) { progressColor = color; int radius = this.radius - (padding / 2); GradientDrawable gradient = new GradientDrawable(); gradient.setShape(GradientDrawable.RECTANGLE); gradient.setColor(progressColor); gradient.setCornerRadii(new float [] { radius, radius, radius, radius, radius, radius, radius, radius}); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { layoutProgress.setBackground(gradient); } else { layoutProgress.setBackgroundDrawable(gradient); } if(!isProgressBarCreated) { isProgressColorSetBeforeDraw = true; } }
@SuppressWarnings("deprecation") @SuppressLint("NewApi") public void setProgressColor(int color) { progressColor = color; int radius = this.radius - (padding / 2); GradientDrawable gradient = new GradientDrawable(); gradient.setShape(GradientDrawable.RECTANGLE); gradient.setColor(progressColor); gradient.setCornerRadii(new float [] { 0, 0, radius, radius, radius, radius, 0, 0}); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { layoutProgress.setBackgroundDrawable(gradient); } else { layoutProgress.setBackground(gradient); } if(!isProgressBarCreated) { isProgressColorSetBeforeDraw = true; } }
@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; } }
@SuppressWarnings("deprecation") @Override protected void drawProgress(LinearLayout layoutProgress, float max, float progress, float totalWidth, int radius, int padding, int colorProgress, boolean isReverse) { GradientDrawable backgroundDrawable = createGradientDrawable(colorProgress); int newRadius = radius - (padding / 2); if (isReverse && progress != max) backgroundDrawable.setCornerRadii(new float[]{newRadius, newRadius, newRadius, newRadius, newRadius, newRadius, newRadius, newRadius}); else backgroundDrawable.setCornerRadii(new float[]{0, 0, newRadius, newRadius, newRadius, newRadius, 0, 0}); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { layoutProgress.setBackground(backgroundDrawable); } else { layoutProgress.setBackgroundDrawable(backgroundDrawable); } float ratio = max / progress; int progressWidth = (int) ((totalWidth - ((padding * 2) + ivProgressIcon.getWidth())) / ratio); ViewGroup.LayoutParams progressParams = layoutProgress.getLayoutParams(); progressParams.width = progressWidth; layoutProgress.setLayoutParams(progressParams); }
@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 { layoutBackground.setBackgroundDrawable(backgroundDrawable); } }
@Override protected void drawProgress(LinearLayout layoutProgress, float max, float progress, float totalWidth, int radius, int padding, int colorProgress, boolean isReverse) { GradientDrawable backgroundDrawable = createGradientDrawable(colorProgress); 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) { layoutProgress.setBackground(backgroundDrawable); } else { layoutProgress.setBackgroundDrawable(backgroundDrawable); } float ratio = max / progress; int progressWidth = (int) ((totalWidth - (padding * 2)) / ratio); ViewGroup.LayoutParams progressParams = layoutProgress.getLayoutParams(); progressParams.width = progressWidth; layoutProgress.setLayoutParams(progressParams); }
@SuppressWarnings("deprecation") @Override protected void drawProgress(LinearLayout layoutProgress, float max, float progress, float totalWidth, int radius, int padding, int colorProgress, boolean isReverse) { GradientDrawable backgroundDrawable = createGradientDrawable(colorProgress); 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) { layoutProgress.setBackground(backgroundDrawable); } else { layoutProgress.setBackgroundDrawable(backgroundDrawable); } float ratio = max / progress; int progressWidth = (int) ((totalWidth - (padding * 2)) / ratio); ViewGroup.LayoutParams progressParams = layoutProgress.getLayoutParams(); progressParams.width = progressWidth; layoutProgress.setLayoutParams(progressParams); }
@SuppressWarnings("deprecation") private void drawIconBackgroundColor() { GradientDrawable iconBackgroundDrawable = createGradientDrawable(colorIconBackground); int radius = getRadius() - (getPadding() / 2); iconBackgroundDrawable.setCornerRadii(new float[]{radius, radius, 0, 0, 0, 0, radius, radius}); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { ivProgressIcon.setBackground(iconBackgroundDrawable); } else { ivProgressIcon.setBackgroundDrawable(iconBackgroundDrawable); } }
(int) mIndicatorMarginTop, (int) (paddingLeft + mIndicatorRect.right - mIndicatorMarginRight), (int) (mIndicatorMarginTop + mIndicatorHeight)); mIndicatorDrawable.setCornerRadii(mRadiusArr); mIndicatorDrawable.draw(canvas);