下面列出了android.graphics.drawable.ShapeDrawable#setShape() 实例代码,或者点击链接到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 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;
}
@SuppressWarnings("deprecation")
private ImageView createBackgroundImageView(Context context, boolean fullscreen) {
BackgroundImageView view = new BackgroundImageView(context, fullscreen);
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
int cornerRadius;
if (!fullscreen) {
cornerRadius = SizeUtil.dp20;
} else {
cornerRadius = 0;
}
view.setImageBitmap(options.getBackgroundImage());
ShapeDrawable footerBackground = new ShapeDrawable();
footerBackground.setShape(createRoundRect(cornerRadius));
footerBackground.getPaint().setColor(options.getBackgroundColor());
if (Build.VERSION.SDK_INT >= 16) {
view.setBackground(footerBackground);
} else {
view.setBackgroundDrawable(footerBackground);
}
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
view.setLayoutParams(layoutParams);
return view;
}
private Drawable createProductImageDrawable(Product product) {
final ShapeDrawable background = new ShapeDrawable();
background.setShape(new OvalShape());
background.getPaint().setColor(ContextCompat.getColor(getContext(), product.color));
final BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(),
BitmapFactory.decodeResource(getResources(), product.image));
final LayerDrawable layerDrawable = new LayerDrawable
(new Drawable[]{background, bitmapDrawable});
final int padding = (int) getResources().getDimension(R.dimen.spacing_huge);
layerDrawable.setLayerInset(1, padding, padding, padding, padding);
return layerDrawable;
}
private void applyProgressBarSettings() {
if (mHeaderProgressBar != null) {
final int strokeWidth = mHeaderProgressBar.getResources()
.getDimensionPixelSize(R.dimen.ptr_progress_bar_stroke_width);
mHeaderProgressBar.setIndeterminateDrawable(
new SmoothProgressDrawable.Builder(mHeaderProgressBar.getContext())
.color(mProgressDrawableColor)
.width(strokeWidth)
.build());
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(mProgressDrawableColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
mHeaderProgressBar.setProgressDrawable(clipDrawable);
}
}
private void applyProgressBarSettings() {
if (mHeaderProgressBar != null) {
final int strokeWidth = mHeaderProgressBar.getResources()
.getDimensionPixelSize(R.dimen.ptr_progress_bar_stroke_width);
mHeaderProgressBar.setIndeterminateDrawable(
new SmoothProgressDrawable.Builder(mHeaderProgressBar.getContext())
.color(mProgressDrawableColor)
.width(strokeWidth)
.build());
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(mProgressDrawableColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
mHeaderProgressBar.setProgressDrawable(clipDrawable);
}
}
/**
* 根据 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 ProgressBar createProgressBar() {
ProgressBar pb = (ProgressBar) View.inflate(mActivity, R.layout.actionbar_progressbar, null);
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(Color.parseColor("#FF33B5E5"));
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
pb.setProgressDrawable(clipDrawable);
return pb;
}
/**
* 根据指定的背景色,获得当前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 void applyProgressBarSettings() {
if (mHeaderProgressBar != null) {
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(mProgressDrawableColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
mHeaderProgressBar.setProgressDrawable(clipDrawable);
}
}
private void updateHeaderState(){
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(page < maxPage ? nextPageColor : refreshColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
pfbProgressbar.setProgressDrawable(clipDrawable);
pfbTitle.setText(page < maxPage ? R.string.pull_bottom_nextpage : R.string.pull_to_refresh_pull_label);
}
private LayerDrawable getLayerList(boolean isPressed) {
float[] radii = new float[8];
for (int i = 0; i < radii.length; i++) {
radii[i] = corners;
}
ShapeDrawable shapeShadow = new ShapeDrawable(new RectShape());
shapeShadow.getPaint().setColor(shadowColor);
shapeShadow.setShape(new RoundRectShape(radii, null, null));
ShapeDrawable shapeBackground = new ShapeDrawable(new RectShape());
shapeBackground.getPaint().setColor(backgroundColor);
shapeBackground.setShape(new RoundRectShape(radii, null, null));
LayerDrawable composite = new LayerDrawable(new Drawable[]{shapeShadow, shapeBackground});
if (isPressed) {
composite.setLayerInset(0, 0, (int) (normalHeight - pressedHeight), 0, 0);
composite.setLayerInset(1, 0, (int) (normalHeight - pressedHeight), 0, (int) pressedHeight);
} else {
composite.setLayerInset(0, 0, 0, 0, 0);
composite.setLayerInset(1, 0, 0, 0, (int) normalHeight);
}
return composite;
}
private void applyProgressBarSettings() {
if (mHeaderProgressBar != null) {
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(mProgressDrawableColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
mHeaderProgressBar.setProgressDrawable(clipDrawable);
}
}
private void applyProgressBarSettings() {
if (mHeaderProgressBar != null) {
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(mProgressDrawableColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
mHeaderProgressBar.setProgressDrawable(clipDrawable);
}
}
protected void drawYLabels(ShapeDrawable labelDrawable) {
Shape labelsShape = new Shape() {
@Override
public void draw(Canvas canvas, Paint paint) {
labelPaint.setTextAlign(Paint.Align.RIGHT);
labelPaint.setTextSize(lineChartStyle.getLabelTextSize());
labelPaint.setColor(lineChartStyle.getLabelTextColor());
long minY = getMinY();
long maxY = getMaxY();
long yrange = maxY - minY;
float height = getHeight();
float left = getYLabelWidth();
List<Long> yLabels = getYLabels();
for (long y : yLabels) {
String label = formatYLabel(y);
float yCoordinate = getYCoordinate(height, y, minY, yrange);
canvas.drawText(label, left, yCoordinate, labelPaint);
}
}
};
measureYLabel();
labelDrawable.setBounds(0, 0, getWidth(), getHeight());
labelDrawable.setShape(labelsShape);
}
protected void drawXLabels(ShapeDrawable labelDrawable) {
Shape labelsShape = new Shape() {
@Override
public void draw(Canvas canvas, Paint paint) {
labelPaint.setTextAlign(Paint.Align.CENTER);
labelPaint.setTextSize(lineChartStyle.getLabelTextSize());
labelPaint.setColor(lineChartStyle.getLabelTextColor());
long minX = getMinX();
long maxX = getMaxX();
long xrange = maxX - minX;
float width = getWidth();
float height = getHeight();
float labelHeight = height - lineChartStyle.getXLabelMargin();
Rect textBounds = new Rect();
List<Long> xLabels = getXLabels();
for (long x : xLabels) {
String label = formatXLabel(x);
labelPaint.getTextBounds(label, 0, label.length(), textBounds);
float xCoordinate = getXCoordinate(width, x, minX, xrange);
canvas.drawText(label, xCoordinate, labelHeight, labelPaint);
}
}
};
measureXLabel();
labelDrawable.setBounds(0, 0, getWidth(), getHeight());
labelDrawable.setShape(labelsShape);
}
protected void drawLineChart(ShapeDrawable chartDrawable) {
Shape chartShape = new Shape() {
@Override
public void draw(Canvas canvas, Paint paint) {
long minX = getMinX();
long maxX = getMaxX();
long xrange = maxX - minX;
long minY = getMinY();
long maxY = getMaxY();
long yrange = maxY - minY;
float width = getWidth();
float height = getHeight();
float left = getChartLeftMargin();
float top = getChartTopMargin();
float right = width - getChartRightMargin();
float bottom = height - getChartBottomMargin();
drawChartFrame(canvas, left, top, right, bottom);
drawXGrid(canvas, minX, xrange);
drawYGrid(canvas, minY, yrange);
List<LineChartStyle.Border> borders = lineChartStyle.getBorders();
for (LineChartStyle.Border border : borders) {
drawChartBorder(canvas, border, left, top, right, bottom);
}
drawLines(canvas, minX, xrange, minY, yrange);
if (lineChartStyle.isDrawPoint()) {
drawPoints(canvas, minX, xrange, minY, yrange);
}
}
};
chartDrawable.setBounds(0, 0, getWidth(), getHeight());
chartDrawable.setShape(chartShape);
}
public static Drawable generateBackgroundWithShadow(View view, @ColorRes int backgroundColor,
@DimenRes int cornerRadius,
@ColorRes int shadowColor,
@DimenRes int elevation,
int shadowGravity) {
float cornerRadiusValue = view.getContext().getResources().getDimension(cornerRadius);
int elevationValue = (int) view.getContext().getResources().getDimension(elevation);
int shadowColorValue = ContextCompat.getColor(view.getContext(), shadowColor);
int backgroundColorValue = ContextCompat.getColor(view.getContext(), backgroundColor);
float[] outerRadius = {cornerRadiusValue, cornerRadiusValue, cornerRadiusValue,
cornerRadiusValue, cornerRadiusValue, cornerRadiusValue, cornerRadiusValue,
cornerRadiusValue};
Rect shapeDrawablePadding = new Rect();
shapeDrawablePadding.left = 0;
shapeDrawablePadding.right = 0;
int DY = 0;
int DX = 0;
switch (shadowGravity) {
case Gravity.CENTER:
shapeDrawablePadding.top = elevationValue;
shapeDrawablePadding.bottom = elevationValue;
DY = 0;
break;
case Gravity.RIGHT:
shapeDrawablePadding.right = elevationValue * 2;
shapeDrawablePadding.bottom = elevationValue * 2;
DY = elevationValue / 3;
DX = elevationValue / 3;
break;
case Gravity.TOP:
shapeDrawablePadding.top = elevationValue * 2;
shapeDrawablePadding.bottom = elevationValue;
DY = -1 * elevationValue / 3;
break;
case Gravity.BOTTOM:
shapeDrawablePadding.top = elevationValue;
shapeDrawablePadding.bottom = elevationValue * 2;
DY = elevationValue / 3;
break;
}
ShapeDrawable shapeDrawable = new ShapeDrawable();
shapeDrawable.setPadding(shapeDrawablePadding);
shapeDrawable.getPaint().setColor(backgroundColorValue);
shapeDrawable.getPaint().setShadowLayer(cornerRadiusValue / 3, DX, DY, shadowColorValue);
view.setLayerType(LAYER_TYPE_SOFTWARE, shapeDrawable.getPaint());
shapeDrawable.setShape(new RoundRectShape(outerRadius, null, null));
LayerDrawable drawable = new LayerDrawable(new Drawable[]{shapeDrawable});
drawable.setLayerInset(0, 0, 0, 0, elevationValue * 2);
return drawable;
}
public void setCircleBackgroundColor(@ColorRes int circleColorRes) {
ShapeDrawable circleShapeDrawable = new ShapeDrawable();
circleShapeDrawable.setShape(new OvalShape());
circleShapeDrawable.getPaint().setColor(ContextCompat.getColor(context, circleColorRes));
circleView.setBackgroundDrawable(circleShapeDrawable);
}