public static Bitmap roundBitmap(Bitmap bitmap, int i, int i2, float f, float f2, float f3, float f4) throws Throwable { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Rect rect = new Rect(0, 0, width, height); Bitmap createBitmap = (width == i && height == i2) ? Bitmap.createBitmap(bitmap.getWidth (), bitmap.getHeight(), Config.ARGB_8888) : Bitmap.createBitmap(i, i2, Config .ARGB_8888); Canvas canvas = new Canvas(createBitmap); Paint paint = new Paint(); Rect rect2 = new Rect(0, 0, i, i2); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(-12434878); float[] fArr = new float[]{f, f, f2, f2, f3, f3, f4, f4}; ShapeDrawable shapeDrawable = new ShapeDrawable(new RoundRectShape(fArr, new RectF(0.0f, 0.0f, 0.0f, 0.0f), fArr)); shapeDrawable.setBounds(rect2); shapeDrawable.draw(canvas); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect2, paint); return createBitmap; }
public static Drawable getCornerDrawable(float topLeft, float topRight, float bottomLeft, float bottomRight, @ColorInt int color) { float[] outerR = new float[8]; outerR[0] = topLeft; outerR[1] = topLeft; outerR[2] = topRight; outerR[3] = topRight; outerR[4] = bottomRight; outerR[5] = bottomRight; outerR[6] = bottomLeft; outerR[7] = bottomLeft; ShapeDrawable drawable = new ShapeDrawable(); drawable.setShape(new RoundRectShape(outerR, null, null)); drawable.getPaint().setColor(color); return drawable; }
private Drawable createRectDrawable(int color) { RoundRectShape shape = new RoundRectShape( new float[]{ mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius }, null, null); ShapeDrawable shapeDrawable = new ShapeDrawable(shape); shapeDrawable.getPaint().setColor(color); return shapeDrawable; }
@Override public void onBindViewHolder(MyViewHolder holder, int position) { RestTest restTest = datas.get(position); RoundRectShape shape = new RoundRectShape(new float[]{8, 8, 8, 8, 8, 8, 8, 8}, null, null); ShapeDrawable drawable = new ShapeDrawable(shape); String color = "6bbd5b"; switch (restTest.getMethod().toUpperCase()) { case "GET": break; case "POST": color = "248fb2"; break; case "DELETE": color = "e27a7a"; break; case "PUT": color = "9b708b"; break; } drawable.getPaint().setColor(Color.parseColor("#" + color)); holder.tv.setBackground(drawable); holder.tv.setText(restTest.getMethod().toUpperCase()); holder.spec.setText(restTest.getUrl()); }
private static Drawable getBackground(int normalStateColor, int pressedStateColor) { StateListDrawable background = new StateListDrawable(); int c = SizeUtil.dp10; float[] r = new float[] {c, c, c, c, c, c, c, c}; RoundRectShape rr = new RoundRectShape(r, null, null); ShapeDrawable cd = new ShapeDrawable(); cd.setShape(rr); cd.getPaint().setColor(pressedStateColor); background.addState(new int[] {android.R.attr.state_pressed, android.R.attr.state_focused}, cd); background.addState(new int[] {-android.R.attr.state_pressed, android.R.attr.state_focused}, cd); background.addState(new int[] {android.R.attr.state_pressed, -android.R.attr.state_focused}, cd); ShapeDrawable cd1 = new ShapeDrawable(); cd1.setShape(rr); cd1.getPaint().setColor(normalStateColor); background.addState(new int[] {-android.R.attr.state_pressed, -android.R.attr.state_focused}, cd1); return background; }
private StateListDrawable createDefaultOverButtonBgDrawable() { int dp12 = (int) ThemeUtils.applyDimensionDp(this, 12.f); int dp8 = (int) ThemeUtils.applyDimensionDp(this, 8.f); float dp4 = ThemeUtils.applyDimensionDp(this, 4.f); float[] round = new float[]{dp4, dp4, dp4, dp4, dp4, dp4, dp4, dp4}; ShapeDrawable pressedDrawable = new ShapeDrawable(new RoundRectShape(round, null, null)); pressedDrawable.setPadding(dp12, dp8, dp12, dp8); int pressedColor = ThemeUtils.resolveColor(this, R.attr.gallery_toolbar_over_button_pressed_color, R.color.gallery_default_toolbar_over_button_pressed_color); pressedDrawable.getPaint().setColor(pressedColor); int normalColor = ThemeUtils.resolveColor(this, R.attr.gallery_toolbar_over_button_normal_color, R.color.gallery_default_toolbar_over_button_normal_color); ShapeDrawable normalDrawable = new ShapeDrawable(new RoundRectShape(round, null, null)); normalDrawable.setPadding(dp12, dp8, dp12, dp8); normalDrawable.getPaint().setColor(normalColor); StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable); stateListDrawable.addState(new int[]{}, normalDrawable); return stateListDrawable; }
void setCornerRadius(View view, int radius, int color) { radius = _DP(radius); int borderWidth = 0;// 加边框后会出现空心圆角矩形的效果,所以设置为0 float[] outerRadius = new float[8]; float[] innerRadius = new float[8]; for (int i = 0; i < 8; i++) { outerRadius[i] = radius + borderWidth; innerRadius[i] = radius; } ShapeDrawable shapeDrawable = // 创建图形drawable new ShapeDrawable( // 创建圆角矩形 new RoundRectShape(outerRadius, new RectF(borderWidth, borderWidth, borderWidth, borderWidth), innerRadius)); shapeDrawable.getPaint().setColor(color);// 使用指定的颜色绘制,即背景颜色 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { // 高版本SDK使用新的API view.setBackground(shapeDrawable); } else { view.setBackgroundDrawable(shapeDrawable); } }
private static RoundRectShape getShape(ATableView tableView, ATableViewCellBackgroundStyle backgroundStyle) { ATableViewStyle tableStyle = tableView.getStyle(); float[] radius = new float[]{0, 0, 0, 0, 0, 0, 0, 0}; if (tableStyle == ATableViewStyle.Grouped) { Resources res = tableView.getResources(); float radii = Math.round(res.getDimension(R.dimen.atv_grouped_stroke_radius)); if (backgroundStyle == ATableViewCellBackgroundStyle.Single) { radius = new float[]{radii, radii, radii, radii, radii, radii, radii, radii}; } else if (backgroundStyle == ATableViewCellBackgroundStyle.Top) { radius = new float[]{radii, radii, radii, radii, 0, 0, 0, 0}; } else if (backgroundStyle == ATableViewCellBackgroundStyle.Bottom) { radius = new float[]{0, 0, 0, 0, radii, radii, radii, radii}; } } return new RoundRectShape(radius, null, null); }
private void setShape() { ShapeDrawable drawable = new ShapeDrawable(); // Set color of drawable. drawable.getPaint().setColor((backgroundColor == Component.COLOR_DEFAULT) ? SHAPED_DEFAULT_BACKGROUND_COLOR : backgroundColor); // Set shape of drawable. switch (shape) { case Component.BUTTON_SHAPE_ROUNDED: drawable.setShape(new RoundRectShape(ROUNDED_CORNERS_ARRAY, null, null)); break; case Component.BUTTON_SHAPE_RECT: drawable.setShape(new RectShape()); break; case Component.BUTTON_SHAPE_OVAL: drawable.setShape(new OvalShape()); break; default: throw new IllegalArgumentException(); } // Set drawable to the background of the button. view.setBackgroundDrawable(drawable); view.invalidate(); }
/** * Generate bg drawable drawable. * * @param pressColor the press color * @param defaultColor the default color * @return the drawable */ public static Drawable generateRoundDrawable(float radii, int pressColor, int defaultColor) { //圆角 Shape roundRectShape = new RoundRectShape(new float[]{radii, radii, radii, radii, radii, radii, radii, radii}, null, null);//圆角背景 //按下状态 ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape shopDrawablePress.getPaint().setColor(pressColor);//设置颜色 //正常状态 ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShape); shopDrawableNormal.getPaint().setColor(defaultColor); StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态 bgStateDrawable.addState(new int[]{-android.R.attr.state_enabled}, shopDrawablePress); bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态 return bgStateDrawable; }
public static Drawable generateRoundDrawable(float radii, int pressColor, int disableColor, int defaultColor) { //圆角 Shape roundRectShape = new RoundRectShape(new float[]{radii, radii, radii, radii, radii, radii, radii, radii}, null, null);//圆角背景 //按下状态 ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape shopDrawablePress.getPaint().setColor(pressColor);//设置颜色 //正常状态 ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShape); shopDrawableNormal.getPaint().setColor(defaultColor); //禁用状态 ShapeDrawable disableDrawableNormal = new ShapeDrawable(roundRectShape); disableDrawableNormal.getPaint().setColor(disableColor); StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape bgStateDrawable.addState(new int[]{-android.R.attr.state_enabled}, disableDrawableNormal);// bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态 bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态 return bgStateDrawable; }
/** * Generate bg drawable drawable. * * @param radii the radii * @param pressColor the press color * @param defaultColor the default color * @return the drawable */ public static Drawable generateRoundDrawable(Resources res, float radii, int pressColor, int defaultColor) { radii = dpToPx(res, radii); //外环的圆角矩形 float[] outRadii = new float[]{radii, radii, radii, radii, radii, radii, radii, radii};//四个角的 圆角幅度,8个可以设置的值,每个角都有2个边 2*4=8个 //按下状态 Shape roundRectShape = new RoundRectShape(outRadii, null, null);//圆角背景 ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape shopDrawablePress.getPaint().setColor(pressColor);//设置颜色 //正常状态 Shape roundRectShapeNormal = new RoundRectShape(outRadii, null, null); ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShapeNormal); shopDrawableNormal.getPaint().setColor(defaultColor); StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态 bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态 return bgStateDrawable; }
public static Drawable generateRoundDrawable(float rL1, float rL2, float rT1, float rT2, float rR1, float rR2, float rB1, float rB2, int pressColor, int defaultColor) { //外环的圆角矩形 float[] outRadii = new float[]{rL1, rL2, rT1, rT2, rR1, rR2, rB1, rB2};//四个角的 圆角幅度,8个可以设置的值,每个角都有2个边 2*4=8个 //与内环的距离 RectF inset = new RectF(0, 0, 0, 0); //按下状态 Shape roundRectShape = new RoundRectShape(outRadii, inset, null);//圆角背景 ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape shopDrawablePress.getPaint().setColor(pressColor);//设置颜色 //正常状态 Shape roundRectShapeNormal = new RoundRectShape(outRadii, inset, null); ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShapeNormal); shopDrawableNormal.getPaint().setColor(defaultColor); StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态 bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态 return bgStateDrawable; }
public static Drawable generateRoundDrawable(float radiiL, float radiiR, int pressColor, int defaultColor) { //外环的圆角矩形 float[] outRadii = new float[]{radiiL, radiiL, radiiR, radiiR, radiiR, radiiR, radiiL, radiiL};//四个角的 圆角幅度,8个可以设置的值,每个角都有2个边 2*4=8个 //与内环的距离 RectF inset = new RectF(0, 0, 0, 0); //按下状态 Shape roundRectShape = new RoundRectShape(outRadii, inset, null);//圆角背景 ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape shopDrawablePress.getPaint().setColor(pressColor);//设置颜色 //正常状态 Shape roundRectShapeNormal = new RoundRectShape(outRadii, inset, null); ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShapeNormal); shopDrawableNormal.getPaint().setColor(defaultColor); StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态 bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态 return bgStateDrawable; }
/** * 正常 圆角边框; * 按下 圆角色块 */ public static Drawable generateBorderDrawable(float radii, float borderWidth, int pressColor, int defaultColor) { //外环的圆角矩形 float[] outRadii = new float[]{radii, radii, radii, radii, radii, radii, radii, radii};//四个角的 圆角幅度,8个可以设置的值,每个角都有2个边 2*4=8个 RectF inset = new RectF(borderWidth, borderWidth, borderWidth, borderWidth); //按下状态 Shape roundRectShape = new RoundRectShape(outRadii, null, null);//圆角背景 ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape shopDrawablePress.getPaint().setColor(pressColor);//设置颜色 //正常状态 Shape roundRectShapeNormal = new RoundRectShape(outRadii, inset, outRadii); ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShapeNormal); shopDrawableNormal.getPaint().setColor(defaultColor); StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态 bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态 return bgStateDrawable; }
private static Bitmap toRoundCornerMutableBitmap(Canvas canvas, Paint paintClear, Bitmap srcBitmap, float[] roundRadius) { canvas.setBitmap(srcBitmap); RectF inset = new RectF(1, 1, 1, 1); if (roundRadius.length == 2) { float[] tmpRoundRadius = new float[8]; for (int i = 0; i < 4; ++i) { tmpRoundRadius[i * 2 + 0] = roundRadius[0]; tmpRoundRadius[i * 2 + 1] = roundRadius[1]; roundRadius = tmpRoundRadius; } } canvas.save(); canvas.translate(-1, -1); RoundRectShape roundRectShape = new RoundRectShape(null, inset, roundRadius); roundRectShape.resize(srcBitmap.getWidth() + 2, srcBitmap.getHeight() + 2); roundRectShape.draw(canvas, paintClear); canvas.restore(); canvas.setBitmap(null); return srcBitmap; }
public void setThemeBackground(int color, int colorPressed) { int radius = DensityUtil.dip2px(getContext(), 2); float[] radii = new float[]{radius, radius, radius, radius, radius, radius, radius, radius}; StateListDrawable states = new StateListDrawable(); RoundRectShape disableShape = new RoundRectShape(radii, null, null); ShapeDrawable disableDrawable = new ShapeDrawable(disableShape); disableDrawable.getPaint().setColor(getResources().getColor(R.color.material_grey_300)); states.addState(new int[]{-android.R.attr.state_enabled}, disableDrawable); RoundRectShape pressedShape = new RoundRectShape(radii, null, null); ShapeDrawable pressedDrawable = new ShapeDrawable(pressedShape); pressedDrawable.getPaint().setColor(colorPressed); states.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable); RoundRectShape normalShape = new RoundRectShape(radii, null, null); ShapeDrawable normalDrawable = new ShapeDrawable(normalShape); normalDrawable.getPaint().setColor(color); states.addState(new int[]{-android.R.attr.state_pressed}, normalDrawable); setBackgroundDrawable(states); }
private Drawable createRectDrawable(int color) { RoundRectShape shape = new RoundRectShape( new float[] { mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius }, null, null ); ShapeDrawable shapeDrawable = new ShapeDrawable(shape); shapeDrawable.getPaint().setColor(color); return shapeDrawable; }
@NonNull private ShapeDrawable getShapeDrawable(int topLeftAndRightRadius, int bottomLeftAndRightRadius, int bgColor) { float outRectr[] = new float[]{ topLeftAndRightRadius, topLeftAndRightRadius, topLeftAndRightRadius, topLeftAndRightRadius, bottomLeftAndRightRadius, bottomLeftAndRightRadius, bottomLeftAndRightRadius, bottomLeftAndRightRadius}; /** * 注意StateListDrawable的构造方法我们这里使用的 * 是第一参数它是一个float的数组保存的是圆角的半径,它是按照top-left顺时针保存的八个值 */ //创建圆弧形状 RoundRectShape rectShape = new RoundRectShape(outRectr, null, null); //创建drawable ShapeDrawable pressedDrawable = new ShapeDrawable(rectShape); //设置背景的颜色 pressedDrawable.getPaint().setColor(bgColor); return pressedDrawable; }
private static Drawable generateBackgroundDrawable() { final int r = 0; final float[] outerR = new float[]{r, r, r, r, r, r, r, r}; final int color = Color.parseColor("#228BC34A"); RoundRectShape rr = new RoundRectShape(outerR, null, null); ShapeDrawable drawable = new ShapeDrawable(rr); drawable.setShaderFactory(new ShapeDrawable.ShaderFactory() { @Override public Shader resize(int width, int height) { return new LinearGradient(0, 0, 0, 0, color, color, Shader.TileMode.REPEAT); } }); return drawable; }
private static Drawable createButtonShape(Context context, int color) { // Translation of Lollipop's xml button-bg definition to Java int paddingH = DensityUtil.dip2px(context, 8); int paddingV = DensityUtil.dip2px(context, 4); int insetH = DensityUtil.dip2px(context, 4); int insetV = DensityUtil.dip2px(context, 6); int corner_radius = DensityUtil.dip2px(context, 2); float[] outerRadii = new float[8]; Arrays.fill(outerRadii, corner_radius); RoundRectShape r = new RoundRectShape(outerRadii, null, null); ShapeDrawable shapeDrawable = new ShapeDrawable(r); shapeDrawable.getPaint().setColor(color); shapeDrawable.setPadding(paddingH, paddingV, paddingH, paddingV); return new InsetDrawable(shapeDrawable, insetH, insetV, insetH, insetV); }
public static Drawable createBgDrawable(int color, int rTopLeft, int rTopRight, int rBottomRight, int rBottomLeft) { float[] outerRadii = new float[8]; outerRadii[0] = rTopLeft; outerRadii[1] = rTopLeft; outerRadii[2] = rTopRight; outerRadii[3] = rTopRight; outerRadii[4] = rBottomRight; outerRadii[5] = rBottomRight; outerRadii[6] = rBottomLeft; outerRadii[7] = rBottomLeft; RoundRectShape r = new RoundRectShape(outerRadii, null, null); ShapeDrawable shapeDrawable = new ShapeDrawable(r); shapeDrawable.getPaint().setColor(color); return shapeDrawable; }
private RoundRectShape buildRect(ToggleSwitchButton toggleSwitchButton) { if (isFirst(toggleSwitchButton)) return new RoundRectShape( new float[]{cornerRadius, cornerRadius, 0, 0, 0, 0, cornerRadius, cornerRadius}, null, null); else if (isLast(toggleSwitchButton)) return new RoundRectShape( new float[]{0, 0, cornerRadius, cornerRadius, cornerRadius, cornerRadius, 0, 0}, null, null); else return new RoundRectShape( new float[]{0, 0, 0, 0, 0, 0, 0, 0}, null, null); }
private static Drawable createButtonShape(Context context, int color) { // Translation of Lollipop's xml button-bg definition to Java int paddingH = context.getResources() .getDimensionPixelSize(R.dimen.button_padding_horizontal_material); int paddingV = context.getResources() .getDimensionPixelSize(R.dimen.button_padding_vertical_material); int insetH = context.getResources() .getDimensionPixelSize(R.dimen.button_inset_horizontal_material); int insetV = context.getResources() .getDimensionPixelSize(R.dimen.button_inset_vertical_material); float[] outerRadii = new float[8]; Arrays.fill(outerRadii, CORNER_RADIUS); RoundRectShape r = new RoundRectShape(outerRadii, null, null); ShapeDrawable shapeDrawable = new ShapeDrawable(r); shapeDrawable.getPaint().setColor(color); shapeDrawable.setPadding(paddingH, paddingV, paddingH, paddingV); return new InsetDrawable(shapeDrawable, insetH, insetV, insetH, insetV); }
private ShapeDrawable getDotBackground() { ShapeDrawable drawable = null; switch (mode) { case ROUND_RECT: int radius = dip2Pixels(dotRadius); float[] outerRect = new float[] { radius, radius, radius, radius, radius, radius, radius, radius }; RoundRectShape rr = new RoundRectShape(outerRect, null, null); drawable = new InnerShapeDrawableWithText(rr, dotText); drawable.getPaint().setColor(dotColor); break; case CIRCLE: OvalShape os = new OvalShape(); drawable = new InnerShapeDrawableWithText(os, dotText); drawable.getPaint().setColor(dotColor); // int paddingPixels = dip2Pixels(8); // drawable.setPadding(paddingPixels, paddingPixels, paddingPixels, // paddingPixels); break; } return drawable; }
public CustomToast(Context context) { // 此处必须是ApplicationContext,因为Activity退出也可以显示 context = context.getApplicationContext(); windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); rootView = new LinearLayout(context); // 背景设置为圆角矩形 float r = 10; float[] outerR = new float[] { r, r, r, r, r, r, r, r }; RoundRectShape rr = new RoundRectShape(outerR, null, null); ShapeDrawable drawable = new ShapeDrawable(rr); drawable.getPaint().setColor(Color.BLACK); int a = UIUtil.dip2px(context, 10); drawable.setPadding(a, a, a, a); rootView.setBackgroundDrawable(drawable); }