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
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.gesture;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.AccelerateDecelerateInterpolator;
import com.android.internal.R;
import java.util.ArrayList;
/**
* A (transparent) overlay for gesture input that can be placed on top of other
* widgets.
*
* @attr ref android.R.styleable#GestureOverlayView_gestureStrokeWidth
* @attr ref android.R.styleable#GestureOverlayView_gestureColor
* @attr ref android.R.styleable#GestureOverlayView_uncertainGestureColor
* @attr ref android.R.styleable#GestureOverlayView_fadeDuration
* @attr ref android.R.styleable#GestureOverlayView_fadeOffset
*/
public class GestureOverlayView extends View {
private static final int TRANSPARENT_BACKGROUND = 0x00000000;
private static final boolean GESTURE_RENDERING_ANTIALIAS = true;
private static final boolean DITHER_FLAG = true;
private Paint mGesturePaint;
private final Paint mBitmapPaint = new Paint(Paint.DITHER_FLAG);
private Bitmap mBitmap;
private Canvas mBitmapCanvas;
private long mFadeDuration = 300;
private long mFadeOffset = 300;
private long mFadingStart;
private float mGestureStroke = 12.0f;
private int mCertainGestureColor = 0xFFFFFF00;
private int mUncertainGestureColor = 0x3CFFFF00;
private int mInvalidateExtraBorder = 10;
// for rendering immediate ink feedback
private final Rect mInvalidRect = new Rect();
private final Path mPath = new Path();
private float mX;
private float mY;
private float mCurveEndX;
private float mCurveEndY;
// current gesture
private Gesture mCurrentGesture = null;
// TODO: Make this a list of WeakReferences
private final ArrayList<OnGestureListener> mOnGestureListeners =
new ArrayList<OnGestureListener>();
private final ArrayList<GesturePoint> mPointBuffer = new ArrayList<GesturePoint>(100);
// fading out effect
private boolean mIsFadingOut = false;
private float mFadingAlpha = 1;
private final AccelerateDecelerateInterpolator mInterpolator =
new AccelerateDecelerateInterpolator();
private final Runnable mFadingOut = new Runnable() {
public void run() {
if (mIsFadingOut) {
final long now = AnimationUtils.currentAnimationTimeMillis();
final long duration = now - mFadingStart;
if (duration > mFadeDuration) {
mIsFadingOut = false;
mPath.rewind();
mCurrentGesture = null;
mBitmap.eraseColor(TRANSPARENT_BACKGROUND);
} else {
float interpolatedTime = Math.max(0.0f,
Math.min(1.0f, duration / (float) mFadeDuration));
mFadingAlpha = 1.0f - mInterpolator.getInterpolation(interpolatedTime);
postDelayed(this, 16);
}
invalidate();
}
}
};
public GestureOverlayView(Context context) {
super(context);
init();
}
public GestureOverlayView(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.gestureOverlayViewStyle);
}
public GestureOverlayView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.GestureOverlayView, defStyle, 0);
mGestureStroke = a.getFloat(R.styleable.GestureOverlayView_gestureStrokeWidth,
mGestureStroke);
mInvalidateExtraBorder = Math.max(1, ((int) mGestureStroke) - 1);
mCertainGestureColor = a.getColor(R.styleable.GestureOverlayView_gestureColor,
mCertainGestureColor);
mUncertainGestureColor = a.getColor(R.styleable.GestureOverlayView_uncertainGestureColor,
mUncertainGestureColor);
mFadeDuration = a.getInt(R.styleable.GestureOverlayView_fadeDuration, (int) mFadeDuration);
mFadeOffset = a.getInt(R.styleable.GestureOverlayView_fadeOffset, (int) mFadeOffset);
a.recycle();
init();
}
public ArrayList<GesturePoint> getCurrentStroke() {
return mPointBuffer;
}
public Gesture getCurrentGesture() {
return mCurrentGesture;
}
/**
* Set Gesture color
*
* @param color
*/
public void setGestureDrawingColor(int color) {
mGesturePaint.setColor(color);
if (mCurrentGesture != null) {
mBitmap.eraseColor(TRANSPARENT_BACKGROUND);
mCurrentGesture.draw(mBitmapCanvas, mGesturePaint);
}
invalidate();
}
public void setGestureColor(int color) {
mCertainGestureColor = color;
}
public void setUncertainGestureColor(int color) {
mUncertainGestureColor = color;
}
public int getUncertainGestureColor() {
return mUncertainGestureColor;
}
public int getGestureColor() {
return mCertainGestureColor;
}
public float getGestureStroke() {
return mGestureStroke;
}
public void setGestureStroke(float gestureStroke) {
mGestureStroke = gestureStroke;
mInvalidateExtraBorder = Math.max(1, ((int) mGestureStroke) - 1);
mGesturePaint.setStrokeWidth(mGestureStroke);
}
/**
* Set the gesture to be shown in the view
*
* @param gesture
*/
public void setCurrentGesture(Gesture gesture) {
if (mCurrentGesture != null) {
clear(false);
}
mCurrentGesture = gesture;
if (gesture != null) {
if (mBitmapCanvas != null) {
gesture.draw(mBitmapCanvas, mGesturePaint);
invalidate();
}
}
}
private void init() {
mGesturePaint = new Paint();
final Paint gesturePaint = mGesturePaint;
gesturePaint.setAntiAlias(GESTURE_RENDERING_ANTIALIAS);
gesturePaint.setColor(mCertainGestureColor);
gesturePaint.setStyle(Paint.Style.STROKE);
gesturePaint.setStrokeJoin(Paint.Join.ROUND);
gesturePaint.setStrokeCap(Paint.Cap.ROUND);
gesturePaint.setStrokeWidth(mGestureStroke);
gesturePaint.setDither(DITHER_FLAG);
}
@Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
super.onSizeChanged(width, height, oldWidth, oldHeight);
if (width <= 0 || height <= 0) {
return;
}
int targetWidth = width > oldWidth ? width : oldWidth;
int targetHeight = height > oldHeight ? height : oldHeight;
if (mBitmap != null) mBitmap.recycle();
mBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
if (mBitmapCanvas != null) {
mBitmapCanvas.setBitmap(mBitmap);
} else {
mBitmapCanvas = new Canvas(mBitmap);
}
mBitmapCanvas.drawColor(TRANSPARENT_BACKGROUND);
if (mCurrentGesture != null) {
mCurrentGesture.draw(mBitmapCanvas, mGesturePaint);
}
}
public void addOnGestureListener(OnGestureListener listener) {
mOnGestureListeners.add(listener);
}
public void removeOnGestureListener(OnGestureListener listener) {
mOnGestureListeners.remove(listener);
}
public void removeAllOnGestureListeners() {
mOnGestureListeners.clear();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// draw double buffer
if (mIsFadingOut) {
mBitmapPaint.setAlpha((int) (255 * mFadingAlpha));
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
} else {
mBitmapPaint.setAlpha(255);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
}
// draw the current stroke
canvas.drawPath(mPath, mGesturePaint);
}
/**
* Clear up the overlay
*
* @param fadeOut whether the gesture on the overlay should fade out
* gradually or disappear immediately
*/
public void clear(boolean fadeOut) {
if (fadeOut) {
mFadingAlpha = 1.0f;
mIsFadingOut = true;
removeCallbacks(mFadingOut);
mFadingStart = AnimationUtils.currentAnimationTimeMillis() + mFadeOffset;
postDelayed(mFadingOut, mFadeOffset);
} else {
mPath.rewind();
mCurrentGesture = null;
if (mBitmap != null) {
mBitmap.eraseColor(TRANSPARENT_BACKGROUND);
invalidate();
}
}
}
public void cancelFadingOut() {
mIsFadingOut = false;
removeCallbacks(mFadingOut);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return true;
}
processEvent(event);
return true;
}
public void processEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Rect rect = touchStart(event);
invalidate(rect);
break;
case MotionEvent.ACTION_MOVE:
rect = touchMove(event);
if (rect != null) {
invalidate(rect);
}
break;
case MotionEvent.ACTION_UP:
touchUp(event, false);
invalidate();
break;
case MotionEvent.ACTION_CANCEL:
touchUp(event, true);
invalidate();
break;
}
}
private Rect touchStart(MotionEvent event) {
// pass the event to handlers
final ArrayList<OnGestureListener> listeners = mOnGestureListeners;
final int count = listeners.size();
for (int i = 0; i < count; i++) {
OnGestureListener listener = listeners.get(i);
listener.onGestureStarted(this, event);
}
// if there is fading out going on, stop it.
if (mIsFadingOut) {
mIsFadingOut = false;
removeCallbacks(mFadingOut);
mBitmap.eraseColor(TRANSPARENT_BACKGROUND);
mCurrentGesture = null;
}
float x = event.getX();
float y = event.getY();
mX = x;
mY = y;
if (mCurrentGesture == null) {
mCurrentGesture = new Gesture();
}
mPointBuffer.add(new GesturePoint(x, y, event.getEventTime()));
mPath.rewind();
mPath.moveTo(x, y);
mInvalidRect.set((int) x - mInvalidateExtraBorder, (int) y - mInvalidateExtraBorder,
(int) x + mInvalidateExtraBorder, (int) y + mInvalidateExtraBorder);
mCurveEndX = x;
mCurveEndY = y;
return mInvalidRect;
}
private Rect touchMove(MotionEvent event) {
Rect areaToRefresh = null;
final float x = event.getX();
final float y = event.getY();
final float previousX = mX;
final float previousY = mY;
final float dx = Math.abs(x - previousX);
final float dy = Math.abs(y - previousY);
if (dx >= GestureStroke.TOUCH_TOLERANCE || dy >= GestureStroke.TOUCH_TOLERANCE) {
areaToRefresh = mInvalidRect;
// start with the curve end
areaToRefresh.set(
(int) mCurveEndX - mInvalidateExtraBorder,
(int) mCurveEndY - mInvalidateExtraBorder,
(int) mCurveEndX + mInvalidateExtraBorder,
(int) mCurveEndY + mInvalidateExtraBorder);
mCurveEndX = (x + previousX) / 2;
mCurveEndY = (y + previousY) / 2;
mPath.quadTo(previousX, previousY, mCurveEndX, mCurveEndY);
// union with the control point of the new curve
areaToRefresh.union(
(int) previousX - mInvalidateExtraBorder,
(int) previousY - mInvalidateExtraBorder,
(int) previousX + mInvalidateExtraBorder,
(int) previousY + mInvalidateExtraBorder);
// union with the end point of the new curve
areaToRefresh.union(
(int) mCurveEndX - mInvalidateExtraBorder,
(int) mCurveEndY - mInvalidateExtraBorder,
(int) mCurveEndX + mInvalidateExtraBorder,
(int) mCurveEndY + mInvalidateExtraBorder);
mX = x;
mY = y;
}
mPointBuffer.add(new GesturePoint(x, y, event.getEventTime()));
// pass the event to handlers
final ArrayList<OnGestureListener> listeners = mOnGestureListeners;
final int count = listeners.size();
for (int i = 0; i < count; i++) {
listeners.get(i).onGesture(this, event);
}
return areaToRefresh;
}
private void touchUp(MotionEvent event, boolean cancel) {
// add the stroke to the current gesture
mCurrentGesture.addStroke(new GestureStroke(mPointBuffer));
// add the stroke to the double buffer
mBitmapCanvas.drawPath(mPath, mGesturePaint);
if (!cancel) {
// pass the event to handlers
final ArrayList<OnGestureListener> listeners = mOnGestureListeners;
final int count = listeners.size();
for (int i = 0; i < count; i++) {
listeners.get(i).onGestureEnded(this, event);
}
} else {
// pass the event to handlers
final ArrayList<OnGestureListener> listeners = mOnGestureListeners;
final int count = listeners.size();
for (int i = 0; i < count; i++) {
listeners.get(i).onGestureCancelled(this, event);
}
}
mPath.rewind();
mPointBuffer.clear();
}
/**
* An interface for processing gesture events
*/
public static interface OnGestureListener {
void onGestureStarted(GestureOverlayView overlay, MotionEvent event);
void onGesture(GestureOverlayView overlay, MotionEvent event);
void onGestureEnded(GestureOverlayView overlay, MotionEvent event);
void onGestureCancelled(GestureOverlayView overlay, MotionEvent event);
}
}
|