PhotoBgView1.java
4.46 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.sw.laryngoscope.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
/**
* 电量显示控件
*/
public class PhotoBgView1 extends androidx.appcompat.widget.AppCompatImageView {
private Paint mPaint;
private float angle;// 旋转角度
private float mWidth;
private float mHeight;
public static final int LEFT = 270;
public static final int RIGHT = 90;public static final int UP = 0;
public static final int DOWN = 180;
private Paint mBgPaint;
public PhotoBgView1(Context context) {
super(context);
init(context, null, 0);
}
public PhotoBgView1(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public PhotoBgView1(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
initPaint();
}
private void initPaint() {
mBgPaint = new Paint();
mBgPaint.setAntiAlias(true);
mBgPaint.setColor(Color.parseColor("#333333"));
mBgPaint.setStyle(Paint.Style.FILL);
mPaint = new Paint();
mPaint.setAntiAlias(true);
//mPaint.setColor(Color.BLACK);
mPaint.setColor(Color.parseColor("#1B2027"));
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(1);
}
public PhotoBgView1 setWidth(float width){
this.mWidth = width;
return this;
}
public PhotoBgView1 setHeight(float height){
this.mHeight = height;
return this;
}
/**
*
* @Title: setAngle
* @Description: 按角度调整箭头方向 0-360
* @param @param angle
* @return TriangleView this
* @throws
*/
public PhotoBgView1 setAngle(float angle) {
this.angle = angle;
invalidate();
return this;
}
/**
*
* @Title: setDirection
* @Description: 调整箭头方向 LEFT,RIGHT,UP,DOWN
* @param @param direction
* @return TriangleView this
* @throws
*/
public PhotoBgView1 setDirection(int direction){
this.angle = direction;
invalidate();
return this;
}
/**
*
* @Title: setColor
* @Description: 给三角形填充颜色
* @param @param color
* @return TriangleView this
* @throws
*/
public PhotoBgView1 setColor(int color){
mPaint.setColor(color);
invalidate();
return this;
}
public void setSelect(boolean isSel) {
if ( isSel ) {
mBgPaint.setColor(Color.parseColor("#C1C1C1"));
} else {
mBgPaint.setColor(Color.parseColor("#333333"));
}
invalidate();
}
private void drawTriangle(Canvas canvas) {
mWidth = getWidth();
mHeight = getHeight();
canvas.drawRect(0, 0, mWidth, mHeight, mBgPaint);
int sideLen = (int) (mWidth / 12f * 1.0f);
Path path0 = new Path();
path0.moveTo(0, 0);
path0.lineTo(sideLen, 0);
path0.lineTo(0, sideLen);
Path path1 = new Path();
path1.moveTo(mWidth - sideLen, 0);
path1.lineTo(mWidth, 0);
path1.lineTo(mWidth, sideLen);
Path path2 = new Path();
path2.moveTo(0, mHeight - sideLen);
path2.lineTo(0, mHeight);
path2.lineTo(sideLen, mHeight);
Path path3 = new Path();
path3.moveTo(mWidth - sideLen, mHeight);
path3.lineTo(mWidth, mHeight);
path3.lineTo(mWidth, mHeight - sideLen);
canvas.save();
canvas.drawPath(path0, mPaint);
canvas.drawPath(path1, mPaint);
canvas.drawPath(path2, mPaint);
canvas.drawPath(path3, mPaint);
canvas.restore();
}
/**
*
* @Title: calTriangleSileLength
* @Description: 计算三角形边长
* @param @param diameter
* @param @return
* @return float
* @throws
*/
private float calTriangleSileLength(float diameter) {
return (float) (2 * Math.sqrt(Math.pow(diameter / 2, 2)
- Math.pow(diameter / 4, 2)));
}
@Override
protected void onDraw(Canvas canvas) {super.onDraw(canvas);
drawTriangle(canvas);
}
}