java - 如何为可绘制的线条形状制作圆角破折号或圆点

标签 java android android-layout android-drawable xml-drawable

有没有一种方法可以使帽/点变圆,如附图所示?

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>

注意

伙计们,我知道如何制作虚线,我想问的是如何制作“圆角”破折号。!!看看这张来自 Adob​​e XD 的图片就知道我的意思了..!

enter image description here

最佳答案

您可以使用自定义 View 并在 Canvas 上绘图来实现目标。请试试这个并根据您的需要调整尺寸/样式:

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();
}
}

关于java - 如何为可绘制的线条形状制作圆角破折号或圆点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58178998/

相关文章:

android - 在 Android 中自定义工具栏的问题

java - 当从我的 Android 应用程序将数据保存到 mysql 时,它会删除空格

java - 根据第一个单词的长度反转字符串

java - ByteBuffer - CompareTo 方法可能会出现分歧

android - 错误:软件包org.xwalk.core不存在Android Studio和Gradle

Android:如何更改每个元素的 Drawable 资源的颜色?

android - 在BottomSheetDialogFragment 中以编程方式设置查看高度

java - 在hadoop中将文件作为单个记录读取

java - 如何在android中读取xml文件?

android - 如何修复 Parse 中的“无效用户名/密码”