admin
2022-01-20 d8ef9a783b9e0b2a495f02fdf3daaf27ef49e99d
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
package com.demo.lib.common.widget;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.widget.TextView;
 
public class UnderLineTextView extends TextView {
 
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    
    private PathEffect pathEffect = new DashPathEffect(new float[] { 20, 4 , 20, 4 }, 0);
 
    private Path path = new Path();
    
    public UnderLineTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
 
    public UnderLineTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public UnderLineTextView(Context context) {
        super(context);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        paint.setColor(getCurrentTextColor());
        paint.setStyle(Style.STROKE);
        //设置线粗
        paint.setStrokeWidth(2);
        paint.setPathEffect(pathEffect);
        path.moveTo(0, this.getHeight() - 1);
        path.lineTo(this.getWidth(), this.getHeight() - 1);
        canvas.drawPath(path, paint);
    }
}