shape绘制虚线

shape绘制虚线

  • 新建attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--文件 res/value/attr.xml-->
    <!--虚线-->
    <declare-styleable name="DashLineView">
        <attr name="line_space" format="dimension"/>
        <attr name="blank_space" format="dimension"/>
        <attr name="line_color" format="color"/>
        <attr name="orientation">
            <enum name="horizontal" value="0" />
            <enum name="vertical" value="1" />
        </attr>
    </declare-styleable>
</resources>
  • java代码
//shape绘制虚线
public class DashLine extends View {

    private Context context;
    private Paint paint = new Paint();
    private int orientation = 0;
    private int lineColor;
    private int lineSpace;
    private int blankSpace;

    private Path path = new Path();

    public DashLineView(Context context) {
        this(context, null);
    }

    public DashLineView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DashLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initAttrs(attrs);
        init();
    }

    private void initAttrs(AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DashLineView);
        orientation = typedArray.getInteger(R.styleable.DashLineView_orientation, 0);
        lineColor = typedArray.getInteger(R.styleable.DashLineView_line_color, Color.WHITE);
        lineSpace = typedArray.getDimensionPixelOffset(R.styleable.DashLineView_line_space, dp2px(5));
        blankSpace = typedArray.getDimensionPixelOffset(R.styleable.DashLineView_blank_space, dp2px(5));
    }

    private void init() {
        paint.setColor(lineColor);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        //绘制长度为lineSpace的实线后再绘制长度为blankSpace的空白区域,0位间隔
        paint.setPathEffect(new DashPathEffect(new float[]{lineSpace, blankSpace}, 0));

        post(new Runnable() {
            @Override
            public void run() {
                //设置画笔宽度
                if (orientation == 0) { //水平
                    paint.setStrokeWidth(getMeasuredHeight());
                } else {
                    paint.setStrokeWidth(getMeasuredWidth());
                }
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path.moveTo(0, 0);
        if (orientation == 0) { //水平
            path.lineTo(getWidth(), 0);
        } else { //垂直
            path.lineTo(0, getHeight());
        }
        canvas.drawPath(path, paint);
    }

    /**
     * dp转px
     */
    public int dp2px(float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics());
    }
}

  • 使用
 <包名.DashLine
        android:layout_width="match_parent"
        android:layout_height="1dp"
        app:line_space="5dp"
        app:blank_space="5dp"
        app:line_color="@color/black"
        app:orientation="horizontal"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值