-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathStickerView.java
More file actions
executable file
·611 lines (537 loc) · 22.4 KB
/
StickerView.java
File metadata and controls
executable file
·611 lines (537 loc) · 22.4 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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
package com.example.abner.stickerdemo.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
import com.example.abner.stickerdemo.R;
import com.example.abner.stickerdemo.model.StickerPropertyModel;
/**
* 表情贴纸
*/
public class StickerView extends ImageView {
private static final String TAG = "StickerView";
private Bitmap deleteBitmap;
private Bitmap flipVBitmap;
private Bitmap topBitmap;
private Bitmap resizeBitmap;
private Bitmap mBitmap;
private Rect dst_delete;
private Rect dst_resize;
private Rect dst_flipV;
private Rect dst_top;
private int deleteBitmapWidth;
private int deleteBitmapHeight;
private int resizeBitmapWidth;
private int resizeBitmapHeight;
//水平镜像
private int flipVBitmapWidth;
private int flipVBitmapHeight;
//置顶
private int topBitmapWidth;
private int topBitmapHeight;
private Paint localPaint;
private int mScreenwidth, mScreenHeight;
private static final float BITMAP_SCALE = 0.7f;
private PointF mid = new PointF();
private OperationListener operationListener;
private float lastRotateDegree;
//是否是第二根手指放下
private boolean isPointerDown = false;
//手指移动距离必须超过这个数值
private final float pointerLimitDis = 20f;
private final float pointerZoomCoeff = 0.09f;
/**
* 对角线的长度
*/
private float lastLength;
private boolean isInResize = false;
private Matrix matrix = new Matrix();
/**
* 是否在四条线内部
*/
private boolean isInSide;
private float lastX, lastY;
/**
* 是否在编辑模式
*/
private boolean isInEdit = true;
private float MIN_SCALE = 0.5f;
private float MAX_SCALE = 1.2f;
private double halfDiagonalLength;
private float oringinWidth = 0;
//双指缩放时的初始距离
private float oldDis;
private final long stickerId;
private DisplayMetrics dm;
//水平镜像
private boolean isHorizonMirror = false;
public StickerView(final Context context, final AttributeSet attrs) {
super(context, attrs);
stickerId = 0;
init();
}
public StickerView(final Context context) {
super(context);
stickerId = 0;
init();
}
public StickerView(final Context context, final AttributeSet attrs, final int defStyleAttr) {
super(context, attrs, defStyleAttr);
stickerId = 0;
init();
}
private void init() {
dst_delete = new Rect();
dst_resize = new Rect();
dst_flipV = new Rect();
dst_top = new Rect();
localPaint = new Paint();
localPaint.setColor(getResources().getColor(R.color.red_e73a3d));
localPaint.setAntiAlias(true);
localPaint.setDither(true);
localPaint.setStyle(Paint.Style.STROKE);
localPaint.setStrokeWidth(2.0f);
dm = getResources().getDisplayMetrics();
mScreenwidth = dm.widthPixels;
mScreenHeight = dm.heightPixels;
}
@Override
protected void onDraw(final Canvas canvas) {
if (mBitmap != null) {
float[] arrayOfFloat = new float[9];
matrix.getValues(arrayOfFloat);
float f1 = 0.0F * arrayOfFloat[0] + 0.0F * arrayOfFloat[1] + arrayOfFloat[2];
float f2 = 0.0F * arrayOfFloat[3] + 0.0F * arrayOfFloat[4] + arrayOfFloat[5];
float f3 = arrayOfFloat[0] * this.mBitmap.getWidth() + 0.0F * arrayOfFloat[1] + arrayOfFloat[2];
float f4 = arrayOfFloat[3] * this.mBitmap.getWidth() + 0.0F * arrayOfFloat[4] + arrayOfFloat[5];
float f5 = 0.0F * arrayOfFloat[0] + arrayOfFloat[1] * this.mBitmap.getHeight() + arrayOfFloat[2];
float f6 = 0.0F * arrayOfFloat[3] + arrayOfFloat[4] * this.mBitmap.getHeight() + arrayOfFloat[5];
float f7 = arrayOfFloat[0] * this.mBitmap.getWidth() + arrayOfFloat[1] * this.mBitmap.getHeight() + arrayOfFloat[2];
float f8 = arrayOfFloat[3] * this.mBitmap.getWidth() + arrayOfFloat[4] * this.mBitmap.getHeight() + arrayOfFloat[5];
canvas.save();
canvas.drawBitmap(mBitmap, matrix, null);
//删除在右上角
dst_delete.left = (int) (f3 - deleteBitmapWidth / 2);
dst_delete.right = (int) (f3 + deleteBitmapWidth / 2);
dst_delete.top = (int) (f4 - deleteBitmapHeight / 2);
dst_delete.bottom = (int) (f4 + deleteBitmapHeight / 2);
//拉伸等操作在右下角
dst_resize.left = (int) (f7 - resizeBitmapWidth / 2);
dst_resize.right = (int) (f7 + resizeBitmapWidth / 2);
dst_resize.top = (int) (f8 - resizeBitmapHeight / 2);
dst_resize.bottom = (int) (f8 + resizeBitmapHeight / 2);
//垂直镜像在左上角
dst_top.left = (int) (f1 - flipVBitmapWidth / 2);
dst_top.right = (int) (f1 + flipVBitmapWidth / 2);
dst_top.top = (int) (f2 - flipVBitmapHeight / 2);
dst_top.bottom = (int) (f2 + flipVBitmapHeight / 2);
//水平镜像在左下角
dst_flipV.left = (int) (f5 - topBitmapWidth / 2);
dst_flipV.right = (int) (f5 + topBitmapWidth / 2);
dst_flipV.top = (int) (f6 - topBitmapHeight / 2);
dst_flipV.bottom = (int) (f6 + topBitmapHeight / 2);
if (isInEdit) {
canvas.drawLine(f1, f2, f3, f4, localPaint);
canvas.drawLine(f3, f4, f7, f8, localPaint);
canvas.drawLine(f5, f6, f7, f8, localPaint);
canvas.drawLine(f5, f6, f1, f2, localPaint);
canvas.drawBitmap(deleteBitmap, null, dst_delete, null);
canvas.drawBitmap(resizeBitmap, null, dst_resize, null);
canvas.drawBitmap(flipVBitmap, null, dst_flipV, null);
canvas.drawBitmap(topBitmap, null, dst_top, null);
}
canvas.restore();
}
}
@Override
public void setImageResource(final int resId) {
setBitmap(BitmapFactory.decodeResource(getResources(), resId));
}
public void setBitmap(final Bitmap bitmap) {
matrix.reset();
mBitmap = bitmap;
setDiagonalLength();
initBitmaps();
int w = mBitmap.getWidth();
int h = mBitmap.getHeight();
oringinWidth = w;
float initScale = (MIN_SCALE + MAX_SCALE) / 2;
matrix.postScale(initScale, initScale, w / 2, h / 2);
//Y坐标为 (顶部操作栏+正方形图)/2
matrix.postTranslate(mScreenwidth / 2 - w / 2, (mScreenwidth) / 2 - h / 2);
invalidate();
}
private void setDiagonalLength() {
halfDiagonalLength = Math.hypot(mBitmap.getWidth(), mBitmap.getHeight()) / 2;
}
private void initBitmaps() {
//当图片的宽比高大时 按照宽计算 缩放大小根据图片的大小而改变 最小为图片的1/8 最大为屏幕宽
if (mBitmap.getWidth() >= mBitmap.getHeight()) {
float minWidth = mScreenwidth / 8;
if (mBitmap.getWidth() < minWidth) {
MIN_SCALE = 1f;
} else {
MIN_SCALE = 1.0f * minWidth / mBitmap.getWidth();
}
if (mBitmap.getWidth() > mScreenwidth) {
MAX_SCALE = 1;
} else {
MAX_SCALE = 1.0f * mScreenwidth / mBitmap.getWidth();
}
} else {
//当图片高比宽大时,按照图片的高计算
float minHeight = mScreenwidth / 8;
if (mBitmap.getHeight() < minHeight) {
MIN_SCALE = 1f;
} else {
MIN_SCALE = 1.0f * minHeight / mBitmap.getHeight();
}
if (mBitmap.getHeight() > mScreenwidth) {
MAX_SCALE = 1;
} else {
MAX_SCALE = 1.0f * mScreenwidth / mBitmap.getHeight();
}
}
topBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_top_enable);
deleteBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_delete);
flipVBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_flip);
resizeBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_resize);
deleteBitmapWidth = (int) (deleteBitmap.getWidth() * BITMAP_SCALE);
deleteBitmapHeight = (int) (deleteBitmap.getHeight() * BITMAP_SCALE);
resizeBitmapWidth = (int) (resizeBitmap.getWidth() * BITMAP_SCALE);
resizeBitmapHeight = (int) (resizeBitmap.getHeight() * BITMAP_SCALE);
flipVBitmapWidth = (int) (flipVBitmap.getWidth() * BITMAP_SCALE);
flipVBitmapHeight = (int) (flipVBitmap.getHeight() * BITMAP_SCALE);
topBitmapWidth = (int) (topBitmap.getWidth() * BITMAP_SCALE);
topBitmapHeight = (int) (topBitmap.getHeight() * BITMAP_SCALE);
}
@Override
public boolean onTouchEvent(final MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
boolean handled = true;
switch (action) {
case MotionEvent.ACTION_DOWN:
if (isInButton(event, dst_delete)) {
if (operationListener != null) {
operationListener.onDeleteClick();
}
} else if (isInResize(event)) {
isInResize = true;
lastRotateDegree = rotationToStartPoint(event);
midPointToStartPoint(event);
lastLength = diagonalLength(event);
} else if (isInButton(event, dst_flipV)) {
//水平镜像
PointF localPointF = new PointF();
midDiagonalPoint(localPointF);
matrix.postScale(-1.0F, 1.0F, localPointF.x, localPointF.y);
isHorizonMirror = !isHorizonMirror;
invalidate();
} else if (isInButton(event, dst_top)) {
//置顶
bringToFront();
if (operationListener != null) {
operationListener.onTop(this);
}
} else if (isInBitmap(event)) {
isInSide = true;
lastX = event.getX(0);
lastY = event.getY(0);
} else {
handled = false;
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
if (spacing(event) > pointerLimitDis) {
oldDis = spacing(event);
isPointerDown = true;
midPointToStartPoint(event);
} else {
isPointerDown = false;
}
isInSide = false;
isInResize = false;
break;
case MotionEvent.ACTION_MOVE:
//双指缩放
if (isPointerDown) {
float scale;
float disNew = spacing(event);
if (disNew == 0 || disNew < pointerLimitDis) {
scale = 1;
} else {
scale = disNew / oldDis;
//缩放缓慢
scale = (scale - 1) * pointerZoomCoeff + 1;
}
float scaleTemp = (scale * Math.abs(dst_flipV.left - dst_resize.left)) / oringinWidth;
if (((scaleTemp <= MIN_SCALE)) && scale < 1 ||
(scaleTemp >= MAX_SCALE) && scale > 1) {
scale = 1;
} else {
lastLength = diagonalLength(event);
}
matrix.postScale(scale, scale, mid.x, mid.y);
invalidate();
} else if (isInResize) {
matrix.postRotate((rotationToStartPoint(event) - lastRotateDegree) * 2, mid.x, mid.y);
lastRotateDegree = rotationToStartPoint(event);
float scale = diagonalLength(event) / lastLength;
if (((diagonalLength(event) / halfDiagonalLength <= MIN_SCALE)) && scale < 1 ||
(diagonalLength(event) / halfDiagonalLength >= MAX_SCALE) && scale > 1) {
scale = 1;
if (!isInResize(event)) {
isInResize = false;
}
} else {
lastLength = diagonalLength(event);
}
matrix.postScale(scale, scale, mid.x, mid.y);
invalidate();
} else if (isInSide) {
float x = event.getX(0);
float y = event.getY(0);
//TODO 移动区域判断 不能超出屏幕
matrix.postTranslate(x - lastX, y - lastY);
lastX = x;
lastY = y;
invalidate();
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
isInResize = false;
isInSide = false;
isPointerDown = false;
break;
}
if (handled && operationListener != null) {
operationListener.onEdit(this);
}
return handled;
}
/**
* 计算图片的角度等属性
*
* @param model
* @return
*/
public StickerPropertyModel calculate(final StickerPropertyModel model) {
float[] v = new float[9];
matrix.getValues(v);
// translation is simple
float tx = v[Matrix.MTRANS_X];
float ty = v[Matrix.MTRANS_Y];
Log.d(TAG, "tx : " + tx + " ty : " + ty);
// calculate real scale
float scalex = v[Matrix.MSCALE_X];
float skewy = v[Matrix.MSKEW_Y];
float rScale = (float) Math.sqrt(scalex * scalex + skewy * skewy);
Log.d(TAG, "rScale : " + rScale);
// calculate the degree of rotation
float rAngle = Math.round(Math.atan2(v[Matrix.MSKEW_X], v[Matrix.MSCALE_X]) * (180 / Math.PI));
Log.d(TAG, "rAngle : " + rAngle);
PointF localPointF = new PointF();
midDiagonalPoint(localPointF);
Log.d(TAG, " width : " + (mBitmap.getWidth() * rScale) + " height " + (mBitmap.getHeight() * rScale));
float minX = localPointF.x;
float minY = localPointF.y;
Log.d(TAG, "midX : " + minX + " midY : " + minY);
model.setDegree((float) Math.toRadians(rAngle));
//TODO 占屏幕百分比
float precentWidth = (mBitmap.getWidth() * rScale) / mScreenwidth;
model.setScaling(precentWidth);
model.setxLocation(minX / mScreenwidth);
model.setyLocation(minY / mScreenwidth);
model.setStickerId(stickerId);
if (isHorizonMirror) {
model.setHorizonMirror(1);
} else {
model.setHorizonMirror(2);
}
return model;
}
/**
* 是否在四条线内部
* 图片旋转后 可能存在菱形状态 不能用4个点的坐标范围去判断点击区域是否在图片内
*
* @return
*/
private boolean isInBitmap(final MotionEvent event) {
float[] arrayOfFloat1 = new float[9];
this.matrix.getValues(arrayOfFloat1);
//左上角
float f1 = 0.0F * arrayOfFloat1[0] + 0.0F * arrayOfFloat1[1] + arrayOfFloat1[2];
float f2 = 0.0F * arrayOfFloat1[3] + 0.0F * arrayOfFloat1[4] + arrayOfFloat1[5];
//右上角
float f3 = arrayOfFloat1[0] * this.mBitmap.getWidth() + 0.0F * arrayOfFloat1[1] + arrayOfFloat1[2];
float f4 = arrayOfFloat1[3] * this.mBitmap.getWidth() + 0.0F * arrayOfFloat1[4] + arrayOfFloat1[5];
//左下角
float f5 = 0.0F * arrayOfFloat1[0] + arrayOfFloat1[1] * this.mBitmap.getHeight() + arrayOfFloat1[2];
float f6 = 0.0F * arrayOfFloat1[3] + arrayOfFloat1[4] * this.mBitmap.getHeight() + arrayOfFloat1[5];
//右下角
float f7 = arrayOfFloat1[0] * this.mBitmap.getWidth() + arrayOfFloat1[1] * this.mBitmap.getHeight() + arrayOfFloat1[2];
float f8 = arrayOfFloat1[3] * this.mBitmap.getWidth() + arrayOfFloat1[4] * this.mBitmap.getHeight() + arrayOfFloat1[5];
float[] arrayOfFloat2 = new float[4];
float[] arrayOfFloat3 = new float[4];
//确定X方向的范围
arrayOfFloat2[0] = f1;//左上的x
arrayOfFloat2[1] = f3;//右上的x
arrayOfFloat2[2] = f7;//右下的x
arrayOfFloat2[3] = f5;//左下的x
//确定Y方向的范围
arrayOfFloat3[0] = f2;//左上的y
arrayOfFloat3[1] = f4;//右上的y
arrayOfFloat3[2] = f8;//右下的y
arrayOfFloat3[3] = f6;//左下的y
return pointInRect(arrayOfFloat2, arrayOfFloat3, event.getX(0), event.getY(0));
}
/**
* 判断点是否在一个矩形内部
*
* @param xRange
* @param yRange
* @param x
* @param y
* @return
*/
private boolean pointInRect(final float[] xRange, final float[] yRange, final float x, final float y) {
//四条边的长度
double a1 = Math.hypot(xRange[0] - xRange[1], yRange[0] - yRange[1]);
double a2 = Math.hypot(xRange[1] - xRange[2], yRange[1] - yRange[2]);
double a3 = Math.hypot(xRange[3] - xRange[2], yRange[3] - yRange[2]);
double a4 = Math.hypot(xRange[0] - xRange[3], yRange[0] - yRange[3]);
//待检测点到四个点的距离
double b1 = Math.hypot(x - xRange[0], y - yRange[0]);
double b2 = Math.hypot(x - xRange[1], y - yRange[1]);
double b3 = Math.hypot(x - xRange[2], y - yRange[2]);
double b4 = Math.hypot(x - xRange[3], y - yRange[3]);
double u1 = (a1 + b1 + b2) / 2;
double u2 = (a2 + b2 + b3) / 2;
double u3 = (a3 + b3 + b4) / 2;
double u4 = (a4 + b4 + b1) / 2;
//矩形的面积
double s = a1 * a2;
//海伦公式 计算4个三角形面积
double ss = Math.sqrt(u1 * (u1 - a1) * (u1 - b1) * (u1 - b2))
+ Math.sqrt(u2 * (u2 - a2) * (u2 - b2) * (u2 - b3))
+ Math.sqrt(u3 * (u3 - a3) * (u3 - b3) * (u3 - b4))
+ Math.sqrt(u4 * (u4 - a4) * (u4 - b4) * (u4 - b1));
return Math.abs(s - ss) < 0.5;
}
/**
* 触摸是否在某个button范围
*
* @param event
* @param rect
* @return
*/
private boolean isInButton(MotionEvent event, Rect rect) {
int left = rect.left;
int right = rect.right;
int top = rect.top;
int bottom = rect.bottom;
return event.getX(0) >= left && event.getX(0) <= right && event.getY(0) >= top && event.getY(0) <= bottom;
}
/**
* 触摸是否在拉伸区域内
*
* @param event
* @return
*/
private boolean isInResize(final MotionEvent event) {
int left = -20 + this.dst_resize.left;
int top = -20 + this.dst_resize.top;
int right = 20 + this.dst_resize.right;
int bottom = 20 + this.dst_resize.bottom;
return event.getX(0) >= left && event.getX(0) <= right && event.getY(0) >= top && event.getY(0) <= bottom;
}
/**
* 触摸的位置和图片左上角位置的中点
*
* @param event
*/
private void midPointToStartPoint(final MotionEvent event) {
float[] arrayOfFloat = new float[9];
matrix.getValues(arrayOfFloat);
float f1 = 0.0f * arrayOfFloat[0] + 0.0f * arrayOfFloat[1] + arrayOfFloat[2];
float f2 = 0.0f * arrayOfFloat[3] + 0.0f * arrayOfFloat[4] + arrayOfFloat[5];
float f3 = f1 + event.getX(0);
float f4 = f2 + event.getY(0);
mid.set(f3 / 2, f4 / 2);
}
/**
* 计算对角线交叉的位置
*
* @param paramPointF
*/
private void midDiagonalPoint(final PointF paramPointF) {
float[] arrayOfFloat = new float[9];
this.matrix.getValues(arrayOfFloat);
float f1 = 0.0F * arrayOfFloat[0] + 0.0F * arrayOfFloat[1] + arrayOfFloat[2];
float f2 = 0.0F * arrayOfFloat[3] + 0.0F * arrayOfFloat[4] + arrayOfFloat[5];
float f3 = arrayOfFloat[0] * this.mBitmap.getWidth() + arrayOfFloat[1] * this.mBitmap.getHeight() + arrayOfFloat[2];
float f4 = arrayOfFloat[3] * this.mBitmap.getWidth() + arrayOfFloat[4] * this.mBitmap.getHeight() + arrayOfFloat[5];
float f5 = f1 + f3;
float f6 = f2 + f4;
paramPointF.set(f5 / 2.0F, f6 / 2.0F);
}
/**
* 在滑动旋转过程中,总是以左上角原点作为绝对坐标计算偏转角度
*
* @param event
* @return
*/
private float rotationToStartPoint(final MotionEvent event) {
float[] arrayOfFloat = new float[9];
matrix.getValues(arrayOfFloat);
float x = 0.0f * arrayOfFloat[0] + 0.0f * arrayOfFloat[1] + arrayOfFloat[2];
float y = 0.0f * arrayOfFloat[3] + 0.0f * arrayOfFloat[4] + arrayOfFloat[5];
double arc = Math.atan2(event.getY(0) - y, event.getX(0) - x);
return (float) Math.toDegrees(arc);
}
/**
* 触摸点到矩形中点的距离
*
* @param event
* @return
*/
private float diagonalLength(final MotionEvent event) {
float diagonalLength = (float) Math.hypot(event.getX(0) - mid.x, event.getY(0) - mid.y);
return diagonalLength;
}
/**
* 计算双指之间的距离
*/
private float spacing(final MotionEvent event) {
if (event.getPointerCount() == 2) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
} else {
return 0;
}
}
public interface OperationListener {
void onDeleteClick();
void onEdit(StickerView stickerView);
void onTop(StickerView stickerView);
}
public void setOperationListener(final OperationListener operationListener) {
this.operationListener = operationListener;
}
public void setInEdit(final boolean isInEdit) {
this.isInEdit = isInEdit;
invalidate();
}
}