endian11
2020-09-10 8e2bd486a670a529eb6ef08df35e27a71747afca
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 safeluck.drive.evaluation.customview;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
 
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
 
import androidx.annotation.ColorRes;
import androidx.annotation.Nullable;
 
import safeluck.drive.evaluation.R;
import safeluck.drive.evaluation.util.Utils;
 
/**
 * MyApplication2
 * Created by lzw on 2019/3/18. 11:18:47
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class ArrowView extends View {
 
    private Path path = new Path();
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private float PADDING = Utils.px2dp(40);
    private double a = Math.toRadians(55.0);
    private int color = Color.parseColor("#1E4A89");
    private String text = "直角转弯";
    private Path textPath = new Path();
 
    public ArrowView(Context context) {
        this(context,null);
    }
 
    public ArrowView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
 
    public ArrowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ArrowView,defStyleAttr,0);
        text = a.getString(R.styleable.ArrowView_text);
        color = a.getColor(R.styleable.ArrowView_color,Color.parseColor("#99C3D1"));
        a.recycle();
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path.moveTo(getWidth()/2-PADDING,getHeight()/2-PADDING);
        path.rLineTo(PADDING,0);
        path.rLineTo(PADDING*(float)Math.cos(a),PADDING*(float)Math.sin(a));
        path.rLineTo(-PADDING*(float)Math.cos(a),PADDING*(float)Math.sin(a));
        path.rLineTo(-(PADDING),0);
        path.rLineTo(PADDING*(float)Math.cos(a),-PADDING*(float)Math.sin(a));
        path.rLineTo(-PADDING*(float)Math.cos(a),-PADDING*(float)Math.sin(a));
        path.close();
        paint.setColor(color);
        canvas.drawPath(path,paint);
        paint.setColor(Color.parseColor("#1E4A89"));
        char[] index =  getKeyChar(text);
        Paint.FontMetrics fm = paint.getFontMetrics();
        float childHeight = PADDING/text.length();
        for (int i = 0; i < text.length(); i++) {
            canvas.drawText(String.valueOf(index[i]),getWidth()/2-PADDING/3, (float) (((i + 0.5) * childHeight) - (fm.ascent + fm.descent) / 2)+PADDING/3, paint);
        }
    }
 
    protected char[] getKeyChar(String str) {
        char[] keys = new char[str.length()];
        for (int i = 0; i < keys.length; i++) {
            keys[i] = str.charAt(i);
        }
        return keys;
    }
 
    public void setColor( int mColor){
        this.color = mColor;
        requestLayout();
    }
}