代码之家  ›  专栏  ›  技术社区  ›  Alaa AbuZarifa

如何为可绘制线条的形状生成圆角虚线或圆点

  •  0
  • Alaa AbuZarifa  · 技术社区  · 6 年前

    有没有办法可以像附在图片中那样使圆顶/圆点变圆?

    enter image description here

    <shape
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="line">
    
        <stroke 
        android:width="2dp"
        android:color="@color/grey"
        android:dashWidth="3dp"
        android:dashGap="3dp" />
    
    </shape>
    

    注意

    伙计们,我知道怎么画一条虚线,我在问怎么画一个“圆形”虚线。!!请看Adobe XD中的这张图片,了解我的意思。。!

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  2
  •   Alaa AbuZarifa    6 年前

    您可以使用自定义视图和画布上的绘图来实现目标。请试试这个,根据您的需要调整尺码/款式:

    public class RoundedDashView extends View {
    
    public enum Orientation {
        VERTICAL,
        HORIZONTAL
    }
    
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Path path = new Path();
    private Orientation orientation = Orientation.HORIZONTAL;
    
    public RoundedDashView(Context context) {
        super(context);
        init();
    }
    
    public RoundedDashView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public RoundedDashView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    public RoundedDashView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }
    
    private void init() {
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(10);
        paint.setColor(Color.GRAY);
        paint.setPathEffect(new DashPathEffect(new float[]{20, 25}, 20));
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path.reset();
        if (orientation == Orientation.VERTICAL) {
            path.moveTo(getWidth() / 2, 0);
            path.quadTo(getWidth() / 2, getHeight() / 2, getWidth() / 2, getHeight());
        } else {
            path.moveTo(0, getHeight() / 2);
            path.quadTo(getWidth() / 2, getHeight() / 2, getWidth(), getHeight() / 2);
        }
        canvas.drawPath(path, paint);
    }
    
    public void setOrientation(Orientation orientation) {
        this.orientation = orientation;
        invalidate();
    }
    }