ArrowDrawable.java
12 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package com.studymachine.www.other;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.studymachine.www.R;
/**
* author : 王浩 & Android 轮子哥
* github : https://github.com/bingoogolapple/BGATransformersTip-Android
* time : 2019/08/19
* desc : 带箭头背景的 Drawable
*/
@SuppressLint("RtlHardcoded")
public final class ArrowDrawable extends Drawable {
private final Builder mBuilder;
private final Paint mPaint;
private Path mPath;
private ArrowDrawable(Builder builder) {
mBuilder = builder;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setAntiAlias(true);
}
@Override
public void draw(@NonNull Canvas canvas) {
if (mPath == null) {
return;
}
if (mBuilder.mShadowSize > 0) {
mPaint.setMaskFilter(new BlurMaskFilter(mBuilder.mShadowSize, BlurMaskFilter.Blur.OUTER));
mPaint.setColor(mBuilder.mShadowColor);
canvas.drawPath(mPath, mPaint);
}
mPaint.setMaskFilter(null);
mPaint.setColor(mBuilder.mBackgroundColor);
canvas.drawPath(mPath, mPaint);
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
mPaint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@SuppressWarnings("SuspiciousNameCombination")
@Override
protected void onBoundsChange(Rect viewRect) {
if (mPath == null) {
mPath = new Path();
} else {
mPath.reset();
}
RectF excludeShadowRectF = new RectF(viewRect);
excludeShadowRectF.inset(mBuilder.mShadowSize, mBuilder.mShadowSize);
PointF centerPointF = new PointF();
// 判断箭头的位置
switch (mBuilder.mArrowOrientation) {
case Gravity.LEFT:
excludeShadowRectF.left += mBuilder.mArrowHeight;
centerPointF.x = excludeShadowRectF.left;
break;
case Gravity.RIGHT:
excludeShadowRectF.right -= mBuilder.mArrowHeight;
centerPointF.x = excludeShadowRectF.right;
break;
case Gravity.TOP:
excludeShadowRectF.top += mBuilder.mArrowHeight;
centerPointF.y = excludeShadowRectF.top;
break;
case Gravity.BOTTOM:
excludeShadowRectF.bottom -= mBuilder.mArrowHeight;
centerPointF.y = excludeShadowRectF.bottom;
break;
default:
break;
}
// 判断箭头的重心
switch (mBuilder.mArrowGravity) {
case Gravity.LEFT:
centerPointF.x = excludeShadowRectF.left + mBuilder.mArrowHeight;
break;
case Gravity.CENTER_HORIZONTAL:
centerPointF.x = viewRect.width() / 2f;
break;
case Gravity.RIGHT:
centerPointF.x = excludeShadowRectF.right - mBuilder.mArrowHeight;
break;
case Gravity.TOP:
centerPointF.y = excludeShadowRectF.top + mBuilder.mArrowHeight;
break;
case Gravity.CENTER_VERTICAL:
centerPointF.y = viewRect.height() / 2f;
break;
case Gravity.BOTTOM:
centerPointF.y = excludeShadowRectF.bottom - mBuilder.mArrowHeight;
break;
default:
break;
}
// 更新箭头偏移量
centerPointF.x += mBuilder.mArrowOffsetX;
centerPointF.y += mBuilder.mArrowOffsetY;
switch (mBuilder.mArrowGravity) {
case Gravity.LEFT:
case Gravity.RIGHT:
case Gravity.CENTER_HORIZONTAL:
centerPointF.x = Math.max(centerPointF.x, excludeShadowRectF.left + mBuilder.mRadius + mBuilder.mArrowHeight);
centerPointF.x = Math.min(centerPointF.x, excludeShadowRectF.right - mBuilder.mRadius - mBuilder.mArrowHeight);
break;
case Gravity.TOP:
case Gravity.BOTTOM:
case Gravity.CENTER_VERTICAL:
centerPointF.y = Math.max(centerPointF.y, excludeShadowRectF.top + mBuilder.mRadius + mBuilder.mArrowHeight);
centerPointF.y = Math.min(centerPointF.y, excludeShadowRectF.bottom - mBuilder.mRadius - mBuilder.mArrowHeight);
break;
default:
break;
}
switch (mBuilder.mArrowOrientation) {
case Gravity.LEFT:
case Gravity.RIGHT:
centerPointF.x = Math.max(centerPointF.x, excludeShadowRectF.left);
centerPointF.x = Math.min(centerPointF.x, excludeShadowRectF.right);
break;
case Gravity.TOP:
case Gravity.BOTTOM:
centerPointF.y = Math.max(centerPointF.y, excludeShadowRectF.top);
centerPointF.y = Math.min(centerPointF.y, excludeShadowRectF.bottom);
break;
default:
break;
}
// 箭头区域(其实是旋转了 90 度后的正方形区域)
Path arrowPath = new Path();
arrowPath.moveTo(centerPointF.x - mBuilder.mArrowHeight, centerPointF.y);
arrowPath.lineTo(centerPointF.x, centerPointF.y - mBuilder.mArrowHeight);
arrowPath.lineTo(centerPointF.x + mBuilder.mArrowHeight, centerPointF.y);
arrowPath.lineTo(centerPointF.x, centerPointF.y + mBuilder.mArrowHeight);
arrowPath.close();
mPath.addRoundRect(excludeShadowRectF, mBuilder.mRadius, mBuilder.mRadius, Path.Direction.CW);
mPath.addPath(arrowPath);
invalidateSelf();
}
public static final class Builder {
/** 上下文对象 */
private final Context mContext;
/** 箭头高度 */
private int mArrowHeight;
/** 背景圆角大小 */
private int mRadius;
/** 箭头方向 */
private int mArrowOrientation;
/** 箭头重心 */
private int mArrowGravity;
/** 箭头水平方向偏移 */
private int mArrowOffsetX;
/** 箭头垂直方向偏移 */
private int mArrowOffsetY;
/** 阴影大小 */
private int mShadowSize;
/** 背景颜色 */
private int mBackgroundColor;
/** 阴影颜色 */
private int mShadowColor;
public Builder(Context context) {
mContext = context;
mBackgroundColor = 0xFF000000;
mShadowColor = 0x33000000;
mArrowHeight = (int) context.getResources().getDimension(R.dimen.dp_6);
mRadius = (int) context.getResources().getDimension(R.dimen.dp_4);
mShadowSize = 0;
mArrowOffsetX = 0;
mArrowOffsetY = 0;
mArrowOrientation = Gravity.NO_GRAVITY;
mArrowGravity = Gravity.NO_GRAVITY;
}
/**
* 设置背景色
*/
public Builder setBackgroundColor(@ColorInt int color) {
mBackgroundColor = color;
return this;
}
/**
* 设置阴影色
*/
public Builder setShadowColor(@ColorInt int color) {
mShadowColor = color;
return this;
}
/**
* 设置箭头高度
*/
public Builder setArrowHeight(int height) {
mArrowHeight = height;
return this;
}
/**
* 设置浮窗圆角半径
*/
public Builder setRadius(int radius) {
mRadius = radius;
return this;
}
/**
* 设置箭头方向(左上右下)
*/
public Builder setArrowOrientation(int orientation) {
switch (orientation = Gravity.getAbsoluteGravity(orientation, mContext.getResources().getConfiguration().getLayoutDirection())) {
case Gravity.LEFT:
case Gravity.TOP:
case Gravity.RIGHT:
case Gravity.BOTTOM:
mArrowOrientation = orientation;
break;
default:
// 箭头只能在左上右下这四个位置
throw new IllegalArgumentException("are you ok?");
}
return this;
}
/**
* 设置箭头布局重心
*/
public Builder setArrowGravity(int gravity) {
gravity = Gravity.getAbsoluteGravity(gravity, mContext.getResources().getConfiguration().getLayoutDirection());
if (gravity == Gravity.CENTER) {
switch (mArrowOrientation) {
case Gravity.LEFT:
case Gravity.RIGHT:
gravity = Gravity.CENTER_VERTICAL;
break;
case Gravity.TOP:
case Gravity.BOTTOM:
gravity = Gravity.CENTER_HORIZONTAL;
break;
default:
break;
}
}
switch (gravity) {
case Gravity.LEFT:
case Gravity.RIGHT:
if (mArrowOrientation == Gravity.LEFT || mArrowOrientation == Gravity.RIGHT) {
throw new IllegalArgumentException("are you ok?");
}
break;
case Gravity.TOP:
case Gravity.BOTTOM:
if (mArrowOrientation == Gravity.TOP || mArrowOrientation == Gravity.BOTTOM) {
throw new IllegalArgumentException("are you ok?");
}
break;
case Gravity.CENTER_VERTICAL:
case Gravity.CENTER_HORIZONTAL:
break;
default:
// 箭头只能在左上右下这四个位置
throw new IllegalArgumentException("are you ok?");
}
mArrowGravity = gravity;
return this;
}
/**
* 设置箭头在 x 轴的偏移量
*/
public Builder setArrowOffsetX(int offsetX) {
mArrowOffsetX = offsetX;
return this;
}
/**
* 设置箭头在 y 轴的偏移量
*/
public Builder setArrowOffsetY(int offsetY) {
mArrowOffsetY = offsetY;
return this;
}
/**
* 设置阴影宽度
*/
public Builder setShadowSize(int size) {
mShadowSize = size;
return this;
}
/**
* 构建 Drawable
*/
public Drawable build() {
if (mArrowOrientation == Gravity.NO_GRAVITY || mArrowGravity == Gravity.NO_GRAVITY) {
// 必须要先设置箭头的方向及重心
throw new IllegalArgumentException("are you ok?");
}
return new ArrowDrawable(this);
}
/**
* 应用到 View
*/
public void apply(View view) {
view.setBackground(build());
if (mShadowSize > 0 || mArrowHeight > 0) {
if (view.getPaddingTop() == 0 && view.getBottom() == 0 &&
view.getPaddingLeft() == 0 && view.getPaddingRight() == 0) {
view.setPadding(mShadowSize, mShadowSize + mArrowHeight, mShadowSize, mShadowSize);
}
}
}
}
}