/
DragBallView.java
417 lines (345 loc) · 12 KB
/
DragBallView.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package com.allen.androidcustomview.widget;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.BounceInterpolator;
/**
* Created by xiaoyao on 2017/9/7.
* 仿QQ消息红点拖拽效果view
*/
public class DragBallView extends View {
private Paint circlePaint;
private Paint textPaint;
private int circleColor = Color.RED;
private float radiusStart;
private float radiusEnd;
private Path path;
private int startX;
private int startY;
//是否可拖拽
private boolean mIsCanDrag = false;
//是否超过最大距离
private boolean isOutOfRang = false;
//最终圆是否消失
private boolean disappear = false;
//两圆相离最大距离
private float maxDistance;
//贝塞尔曲线需要的点
private PointF pointA;
private PointF pointB;
private PointF pointC;
private PointF pointD;
//控制点坐标
private PointF pointO;
//起始位置点
private PointF pointStart;
//拖拽位置点
private PointF pointEnd;
//根据滑动位置动态改变圆的半径
private float currentRadiusStart;
private float currentRadiusEnd;
private Rect textRect = new Rect();
//消息数
private int msgCount = 0;
private OnDragBallListener onDragBallListener;
public DragBallView(Context context) {
this(context, null);
}
public DragBallView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public DragBallView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
initPoint();
}
/**
* 初始化所有点
*/
private void initPoint() {
pointStart = new PointF(startX, startY);
pointEnd = new PointF(startX, startY);
pointA = new PointF();
pointB = new PointF();
pointC = new PointF();
pointD = new PointF();
pointO = new PointF();
}
/**
* 初始化画笔
*/
private void initPaint() {
circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(circleColor);
circlePaint.setAntiAlias(true);
circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
path = new Path();
initTextPaint();
}
/**
* 初始化文字画笔
*/
private void initTextPaint() {
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(sp2px(13));
textPaint.setColor(Color.WHITE);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setAntiAlias(true);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
startX = w / 2;
startY = h / 2;
maxDistance = dp2px(100);
radiusStart = dp2px(15);
radiusEnd = dp2px(15);
currentRadiusEnd = radiusEnd;
currentRadiusStart = radiusStart;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
pointStart.set(startX, startY);
if (isOutOfRang) {
if (!disappear) {
drawEndBall(canvas, pointEnd, currentRadiusEnd);
}
} else {
drawStartBall(canvas, pointStart, currentRadiusStart);
if (mIsCanDrag) {
drawEndBall(canvas, pointEnd, currentRadiusEnd);
drawBezier(canvas);
}
}
if (!disappear) {
if (msgCount > 0) {
if (pointEnd.x==0||pointEnd.y==0){
drawText(canvas, msgCount, pointStart);
}else {
drawText(canvas, msgCount, pointEnd);
}
}
}
}
/**
* 绘制文字
*
* @param canvas 画布
*/
private void drawText(Canvas canvas, int msgCount, PointF point) {
textRect.left = (int) (point.x - radiusStart);
textRect.top = (int) (point.y - radiusStart);
textRect.right = (int) (point.x + radiusStart);
textRect.bottom = (int) (point.y + radiusStart);
Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
int baseline = (textRect.bottom + textRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
//文字绘制到整个布局的中心位置
canvas.drawText(msgCount > 99 ? "99+" : msgCount + "", textRect.centerX(), baseline, textPaint);
}
/**
* 画起始小球
*
* @param canvas 画布
* @param pointF 点坐标
* @param radius 半径
*/
private void drawStartBall(Canvas canvas, PointF pointF, float radius) {
canvas.drawCircle(pointF.x, pointF.y, radius, circlePaint);
}
/**
* 画拖拽结束的小球
*
* @param canvas 画布
* @param pointF 点坐标
* @param radius 半径
*/
private void drawEndBall(Canvas canvas, PointF pointF, float radius) {
canvas.drawCircle(pointF.x, pointF.y, radius, circlePaint);
}
/**
* 画贝塞尔曲线
*
* @param canvas 画布
*/
private void drawBezier(Canvas canvas) {
path.reset();
path.moveTo(pointA.x, pointA.y);
path.quadTo(pointO.x, pointO.y, pointB.x, pointB.y);
path.lineTo(pointC.x, pointC.y);
path.quadTo(pointO.x, pointO.y, pointD.x, pointD.y);
path.lineTo(pointA.x, pointA.y);
path.close();
canvas.drawPath(path, circlePaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float currentX;
float currentY;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
setIsCanDrag(event);
break;
case MotionEvent.ACTION_MOVE:
if (mIsCanDrag) {
currentX = event.getX();
currentY = event.getY();
//设置拖拽圆的坐标
pointEnd.set(currentX, currentY);
if (!isOutOfRang) {
setCurrentRadius();
setABCDOPoint();
}
invalidate();
}
break;
case MotionEvent.ACTION_UP:
if (mIsCanDrag) {
if (isOutOfRang) {
//消失动画
disappear = true;
if (onDragBallListener != null) {
onDragBallListener.onDisappear();
}
invalidate();
} else {
disappear = false;
//回弹动画
final float a = (pointEnd.y - pointStart.y) / (pointEnd.x - pointStart.x);
ValueAnimator valueAnimator = ValueAnimator.ofFloat(pointEnd.x, pointStart.x);
valueAnimator.setDuration(500);
valueAnimator.setInterpolator(new BounceInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float x = (float) animation.getAnimatedValue();
float y = pointStart.y + a * (x - pointStart.x);
pointEnd.set(x, y);
setCurrentRadius();
setABCDOPoint();
invalidate();
}
});
valueAnimator.start();
}
}
break;
}
return true;
}
/**
* 设置当前计算的到的半径
*/
private void setCurrentRadius() {
//两个圆心之间的距离
float distance = (float) Math.sqrt(Math.pow(pointStart.x - pointEnd.x, 2) + Math.pow(pointStart.y - pointEnd.y, 2));
//拖拽距离在设置的最大值范围内才绘制贝塞尔图形
if (distance <= maxDistance) {
//比例系数 控制两圆半径缩放
float percent = distance / maxDistance;
//之所以*0.6和0.2只为了放置拖拽过程圆变化的过大和过小这个系数是多次尝试的出的
//你也可以适当调整系数达到自己想要的效果
currentRadiusStart = (1 - percent * 0.6f) * radiusStart;
currentRadiusEnd = (1 + percent * 0.2f) * radiusEnd;
isOutOfRang = false;
} else {
isOutOfRang = true;
currentRadiusStart = radiusStart;
currentRadiusEnd = radiusEnd;
}
}
/**
* 判断是否可以拖拽
*
* @param event event
*/
private void setIsCanDrag(MotionEvent event) {
Rect rect = new Rect();
rect.left = (int) (startX - radiusStart);
rect.top = (int) (startY - radiusStart);
rect.right = (int) (startX + radiusStart);
rect.bottom = (int) (startY + radiusStart);
//触摸点是否在圆的坐标域内
mIsCanDrag = rect.contains((int) event.getX(), (int) event.getY());
}
/**
* 设置贝塞尔曲线的相关点坐标 计算方式参照结算图即可看明白
* (ps为了画个清楚这个图花了不少功夫哦)
*/
private void setABCDOPoint() {
//控制点坐标
pointO.set((pointStart.x + pointEnd.x) / 2.0f, (pointStart.y + pointEnd.y) / 2.0f);
float x = pointEnd.x - pointStart.x;
float y = pointEnd.y - pointStart.y;
//斜率 tanA=rate
double rate;
rate = x / y;
//角度 根据反正切函数算角度
float angle = (float) Math.atan(rate);
pointA.x = (float) (pointStart.x + Math.cos(angle) * currentRadiusStart);
pointA.y = (float) (pointStart.y - Math.sin(angle) * currentRadiusStart);
pointB.x = (float) (pointEnd.x + Math.cos(angle) * currentRadiusEnd);
pointB.y = (float) (pointEnd.y - Math.sin(angle) * currentRadiusEnd);
pointC.x = (float) (pointEnd.x - Math.cos(angle) * currentRadiusEnd);
pointC.y = (float) (pointEnd.y + Math.sin(angle) * currentRadiusEnd);
pointD.x = (float) (pointStart.x - Math.cos(angle) * currentRadiusStart);
pointD.y = (float) (pointStart.y + Math.sin(angle) * currentRadiusStart);
}
/**
* 设置消息数
*
* @param count 消息个数
*/
public void setMsgCount(int count) {
msgCount = count;
invalidate();
}
public void reset() {
msgCount = 0;
mIsCanDrag = false;
isOutOfRang = false;
disappear = false;
pointStart.set(startX, startY);
pointEnd.set(startX, startY);
setABCDOPoint();
invalidate();
}
public void setOnDragBallListener(OnDragBallListener onDragBallListener) {
this.onDragBallListener = onDragBallListener;
}
/**
* 回调事件
*/
public interface OnDragBallListener {
void onDisappear();
}
/**
* dp 2 px
*
* @param dpVal
*/
protected int dp2px(int dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, getResources().getDisplayMetrics());
}
/**
* sp 2 px
*
* @param spVal
* @return
*/
protected int sp2px(int spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, getResources().getDisplayMetrics());
}
}