PhotoBgView_50.java
7.27 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import com.sw.laryngoscope.R;
import com.sw.laryngoscope.common.Constant;
import com.sw.laryngoscope.manager.SpManager;
/**
* 电量显示控件
*/
public class PhotoBgView_50 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;
public PhotoBgView_50(Context context) {
super(context);
init(context, null, 0);
}
public PhotoBgView_50(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public PhotoBgView_50(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() {
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 PhotoBgView_50 setWidth(float width){
this.mWidth = width;
return this;
}
public PhotoBgView_50 setHeight(float height){
this.mHeight = height;
return this;
}
/**
*
* @Title: setAngle
* @Description: 按角度调整箭头方向 0-360
* @param @param angle
* @return TriangleView this
* @throws
*/
public PhotoBgView_50 setAngle(float angle) {
this.angle = angle;
invalidate();
return this;
}
/**
*
* @Title: setDirection
* @Description: 调整箭头方向 LEFT,RIGHT,UP,DOWN
* @param @param direction
* @return TriangleView this
* @throws
*/
public PhotoBgView_50 setDirection(int direction){
this.angle = direction;
invalidate();
return this;
}
/**
*
* @Title: setColor
* @Description: 给三角形填充颜色
* @param @param color
* @return TriangleView this
* @throws
*/
public PhotoBgView_50 setColor(int color){
mPaint.setColor(color);
invalidate();
return this;
}
private void drawTriangle(Canvas canvas) {
mWidth = getWidth();
mHeight = getHeight();
if ( SpManager.get_border_type() == Constant.BORDER_TYPE_A ) {
Path p = new Path();
//p.addCircle(mWidth / 2f, mHeight / 2f, mWidth / 2f, Path.Direction.CW);
p.addRoundRect(new RectF(0, 0, mWidth, mHeight), 50, 50, Path.Direction.CW);
canvas.clipPath(p, Region.Op.DIFFERENCE);
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
//canvas.restore();
//canvas.drawColor(getResources().getColor(R.color.transparent));
canvas.drawColor(Color.parseColor("#1B2027"));
}
if ( SpManager.get_border_type() == Constant.BORDER_TYPE_B ) {
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();
}
if ( SpManager.get_border_type() == Constant.BORDER_TYPE_C ) {
Path p = new Path();
//p.addCircle(mWidth / 2f, mHeight / 2f, mWidth / 2f, Path.Direction.CW);
p.addCircle(mWidth / 2f, mHeight / 2f, mHeight / 2f, Path.Direction.CW);
canvas.clipPath(p, Region.Op.DIFFERENCE);
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
//canvas.restore();
//canvas.drawColor(getResources().getColor(R.color.transparent));
canvas.drawColor(Color.parseColor("#1B2027"));
}
if ( SpManager.get_border_type() == Constant.BORDER_TYPE_D ) {
}
//Rect rect = new Rect(0, 0, (int) mWidth, (int) mHeight);
/*Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.TRANSPARENT);//.setColor(Color.parseColor("#1B2027"));
canvas.drawRect(rect, paint);
canvas.save();*/
//红色的方形
/*Rect rect = new Rect(0, 0, (int) mWidth, (int) mHeight);
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.parseColor("#1B2027"));
//canvas.drawRect(rect, paint);
//这里开始保存渲染层,从 canvas.saveLayer()方法 到 canvas.restoreToCount()方法结束,期间不会影响其他绘制
int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
//PorterDuff.Mode.CLEAR 清除 蓝色的方形 所覆盖的面积
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRoundRect(new RectF(0, 0, mWidth, mHeight), 100, 100, paint);
//移除 Xfermode
paint.setXfermode(null);
//恢复画布状态
canvas.restoreToCount(layerId);*/
/*Paint _debugInfoPaint = new Paint(Paint.ANTI_ALIAS_FLAG); //设置无锯齿 也可以使用setAntiAlias(true)
_debugInfoPaint.setColor(Color.GREEN);//设置画笔颜色
_debugInfoPaint.setAlpha(200);
_debugInfoPaint.setStrokeWidth(1.5f);//设置线宽
_debugInfoPaint.setStyle(Paint.Style.STROKE);//设置样式:FILL表示颜色填充整个;STROKE表示空心
canvas.drawRoundRect(new RectF(0, 0, mWidth, mHeight), 20, 20, _debugInfoPaint);*/
}
/**
*
* @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);
}
}