【问题标题】:How to programmatically create a round cornered border using ShapeDrawable in android?如何在android中使用Shape Drawable以编程方式创建圆角边框?
【发布时间】:2013-11-24 05:36:25
【问题描述】:

我需要通过扩展 ShapeDrawable 以编程方式创建带圆角的边框。我需要一个带有圆角的黑色边框,外部像素为白色,内部像素为透明。我目前的代码有多个问题,其中它没有创建一个与边框厚度相同的平滑角,并且边框的外部像素是透明的而不是白色的。

这是我目前得到的角落的图片

这是我在构造函数中为“填充”传递 Color.TRANSPARENT 的代码:

public class CustomShape extends ShapeDrawable {
 private final Paint fillpaint, strokepaint;
public CustomShape(int fill, int strokeWidth,int radius) {

    super(new RoundRectShape(new float[] { radius, radius, radius, radius, radius, radius, radius, radius }, null, null));
    fillpaint = new Paint(this.getPaint());
    fillpaint.setColor(fill);
    strokepaint = new Paint(fillpaint);
    strokepaint.setStyle(Paint.Style.STROKE);
    strokepaint.setStrokeWidth(strokeWidth);
    strokepaint.setColor(Color.BLACK);
}



@Override
protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
    shape.draw(canvas, fillpaint);
    shape.draw(canvas, strokepaint);
}

}

【问题讨论】:

  • 试图扩展 Drawable(并进行自定义 dtaw)而不是 ShapeDrawable?

标签: java android border drawable shapedrawable


【解决方案1】:

如果您需要均匀的圆角(从您的示例看来您确实需要),您可以简单地使用带有纯色的 GragentDrawable

GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);

view.setBackground(gd);

可以在here找到 GradientDrawable 文档。

编辑:分别针对每个角

您可以使用setCornerRadii (float[] radii) 方法分别指定每个角的半径。 “对于每个角,数组包含 2 个值,[X_radius, Y_radius]。这些角按左上角、右上角、右下角、左下角的顺序排列。仅当形状为 RECTANGLE 类型时才使用此属性。

建议在更改此属性之前调用mutate()

【讨论】:

  • 是否可以指定leftCornerRadius和rightCornerRadius?
  • 另外添加了一个注释,每个角如何单独圆角
【解决方案2】:

您可以实现自定义可绘制对象。以下是xml的示例。

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#ffffffff"/>    

    <stroke android:width="3dp"
            android:color="#ff000000"
            />

    <padding android:left="1dp"
             android:top="1dp"
             android:right="1dp"
             android:bottom="1dp"
             /> 

    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
</shape>

将此 xml 保存在项目的可绘制文件夹中。现在将它用作任何小部件的普通可绘制对象。例如:android:background="R.drawable.round_shape"

此示例在link之后引用。

【讨论】:

    【解决方案3】:
    GradientDrawable drawable = (GradientDrawable)image.getBackground();
    drawable.setGradientRadius(radiuspx);
    

    【讨论】:

      【解决方案4】:

      除了指定圆角尺寸,您还可以使用 GradientDrawable 和方法 setCornerRadii()

      GradientDrawable d = new GradientDrawable();
      d.setCornerRadii({5.0f,5.0f,5.0f,5.0f});
      textViewExample.setBackgroundResource(d);
      

      【讨论】:

        【解决方案5】:
         setCornerRadii(new float[] {
                            topLeftRadius, topLeftRadius,
                            topRightRadius, topRightRadius,
                            bottomRightRadius, bottomRightRadius,
                            bottomLeftRadius, bottomLeftRadius
                    });
        

        【讨论】:

          【解决方案6】:

          而不是GradientDrawable,最好使用ShapeDrawableRoundRectShape,这是一个例子:

          // individualRoundedCorners
          val roundCorners = floatArrayOf(
              topLeftRadius, topLeftRadius,
              topRightRadius, topRightRadius,
              bottomRightRadius, bottomRightRadius,
              bottomLeftRadius, bottomLeftRadius
          )
          

          // similarCornerRadius
          val roundCorners = FloatArray(8) { cornerRadius }
          

          然后是drawable本身

          val shapeDrawable = ShapeDrawable().apply {
                  shape = RoundRectShape(roundCorners, null, null)
                  paint.color = Color.RED
              }
          

          更多关于RoundRectShape

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-20
            • 2014-03-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多