-
Notifications
You must be signed in to change notification settings - Fork 21
/
BubbleLayout.java
228 lines (198 loc) · 6.85 KB
/
BubbleLayout.java
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 me.wy.app;
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.graphics.Point;
import android.graphics.RectF;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.FrameLayout;
/**
* Created by WangYi
*
* @Date : 2018/1/16
* @Desc : 气泡布局
*/
public class BubbleLayout extends FrameLayout {
public static final int LEFT = 1;
public static final int TOP = 2;
public static final int RIGHT = 3;
public static final int BOTTOM = 4;
@IntDef({LEFT, TOP, RIGHT, BOTTOM})
private @interface Direction {
}
/**
* 圆角大小
*/
private int mRadius;
/**
* 三角形的方向
*/
@Direction
private int mDirection;
/**
* 三角形的底边中心点
*/
private Point mDatumPoint;
/**
* 三角形位置偏移量(默认居中)
*/
private int mOffset;
private Paint mBorderPaint;
private Path mPath;
private RectF mRect;
public BubbleLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BubbleLayout);
//背景颜色
int backGroundColor = ta.getColor(R.styleable.BubbleLayout_background_color, Color.WHITE);
//阴影颜色
int shadowColor = ta.getColor(R.styleable.BubbleLayout_shadow_color,
Color.parseColor("#999999"));
int defShadowSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
4, getResources().getDisplayMetrics());
//阴影尺寸
int shadowSize = ta.getDimensionPixelSize(R.styleable.BubbleLayout_shadow_size, defShadowSize);
mRadius = ta.getDimensionPixelSize(R.styleable.BubbleLayout_radius, 0);
//三角形方向
mDirection = ta.getInt(R.styleable.BubbleLayout_direction, BOTTOM);
mOffset = ta.getDimensionPixelOffset(R.styleable.BubbleLayout_offset, 0);
ta.recycle();
mBorderPaint = new Paint();
mBorderPaint.setAntiAlias(true);
mBorderPaint.setColor(backGroundColor);
mBorderPaint.setShadowLayer(shadowSize, 0, 0, shadowColor);
mPath = new Path();
mRect = new RectF();
mDatumPoint = new Point();
setWillNotDraw(false);
//关闭硬件加速
setLayerType(LAYER_TYPE_SOFTWARE, null);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mDatumPoint.x > 0 && mDatumPoint.y > 0)
switch (mDirection) {
case LEFT:
drawLeftTriangle(canvas);
break;
case TOP:
drawTopTriangle(canvas);
break;
case RIGHT:
drawRightTriangle(canvas);
break;
case BOTTOM:
drawBottomTriangle(canvas);
break;
}
}
private void drawLeftTriangle(Canvas canvas) {
int triangularLength = getPaddingLeft();
if (triangularLength == 0) {
return;
}
mPath.addRoundRect(mRect, mRadius, mRadius, Path.Direction.CCW);
mPath.moveTo(mDatumPoint.x, mDatumPoint.y - triangularLength / 2);
mPath.lineTo(mDatumPoint.x - triangularLength / 2, mDatumPoint.y);
mPath.lineTo(mDatumPoint.x, mDatumPoint.y + triangularLength / 2);
mPath.close();
canvas.drawPath(mPath, mBorderPaint);
}
private void drawTopTriangle(Canvas canvas) {
int triangularLength = getPaddingTop();
if (triangularLength == 0) {
return;
}
mPath.addRoundRect(mRect, mRadius, mRadius, Path.Direction.CCW);
mPath.moveTo(mDatumPoint.x + triangularLength / 2, mDatumPoint.y);
mPath.lineTo(mDatumPoint.x, mDatumPoint.y - triangularLength / 2);
mPath.lineTo(mDatumPoint.x - triangularLength / 2, mDatumPoint.y);
mPath.close();
canvas.drawPath(mPath, mBorderPaint);
}
private void drawRightTriangle(Canvas canvas) {
int triangularLength = getPaddingRight();
if (triangularLength == 0) {
return;
}
mPath.addRoundRect(mRect, mRadius, mRadius, Path.Direction.CCW);
mPath.moveTo(mDatumPoint.x, mDatumPoint.y - triangularLength / 2);
mPath.lineTo(mDatumPoint.x + triangularLength / 2, mDatumPoint.y);
mPath.lineTo(mDatumPoint.x, mDatumPoint.y + triangularLength / 2);
mPath.close();
canvas.drawPath(mPath, mBorderPaint);
}
private void drawBottomTriangle(Canvas canvas) {
int triangularLength = getPaddingBottom();
if (triangularLength == 0) {
return;
}
mPath.addRoundRect(mRect, mRadius, mRadius, Path.Direction.CCW);
mPath.moveTo(mDatumPoint.x + triangularLength / 2, mDatumPoint.y);
mPath.lineTo(mDatumPoint.x, mDatumPoint.y + triangularLength / 2);
mPath.lineTo(mDatumPoint.x - triangularLength / 2, mDatumPoint.y);
mPath.close();
canvas.drawPath(mPath, mBorderPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mRect.left = getPaddingLeft();
mRect.top = getPaddingTop();
mRect.right = w - getPaddingRight();
mRect.bottom = h - getPaddingBottom();
switch (mDirection) {
case LEFT:
mDatumPoint.x = getPaddingLeft();
mDatumPoint.y = h / 2;
break;
case TOP:
mDatumPoint.x = w / 2;
mDatumPoint.y = getPaddingTop();
break;
case RIGHT:
mDatumPoint.x = w - getPaddingRight();
mDatumPoint.y = h / 2;
break;
case BOTTOM:
mDatumPoint.x = w / 2;
mDatumPoint.y = h - getPaddingBottom();
break;
}
if (mOffset != 0) {
applyOffset();
}
}
/**
* 设置三角形偏移位置
*
* @param offset 偏移量
*/
public void setTriangleOffset(int offset) {
this.mOffset = offset;
applyOffset();
invalidate();
}
private void applyOffset() {
switch (mDirection) {
case LEFT:
case RIGHT:
mDatumPoint.y += mOffset;
break;
case TOP:
case BOTTOM:
mDatumPoint.x += mOffset;
break;
}
}
}