-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathRSSignatureCaptureView.java
More file actions
executable file
·393 lines (312 loc) · 10.6 KB
/
RSSignatureCaptureView.java
File metadata and controls
executable file
·393 lines (312 loc) · 10.6 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
package com.rssignaturecapture;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.Log;
import android.view.View;
import android.view.MotionEvent;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.RectF;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import java.util.ArrayList;
import java.util.List;
import com.rssignaturecapture.utils.TimedPoint;
import com.rssignaturecapture.utils.ControlTimedPoints;
import com.rssignaturecapture.utils.Bezier;
public class RSSignatureCaptureView extends View {
private static final float STROKE_WIDTH = 5f;
private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
private boolean mIsEmpty;
private OnSignedListener mOnSignedListener;
private int mMinWidth;
private int mMaxWidth;
private float mLastTouchX;
private float mLastTouchY;
private float mLastVelocity;
private float mLastWidth;
private RectF mDirtyRect;
private List<TimedPoint> mPoints;
private Paint mPaint = new Paint();
private Path mPath = new Path();
private Bitmap mSignatureBitmap = null;
private float mVelocityFilterWeight;
private Canvas mSignatureBitmapCanvas = null;
private SignatureCallback callback;
private boolean dragged = false;
private boolean multipleTouchDragged = false;
private int SCROLL_THRESHOLD = 5;
private boolean rotateClockwise = false;
public interface SignatureCallback {
void onDragged();
}
public RSSignatureCaptureView(Context context, SignatureCallback callback) {
super(context);
this.callback = callback;
//Fixed parameters
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mMinWidth = convertDpToPx(8);
mMaxWidth = convertDpToPx(16);
mVelocityFilterWeight = 0.4f;
mPaint.setColor(Color.BLACK);
//Dirty rectangle to update only the changed portion of the view
mDirtyRect = new RectF();
clear();
// set the bg color as white
this.setBackgroundColor(Color.WHITE);
// width and height should cover the screen
this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
/**
* Get signature
*
* @return
*/
public Bitmap getSignature() {
Bitmap signatureBitmap = null;
// set the signature bitmap
if (signatureBitmap == null) {
signatureBitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
}
// important for saving signature
final Canvas canvas = new Canvas(signatureBitmap);
this.draw(canvas);
return signatureBitmap;
}
/**
* clear signature canvas
*/
public void clearSignature() {
clear();
}
private void addPoint(TimedPoint newPoint) {
mPoints.add(newPoint);
if (mPoints.size() > 2) {
// To reduce the initial lag make it work with 3 mPoints
// by copying the first point to the beginning.
if (mPoints.size() == 3) mPoints.add(0, mPoints.get(0));
ControlTimedPoints tmp = calculateCurveControlPoints(mPoints.get(0), mPoints.get(1), mPoints.get(2));
TimedPoint c2 = tmp.c2;
tmp = calculateCurveControlPoints(mPoints.get(1), mPoints.get(2), mPoints.get(3));
TimedPoint c3 = tmp.c1;
Bezier curve = new Bezier(mPoints.get(1), c2, c3, mPoints.get(2));
TimedPoint startPoint = curve.startPoint;
TimedPoint endPoint = curve.endPoint;
float velocity = endPoint.velocityFrom(startPoint);
velocity = Float.isNaN(velocity) ? 0.0f : velocity;
velocity = mVelocityFilterWeight * velocity
+ (1 - mVelocityFilterWeight) * mLastVelocity;
// The new width is a function of the velocity. Higher velocities
// correspond to thinner strokes.
float newWidth = strokeWidth(velocity);
// The Bezier's width starts out as last curve's final width, and
// gradually changes to the stroke width just calculated. The new
// width calculation is based on the velocity between the Bezier's
// start and end mPoints.
addBezier(curve, mLastWidth, newWidth);
mLastVelocity = velocity;
mLastWidth = newWidth;
// Remove the first element from the list,
// so that we always have no more than 4 mPoints in mPoints array.
mPoints.remove(0);
}
}
private void addBezier(Bezier curve, float startWidth, float endWidth) {
ensureSignatureBitmap();
float originalWidth = mPaint.getStrokeWidth();
float widthDelta = endWidth - startWidth;
float drawSteps = (float) Math.floor(curve.length());
for (int i = 0; i < drawSteps; i++) {
// Calculate the Bezier (x, y) coordinate for this step.
float t = ((float) i) / drawSteps;
float tt = t * t;
float ttt = tt * t;
float u = 1 - t;
float uu = u * u;
float uuu = uu * u;
float x = uuu * curve.startPoint.x;
x += 3 * uu * t * curve.control1.x;
x += 3 * u * tt * curve.control2.x;
x += ttt * curve.endPoint.x;
float y = uuu * curve.startPoint.y;
y += 3 * uu * t * curve.control1.y;
y += 3 * u * tt * curve.control2.y;
y += ttt * curve.endPoint.y;
// Set the incremental stroke width and draw.
mPaint.setStrokeWidth(startWidth + ttt * widthDelta);
mSignatureBitmapCanvas.drawPoint(x, y, mPaint);
expandDirtyRect(x, y);
}
mPaint.setStrokeWidth(originalWidth);
}
private void ensureSignatureBitmap() {
if (mSignatureBitmap == null) {
mSignatureBitmap = Bitmap.createBitmap(getWidth(), getHeight(),
Bitmap.Config.ARGB_8888);
mSignatureBitmapCanvas = new Canvas(mSignatureBitmap);
}
}
public void setMinStrokeWidth(int minStrokeWidth) {
mMinWidth = minStrokeWidth;
}
public void setMaxStrokeWidth(int maxStrokeWidth) {
mMaxWidth = maxStrokeWidth;
}
public void setStrokeColor(int color) {
mPaint.setColor(color);
}
private float strokeWidth(float velocity) {
return Math.max(mMaxWidth / (velocity + 1), mMinWidth);
}
public void setRotateClockWise(boolean newRotateClockwise){
rotateClockwise = newRotateClockwise;
}
public boolean getRotateClockwise(){
return rotateClockwise;
}
private ControlTimedPoints calculateCurveControlPoints(TimedPoint s1, TimedPoint s2, TimedPoint s3) {
float dx1 = s1.x - s2.x;
float dy1 = s1.y - s2.y;
float dx2 = s2.x - s3.x;
float dy2 = s2.y - s3.y;
TimedPoint m1 = new TimedPoint((s1.x + s2.x) / 2.0f, (s1.y + s2.y) / 2.0f);
TimedPoint m2 = new TimedPoint((s2.x + s3.x) / 2.0f, (s2.y + s3.y) / 2.0f);
float l1 = (float) Math.sqrt(dx1 * dx1 + dy1 * dy1);
float l2 = (float) Math.sqrt(dx2 * dx2 + dy2 * dy2);
float dxm = (m1.x - m2.x);
float dym = (m1.y - m2.y);
float k = l2 / (l1 + l2);
TimedPoint cm = new TimedPoint(m2.x + dxm * k, m2.y + dym * k);
float tx = s2.x - cm.x;
float ty = s2.y - cm.y;
return new ControlTimedPoints(new TimedPoint(m1.x + tx, m1.y + ty), new TimedPoint(m2.x + tx, m2.y + ty));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled() || event.getPointerCount() > 1 || (multipleTouchDragged && event.getAction() != MotionEvent.ACTION_UP)) {
multipleTouchDragged = true;
return false;
}
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastTouchX = eventX;
mLastTouchY = eventY;
getParent().requestDisallowInterceptTouchEvent(true);
mPoints.clear();
mPath.moveTo(eventX, eventY);
addPoint(new TimedPoint(eventX, eventY));
case MotionEvent.ACTION_MOVE:
if((Math.abs(mLastTouchX - eventX) < SCROLL_THRESHOLD || Math.abs(mLastTouchY - eventY) < SCROLL_THRESHOLD) && dragged) {
return false;
}
resetDirtyRect(eventX, eventY);
addPoint(new TimedPoint(eventX, eventY));
dragged = true;
break;
case MotionEvent.ACTION_UP:
if(mPoints.size() >= 3) {
resetDirtyRect(eventX, eventY);
addPoint(new TimedPoint(eventX, eventY));
getParent().requestDisallowInterceptTouchEvent(true);
setIsEmpty(false);
sendDragEventToReact();
}
dragged = false;
multipleTouchDragged = false;
break;
default:
return false;
}
//invalidate();
invalidate(
(int) (mDirtyRect.left - mMaxWidth),
(int) (mDirtyRect.top - mMaxWidth),
(int) (mDirtyRect.right + mMaxWidth),
(int) (mDirtyRect.bottom + mMaxWidth));
return true;
}
public void sendDragEventToReact() {
if (callback != null && dragged) {
callback.onDragged();
}
}
// all touch events during the drawing
@Override
protected void onDraw(Canvas canvas) {
if (mSignatureBitmap != null) {
canvas.drawBitmap(mSignatureBitmap, 0, 0, mPaint);
}
}
/**
* Called when replaying history to ensure the dirty region includes all
* mPoints.
*
* @param historicalX the previous x coordinate.
* @param historicalY the previous y coordinate.
*/
private void expandDirtyRect(float historicalX, float historicalY) {
if (historicalX < mDirtyRect.left) {
mDirtyRect.left = historicalX;
} else if (historicalX > mDirtyRect.right) {
mDirtyRect.right = historicalX;
}
if (historicalY < mDirtyRect.top) {
mDirtyRect.top = historicalY;
} else if (historicalY > mDirtyRect.bottom) {
mDirtyRect.bottom = historicalY;
}
}
/**
* Resets the dirty region when the motion event occurs.
*
* @param eventX the event x coordinate.
* @param eventY the event y coordinate.
*/
private void resetDirtyRect(float eventX, float eventY) {
// The mLastTouchX and mLastTouchY were set when the ACTION_DOWN motion event occurred.
mDirtyRect.left = Math.min(mLastTouchX, eventX);
mDirtyRect.right = Math.max(mLastTouchX, eventX);
mDirtyRect.top = Math.min(mLastTouchY, eventY);
mDirtyRect.bottom = Math.max(mLastTouchY, eventY);
}
private void setIsEmpty(boolean newValue) {
mIsEmpty = newValue;
if (mOnSignedListener != null) {
if (mIsEmpty) {
mOnSignedListener.onClear();
} else {
mOnSignedListener.onSigned();
}
}
}
public void clear() {
dragged = false;
mPoints = new ArrayList<TimedPoint>();
mLastVelocity = 0;
mLastWidth = (mMinWidth + mMaxWidth) / 2;
mPath.reset();
if (mSignatureBitmap != null) {
mSignatureBitmap = null;
ensureSignatureBitmap();
}
setIsEmpty(true);
invalidate();
}
private int convertDpToPx(float dp){
return Math.round(dp*(getResources().getDisplayMetrics().xdpi/ DisplayMetrics.DENSITY_DEFAULT));
}
public interface OnSignedListener {
public void onSigned();
public void onClear();
}
}