admin
2024-01-26 c2d382d99ca506932985d1843d4371d6ed0203ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.lcjian.library.widget;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
 
import com.lcjian.lcjianlibrary.R;
 
public class DashLine extends View {
 
    public static final int HORIZONTAL = 0;
    
    public static final int VERTICAL = 1;
    
    private int mOrientation;
    
    private int mDashWidth;
 
    private int mDashGap;
    
    private DashPathEffect mDashPathEffect;
 
    private int mLineColor;
 
    private Paint mPaint;
 
    private Path mPath;
 
    public DashLine(Context context) {
        this(context, null);
    }
 
    public DashLine(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public DashLine(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
 
        mPaint = new Paint();
        mPath = new Path();
        
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DashLine, defStyleAttr, 0);
 
        mOrientation = a.getInt(R.styleable.DashLine_dashOrientation, 0);
        
        mDashGap = a.getDimensionPixelSize(R.styleable.DashLine_dashGap, 5);
 
        mDashWidth = a.getDimensionPixelSize(R.styleable.DashLine_dashWidth, 5);
 
        mLineColor = a.getColor(R.styleable.DashLine_lineColor, Color.GRAY);
        
        mDashPathEffect = new DashPathEffect(new float[] { mDashWidth, mDashGap }, 0);
 
        a.recycle();
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(mLineColor);
        if (mOrientation == HORIZONTAL) {
            mPaint.setStrokeWidth(getHeight() - getPaddingBottom() - getPaddingTop());
        } else {
            mPaint.setStrokeWidth(getWidth() - getPaddingLeft() - getPaddingRight());
        }
        mPaint.setPathEffect(mDashPathEffect);
        
        mPath.reset();
        if (mOrientation == HORIZONTAL) {
            mPath.moveTo(getPaddingLeft(), getPaddingTop());
            mPath.lineTo(getWidth() - getPaddingRight(), getPaddingTop());
        } else {
            mPath.moveTo(getPaddingLeft(), getPaddingTop());
            mPath.lineTo(getPaddingLeft(), getHeight() - getPaddingBottom());
        }
        canvas.drawPath(mPath, mPaint);
    }
}