下面列出了怎么用android.graphics.drawable.shapes.RoundRectShape的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
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 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;
}
public static void setShadow(View view, Drawable sd) {
RoundRectShape rss = new RoundRectShape(new float[]{12f, 12f, 12f,
12f, 12f, 12f, 12f, 12f}, null, null);
ShapeDrawable sds = new ShapeDrawable(rss);
sds.setShaderFactory(new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient lg = new LinearGradient(0, 0, 0, height,
new int[]{Color.parseColor("#e5e5e5"),
Color.parseColor("#e5e5e5"),
Color.parseColor("#e5e5e5"),
Color.parseColor("#e5e5e5")}, new float[]{0,
0.50f, 0.50f, 1}, Shader.TileMode.REPEAT);
return lg;
}
});
LayerDrawable ld = new LayerDrawable(new Drawable[]{sds, sd});
ld.setLayerInset(0, 5, 5, 0, 0); // inset the shadow so it doesn't start right at the left/top
ld.setLayerInset(1, 0, 0, 5, 5); // inset the top drawable so we can leave a bit of space for the shadow to use
view.setBackgroundDrawable(ld);
}
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 ShapeDrawable a()
{
int i1 = a(8);
float af[] = new float[8];
af[0] = i1;
af[1] = i1;
af[2] = i1;
af[3] = i1;
af[4] = i1;
af[5] = i1;
af[6] = i1;
af[7] = i1;
ShapeDrawable shapedrawable = new ShapeDrawable(new RoundRectShape(af, null, null));
shapedrawable.getPaint().setColor(n);
return shapedrawable;
}
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 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;
}
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);
}
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;
}
public void setBackground(float dipRadius, int badgeColor) {
int radius = dip2Px(dipRadius);
float[] radiusArray = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
RoundRectShape roundRect = new RoundRectShape(radiusArray, null, null);
ShapeDrawable bgDrawable = new ShapeDrawable(roundRect);
bgDrawable.getPaint().setColor(badgeColor);
setBackground(bgDrawable);
}
private LayerDrawable createDrawable(int radius, int topColor, int bottomColor) {
float[] outerRadius = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
//Top
RoundRectShape topRoundRect = new RoundRectShape(outerRadius, null, null);
ShapeDrawable topShapeDrawable = new ShapeDrawable(topRoundRect);
topShapeDrawable.getPaint().setColor(topColor);
//Bottom
RoundRectShape roundRectShape = new RoundRectShape(outerRadius, null, null);
ShapeDrawable bottomShapeDrawable = new ShapeDrawable(roundRectShape);
bottomShapeDrawable.getPaint().setColor(bottomColor);
//Create array
Drawable[] drawArray = {bottomShapeDrawable, topShapeDrawable};
LayerDrawable layerDrawable = new LayerDrawable(drawArray);
//Set shadow height
if (isShadowEnabled && topColor != Color.TRANSPARENT) {
//unpressed drawable
layerDrawable.setLayerInset(0, 0, 0, 0, 0); /*index, left, top, right, bottom*/
} else {
//pressed drawable
layerDrawable.setLayerInset(0, 0, mShadowHeight, 0, 0); /*index, left, top, right, bottom*/
}
layerDrawable.setLayerInset(1, 0, 0, 0, mShadowHeight); /*index, left, top, right, bottom*/
return layerDrawable;
}
/**
* 设置背景颜色
* @param dipRadius 半径
* @param badgeColor 颜色
*/
private void setBackground(int dipRadius, int badgeColor) {
int radius = dip2Px(dipRadius);
float[] radiusArray = new float[]{radius, radius, radius, radius,
radius, radius, radius, radius};
RoundRectShape roundRect = new RoundRectShape(radiusArray, null, null);
ShapeDrawable bgDrawable = new ShapeDrawable(roundRect);
bgDrawable.getPaint().setColor(badgeColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackground(bgDrawable);
}
}
public static ShapeDrawable generateCornerShapeDrawable(int color, int topLeftCorner, int topRightCorner, int bottomRightCorner, int bottomLeftCorner) {
Shape shape = new RoundRectShape(new float[]{topLeftCorner, topLeftCorner, topRightCorner, topRightCorner, bottomRightCorner, bottomRightCorner, bottomLeftCorner, bottomLeftCorner}, null, null);
ShapeDrawable sd = new ShapeDrawable(shape);
sd.getPaint().setColor(color);
sd.getPaint().setStyle(Paint.Style.FILL);
return sd;
}
public static ShapeDrawable generateCornerStrokeDrawable(int color, float width, int topLeftCorner, int topRightCorner, int bottomRightCorner, int bottomLeftCorner) {
Shape shape = new RoundRectShape(new float[]{topLeftCorner, topLeftCorner, topRightCorner, topRightCorner, bottomRightCorner, bottomRightCorner, bottomLeftCorner, bottomLeftCorner}, null, null);
ShapeDrawable sd = new ShapeDrawable(shape);
sd.getPaint().setColor(color);
sd.getPaint().setStyle(Paint.Style.STROKE);
sd.getPaint().setAntiAlias(true);
sd.getPaint().setStrokeWidth(width);
return sd;
}
@NonNull
private static Drawable getRippleMask(int color) {
float[] outerRadii = new float[8];
Arrays.fill(outerRadii, 3);
RoundRectShape r = new RoundRectShape(outerRadii, null, null);
ShapeDrawable shapeDrawable = new ShapeDrawable(r);
shapeDrawable.getPaint().setColor(color);
return shapeDrawable;
}
private static Drawable buildRoundedRect(int radius) {
final float[] radii = new float[8];
Arrays.fill(radii, radius);
final RoundRectShape roundedRectShape = new RoundRectShape(radii, null, radii);
final ShapeDrawable drawable = new ShapeDrawable(roundedRectShape);
drawable.getPaint().setColor(Color.RED);
return drawable;
}
static ShapeDrawable getMessageBackground(ComponentContext c, int color) {
final RoundRectShape roundedRectShape =
new RoundRectShape(
new float[] {40, 40, 40, 40, 40, 40, 40, 40},
null,
new float[] {40, 40, 40, 40, 40, 40, 40, 40});
final ShapeDrawable oval = new ShapeDrawable(roundedRectShape);
oval.getPaint().setColor(color);
return oval;
}
private static Drawable buildCircleDrawable(ComponentContext c, int color, int radiusDp) {
final float radiusPx =
TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, radiusDp, c.getResources().getDisplayMetrics());
final float[] radii = new float[8];
Arrays.fill(radii, radiusPx);
final RoundRectShape roundedRectShape = new RoundRectShape(radii, null, radii);
final ShapeDrawable drawable = new ShapeDrawable(roundedRectShape);
drawable.getPaint().setColor(color);
return drawable;
}
private void drawBorder(Canvas canvas) {
RectF rect = new RectF(getBounds());
rect.inset(borderThickness/2, borderThickness/2);
if (shape instanceof OvalShape) {
canvas.drawOval(rect, borderPaint);
}
else if (shape instanceof RoundRectShape) {
canvas.drawRoundRect(rect, radius, radius, borderPaint);
}
else {
canvas.drawRect(rect, borderPaint);
}
}
@Override
public IBuilder roundRect(int radius) {
this.radius = radius;
float[] radii = {radius, radius, radius, radius, radius, radius, radius, radius};
this.shape = new RoundRectShape(radii, null, null);
return this;
}
private LayerDrawable createDrawable(int radius, int topColor, int bottomColor) {
float[] outerRadius = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
//Top
RoundRectShape topRoundRect = new RoundRectShape(outerRadius, null, null);
ShapeDrawable topShapeDrawable = new ShapeDrawable(topRoundRect);
topShapeDrawable.getPaint().setColor(topColor);
//Bottom
RoundRectShape roundRectShape = new RoundRectShape(outerRadius, null, null);
ShapeDrawable bottomShapeDrawable = new ShapeDrawable(roundRectShape);
bottomShapeDrawable.getPaint().setColor(bottomColor);
//Create array
Drawable[] drawArray = {bottomShapeDrawable, topShapeDrawable};
LayerDrawable layerDrawable = new LayerDrawable(drawArray);
//Set shadow height
if (isShadowEnabled && topColor != Color.TRANSPARENT) {
//unpressed drawable
layerDrawable.setLayerInset(0, 0, 0, 0, 0); /*index, left, top, right, bottom*/
} else {
//pressed drawable
layerDrawable.setLayerInset(0, 0, mShadowHeight, 0, 0); /*index, left, top, right, bottom*/
}
layerDrawable.setLayerInset(1, 0, 0, 0, mShadowHeight); /*index, left, top, right, bottom*/
return layerDrawable;
}
@Override
public IBuilder roundRect(int radius) {
this.radius = radius;
float[] radii = {radius, radius, radius, radius, radius, radius, radius, radius};
this.shape = new RoundRectShape(radii, null, null);
return this;
}
@NonNull private static Drawable getRippleMask(int color) {
float[] outerRadii = new float[8];
Arrays.fill(outerRadii, 3);
RoundRectShape r = new RoundRectShape(outerRadii, null, null);
ShapeDrawable shapeDrawable = new ShapeDrawable(r);
shapeDrawable.getPaint().setColor(color);
return shapeDrawable;
}
/**
* 根据 radius 和 color 来创建 ShapeDrawable
* @param radius 弧度
* @param color 颜色
* @return drawable
*/
private Drawable createShape(float radius, int color){
ShapeDrawable shape = new ShapeDrawable();
// 设置弧度
radius = dp2px(radius);
float[] outerRadii = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
RoundRectShape roundShape = new RoundRectShape(outerRadii, null, null);
shape.setShape(roundShape);
// 设置颜色
shape.getPaint().setColor(color);
return shape;
}
/**
* 根据指定的背景色,获得当前SweetToast实例中mContentView的背景drawable实例
* @param backgroundColor
* @return
*/
private static Drawable getBackgroundDrawable(SweetToast sweetToast, @ColorInt int backgroundColor){
try {
ShapeDrawable shapeDrawable = new ShapeDrawable();
//获取当前设备的屏幕尺寸
//实验发现不同的设备上面,Toast内容区域的padding值并不相同,根据屏幕的宽度分别进行处理,尽量接近设备原生Toast的体验
int widthPixels = sweetToast.getContentView().getResources().getDisplayMetrics().widthPixels;
int heightPixels = sweetToast.getContentView().getResources().getDisplayMetrics().heightPixels;
float density = sweetToast.getContentView().getResources().getDisplayMetrics().density;
if(widthPixels >= 1070){
//例如小米5S:1920 x 1080
shapeDrawable.setPadding((int)(density*13),(int)(density*12),(int)(density*13),(int)(density*12));
}else {
//例如红米2:1280x720
shapeDrawable.setPadding((int)(density*14),(int)(density*13),(int)(density*14),(int)(density*13));
}
float radius = density*8;
float[] outerRadii = new float[]{radius,radius,radius,radius,radius,radius,radius,radius};
int width = sweetToast.getContentView().getWidth();
int height = sweetToast.getContentView().getHeight();
RectF rectF = new RectF(1,1,width-1,height-1);
RoundRectShape roundRectShape = new RoundRectShape(outerRadii,rectF,null);
shapeDrawable.setShape(roundRectShape);
//在Android 5.0以下,直接设置 DrawableCompat.setTint 未变色:DrawableCompat.setTint(shapeDrawable,backgroundColor);
//解决:不使用DrawableCompat,直接使用 Drawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP),
//经测试,在4.22(山寨机)和5.1(中兴)和6.0.1(小米5s)上颜色正常显示
shapeDrawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP);
return shapeDrawable;
}catch (Exception e){
Log.e("幻海流心","e:"+e.getLocalizedMessage());
}
return null;
}
private LayerDrawable createDrawable(int radius, int topColor, int bottomColor) {
float[] outerRadius = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
//Top
RoundRectShape topRoundRect = new RoundRectShape(outerRadius, null, null);
ShapeDrawable topShapeDrawable = new ShapeDrawable(topRoundRect);
topShapeDrawable.getPaint().setColor(topColor);
//Bottom
RoundRectShape roundRectShape = new RoundRectShape(outerRadius, null, null);
ShapeDrawable bottomShapeDrawable = new ShapeDrawable(roundRectShape);
bottomShapeDrawable.getPaint().setColor(bottomColor);
//Create array
Drawable[] drawArray = {bottomShapeDrawable, topShapeDrawable};
LayerDrawable layerDrawable = new LayerDrawable(drawArray);
//Set shadow height
if (isShadowEnabled && topColor != Color.TRANSPARENT) {
//unpressed drawable
layerDrawable.setLayerInset(0, 0, 0, 0, 0); /*index, left, top, right, bottom*/
} else {
//pressed drawable
layerDrawable.setLayerInset(0, 0, mShadowHeight, 0, 0); /*index, left, top, right, bottom*/
}
layerDrawable.setLayerInset(1, 0, 0, 0, mShadowHeight); /*index, left, top, right, bottom*/
return layerDrawable;
}
private void change() {
float[] outerR = new float[]{mSize / 2, mSize / 2, mSize / 2, mSize / 2, mSize / 2, mSize / 2, mSize / 2, mSize / 2};
Shape shape = new RoundRectShape(outerR, null, null);
ShapeDrawable shapeDrawable = new ShapeDrawable(shape);
shapeDrawable.setIntrinsicHeight((int) mSize);
shapeDrawable.setIntrinsicWidth((int) mSize);
shapeDrawable.setPadding(0, 0, 0, 0);
shapeDrawable.getPaint().setColor(mColor);
shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
setBackgroundDrawable(shapeDrawable);
setHeight((int) mSize);
setMinWidth((int) mSize);
}
public static ShapeDrawable generateCornerStrokeDrawable(int color, float width, int topLeftCorner, int topRightCorner, int bottomRightCorner, int bottomLeftCorner) {
Shape shape = new RoundRectShape(new float[]{topLeftCorner, topLeftCorner, topRightCorner, topRightCorner, bottomRightCorner, bottomRightCorner, bottomLeftCorner, bottomLeftCorner}, null, null);
ShapeDrawable sd = new ShapeDrawable(shape);
sd.getPaint().setColor(color);
sd.getPaint().setStyle(Paint.Style.STROKE);
sd.getPaint().setAntiAlias(true);
sd.getPaint().setStrokeWidth(width);
return sd;
}