使用代码重新创建 xml 文件中的形状并以编程方式设置宽度

问题描述

我已经有了形状代码,现在我需要以编程方式绘制相同的代码并根据数组中文本的长度设置其宽度。我无法使用路径数据重现相同的内容

这是代码

let
Source = Json.Document(Web.Contents("https://data.cms.gov/data-api/v1/dataset/9138d25a-5d85-4a12-b3c9-070e544486db/data?filter[NPI]=" )),Source1 = try Source{0} otherwise [],#"Converted to Table" = Record.ToTable(Source1),#"Transposed Table" = Table.Transpose(#"Converted to Table"),#"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table",[PromoteallScalars=true])
in
#"Promoted Headers"

Output shape should be this

请帮我解决这个问题..

解决方法

方法 1:通过创建自定义形状:

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.shapes.Shape;

public class RectangleCutCorner extends Shape {
    private int backgroundColor  = Color.BLACK;
    private float strokeWidth = 1.0f;
    private static final float CORNER = 35.0f;

    private final Paint border = new Paint();
    private final Path  path;

    public RectangleCutCorner() {
        path = new Path();
        border.setColor(backgroundColor);
        border.setStyle(Paint.Style.FILL);
        border.setStrokeWidth(strokeWidth);
        border.setAntiAlias(true);
        border.setDither(true);
        border.setStrokeJoin(Paint.Join.ROUND);
        border.setStrokeCap(Paint.Cap.ROUND);
    }

    //for setting stroke width programmatically
    public void setStrokeWidthToLayout(float strokeWidth){
        this.strokeWidth = strokeWidth;
        border.setStrokeWidth(strokeWidth);
    }

    //for setting background/stroke color programmatically
    public void setBackgroundColor(int backgroundColor){
        this.backgroundColor = backgroundColor;
        border.setColor(backgroundColor);
    }

    //for setting background filled or not programmatically
    public void setBackgroundFill(boolean isFilled){
      if(isFilled){
          border.setStyle(Paint.Style.FILL);
      }else{
          border.setStyle(Paint.Style.STROKE);
      }
    }

    @Override
    protected void onResize(float width,float height) {
        super.onResize(width,height);

        float dx = strokeWidth/2.0f;
        float dy = strokeWidth/2.0f;
        float x  = dx;
        float y  = dy;
        float w  = width  - dx;
        float h  = height - dy;
        path.reset();
        path.moveTo(x + CORNER,y);
        path.lineTo(w - CORNER,y);
        path.lineTo(w,y + CORNER);
        path.lineTo(w,h);
        path.lineTo(x + CORNER,h);
        path.lineTo(dx,y);
        path.close();
    }
    
    @Override
    public void draw(Canvas canvas,Paint paint) {
        // TODO Auto-generated method stub
        canvas.drawPath(path,border);
    }
}

如何以编程方式使用它:

    RectangleCutCorner rectangleCutCorner = new RectangleCutCorner();
    // set color
    rectangleCutCorner.setBackgroundColor(R.color.black);
    //set fill background (true/false)
    rectangleCutCorner.setBackgroundFill(false);
    // set stroke width
    rectangleCutCorner.setStrokeWidthToLayout(2.5f);
    // set background to view
    view.setBackground(new ShapeDrawable(rectangleCutCorner));

以上代码的输出为:

enter image description here

方法 2:通过使用 Material Shape Drawable:

将此依赖项添加到 build.gradle(app) 文件中:

implementation 'com.google.android.material:material:1.3.0'

如何使用:

    ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
            .toBuilder()
            .setTopRightCorner(CornerFamily.CUT,20)
            .build();
    MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
    int[][] states = new int[][] {
            new int[] { android.R.attr.state_enabled},};
    int[] colors = new int[] {
            Color.WHITE,};
    ColorStateList myList = new ColorStateList(states,colors);
    // set background to drawable
    shapeDrawable.setFillColor(myList);
    // set stroke and to view
    shapeDrawable.setStroke(1.5f,Color.BLACK);
    // set background to view
    ViewCompat.setBackground(tvUrl,shapeDrawable);

以上代码的输出为:

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...