@phucprime/react-native-image-editor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +247 -0
  3. package/android/build.gradle +62 -0
  4. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  5. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  6. package/android/gradlew +240 -0
  7. package/android/gradlew.bat +91 -0
  8. package/android/libs/photo-editor-android.jar +0 -0
  9. package/android/src/main/AndroidManifest.xml +5 -0
  10. package/android/src/main/java/com/ahmedadeltito/photoeditor/BaseActivity.java +300 -0
  11. package/android/src/main/java/com/ahmedadeltito/photoeditor/ColorPickerAdapter.java +129 -0
  12. package/android/src/main/java/com/ahmedadeltito/photoeditor/EmojiAdapter.java +91 -0
  13. package/android/src/main/java/com/ahmedadeltito/photoeditor/EmojiFragment.java +56 -0
  14. package/android/src/main/java/com/ahmedadeltito/photoeditor/GalleryUtils.java +73 -0
  15. package/android/src/main/java/com/ahmedadeltito/photoeditor/ImageAdapter.java +69 -0
  16. package/android/src/main/java/com/ahmedadeltito/photoeditor/ImageFragment.java +93 -0
  17. package/android/src/main/java/com/ahmedadeltito/photoeditor/MainActivity.java +29 -0
  18. package/android/src/main/java/com/ahmedadeltito/photoeditor/MediaActivity.java +95 -0
  19. package/android/src/main/java/com/ahmedadeltito/photoeditor/PhotoEditorActivity.java +1055 -0
  20. package/android/src/main/java/com/ahmedadeltito/photoeditor/TranslationService.java +31 -0
  21. package/android/src/main/java/com/ahmedadeltito/photoeditor/UtilFunctions.java +176 -0
  22. package/android/src/main/java/com/ahmedadeltito/photoeditor/widget/RealtimeBlurView.java +359 -0
  23. package/android/src/main/java/com/ahmedadeltito/photoeditor/widget/SlidingUpPanelLayout.java +1527 -0
  24. package/android/src/main/java/com/ahmedadeltito/photoeditor/widget/ViewDragHelper.java +1466 -0
  25. package/android/src/main/java/ui/photoeditor/RNPhotoEditorModule.java +108 -0
  26. package/android/src/main/java/ui/photoeditor/RNPhotoEditorPackage.java +29 -0
  27. package/android/src/main/res/drawable/above_shadow.xml +7 -0
  28. package/android/src/main/res/drawable/below_shadow.xml +7 -0
  29. package/android/src/main/res/drawable/fading_shadow.xml +7 -0
  30. package/android/src/main/res/drawable/rounded_border_text_view.xml +13 -0
  31. package/android/src/main/res/drawable-xhdpi/aa.png +0 -0
  32. package/android/src/main/res/drawable-xhdpi/bb.png +0 -0
  33. package/android/src/main/res/drawable-xhdpi/c.png +0 -0
  34. package/android/src/main/res/drawable-xhdpi/cc.png +0 -0
  35. package/android/src/main/res/drawable-xhdpi/d.png +0 -0
  36. package/android/src/main/res/drawable-xhdpi/dd.png +0 -0
  37. package/android/src/main/res/drawable-xhdpi/e.png +0 -0
  38. package/android/src/main/res/drawable-xhdpi/ee.png +0 -0
  39. package/android/src/main/res/drawable-xhdpi/i.png +0 -0
  40. package/android/src/main/res/drawable-xhdpi/j.png +0 -0
  41. package/android/src/main/res/drawable-xhdpi/k.png +0 -0
  42. package/android/src/main/res/drawable-xhdpi/l.png +0 -0
  43. package/android/src/main/res/drawable-xhdpi/m.png +0 -0
  44. package/android/src/main/res/drawable-xhdpi/n.png +0 -0
  45. package/android/src/main/res/drawable-xhdpi/o.png +0 -0
  46. package/android/src/main/res/drawable-xhdpi/p.png +0 -0
  47. package/android/src/main/res/drawable-xhdpi/q.png +0 -0
  48. package/android/src/main/res/drawable-xhdpi/r.png +0 -0
  49. package/android/src/main/res/drawable-xhdpi/s.png +0 -0
  50. package/android/src/main/res/drawable-xhdpi/t.png +0 -0
  51. package/android/src/main/res/drawable-xhdpi/u.png +0 -0
  52. package/android/src/main/res/drawable-xhdpi/v.png +0 -0
  53. package/android/src/main/res/drawable-xhdpi/w.png +0 -0
  54. package/android/src/main/res/drawable-xhdpi/x.png +0 -0
  55. package/android/src/main/res/drawable-xhdpi/y.png +0 -0
  56. package/android/src/main/res/drawable-xhdpi/z.png +0 -0
  57. package/android/src/main/res/layout/activity_main.xml +28 -0
  58. package/android/src/main/res/layout/activity_photo_editor.xml +316 -0
  59. package/android/src/main/res/layout/add_text_popup_window.xml +47 -0
  60. package/android/src/main/res/layout/color_picker_item_list.xml +15 -0
  61. package/android/src/main/res/layout/fragment_main_photo_edit_emoji.xml +11 -0
  62. package/android/src/main/res/layout/fragment_main_photo_edit_image.xml +10 -0
  63. package/android/src/main/res/layout/fragment_photo_edit_emoji_item_list.xml +17 -0
  64. package/android/src/main/res/layout/fragment_photo_edit_image_item_list.xml +16 -0
  65. package/android/src/main/res/raw/emojioneandroid +0 -0
  66. package/android/src/main/res/raw/eventtusicons +0 -0
  67. package/android/src/main/res/raw/font_awesome_solid.otf +0 -0
  68. package/android/src/main/res/raw/icomoon.ttf +0 -0
  69. package/android/src/main/res/values/arrays.xml +447 -0
  70. package/android/src/main/res/values/attrs.xml +30 -0
  71. package/android/src/main/res/values/colors.xml +21 -0
  72. package/android/src/main/res/values/dimens.xml +4 -0
  73. package/android/src/main/res/values/strings.xml +33 -0
  74. package/android/src/main/res/values/styles.xml +27 -0
  75. package/ios/RNImageEditor.podspec +26 -0
  76. package/ios/RNPhotoEditor.h +13 -0
  77. package/ios/RNPhotoEditor.m +163 -0
  78. package/ios/RNPhotoEditor.xcodeproj/project.pbxproj +362 -0
  79. package/lib/index.d.ts +125 -0
  80. package/lib/index.d.ts.map +1 -0
  81. package/lib/index.js +114 -0
  82. package/lib/index.js.map +1 -0
  83. package/package.json +73 -0
  84. package/react-native-image-editor.podspec +39 -0
  85. package/src/index.ts +232 -0
@@ -0,0 +1,1466 @@
1
+ /*
2
+ * Copyright (C) 2013 The Android Open Source Project
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+
18
+ package com.ahmedadeltito.photoeditor.widget;
19
+
20
+ import android.content.Context;
21
+ import androidx.core.view.MotionEventCompat;
22
+ import androidx.core.view.VelocityTrackerCompat;
23
+ import androidx.core.view.ViewCompat;
24
+ import androidx.core.widget.ScrollerCompat;
25
+ import android.view.MotionEvent;
26
+ import android.view.VelocityTracker;
27
+ import android.view.View;
28
+ import android.view.ViewConfiguration;
29
+ import android.view.ViewGroup;
30
+ import android.view.animation.Interpolator;
31
+
32
+ import java.util.Arrays;
33
+
34
+ /**
35
+ * ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number
36
+ * of useful operations and state tracking for allowing a user to drag and reposition
37
+ * views within their parent ViewGroup.
38
+ */
39
+ public class ViewDragHelper {
40
+ private static final String TAG = "ViewDragHelper";
41
+
42
+ /**
43
+ * A null/invalid pointer ID.
44
+ */
45
+ public static final int INVALID_POINTER = -1;
46
+
47
+ /**
48
+ * A view is not currently being dragged or animating as a result of a fling/snap.
49
+ */
50
+ public static final int STATE_IDLE = 0;
51
+
52
+ /**
53
+ * A view is currently being dragged. The position is currently changing as a result
54
+ * of user input or simulated user input.
55
+ */
56
+ public static final int STATE_DRAGGING = 1;
57
+
58
+ /**
59
+ * A view is currently settling into place as a result of a fling or
60
+ * predefined non-interactive motion.
61
+ */
62
+ public static final int STATE_SETTLING = 2;
63
+
64
+ /**
65
+ * Edge flag indicating that the left edge should be affected.
66
+ */
67
+ public static final int EDGE_LEFT = 1 << 0;
68
+
69
+ /**
70
+ * Edge flag indicating that the right edge should be affected.
71
+ */
72
+ public static final int EDGE_RIGHT = 1 << 1;
73
+
74
+ /**
75
+ * Edge flag indicating that the top edge should be affected.
76
+ */
77
+ public static final int EDGE_TOP = 1 << 2;
78
+
79
+ /**
80
+ * Edge flag indicating that the bottom edge should be affected.
81
+ */
82
+ public static final int EDGE_BOTTOM = 1 << 3;
83
+
84
+ /**
85
+ * Edge flag set indicating all edges should be affected.
86
+ */
87
+ public static final int EDGE_ALL = EDGE_LEFT | EDGE_TOP | EDGE_RIGHT | EDGE_BOTTOM;
88
+
89
+ /**
90
+ * Indicates that a check should occur along the horizontal axis
91
+ */
92
+ public static final int DIRECTION_HORIZONTAL = 1 << 0;
93
+
94
+ /**
95
+ * Indicates that a check should occur along the vertical axis
96
+ */
97
+ public static final int DIRECTION_VERTICAL = 1 << 1;
98
+
99
+ /**
100
+ * Indicates that a check should occur along all axes
101
+ */
102
+ public static final int DIRECTION_ALL = DIRECTION_HORIZONTAL | DIRECTION_VERTICAL;
103
+
104
+ private static final int EDGE_SIZE = 20; // dp
105
+
106
+ private static final int BASE_SETTLE_DURATION = 256; // ms
107
+ private static final int MAX_SETTLE_DURATION = 600; // ms
108
+
109
+ // Current drag state; idle, dragging or settling
110
+ private int mDragState;
111
+
112
+ // Distance to travel before a drag may begin
113
+ private int mTouchSlop;
114
+
115
+ // Last known position/pointer tracking
116
+ private int mActivePointerId = INVALID_POINTER;
117
+ private float[] mInitialMotionX;
118
+ private float[] mInitialMotionY;
119
+ private float[] mLastMotionX;
120
+ private float[] mLastMotionY;
121
+ private int[] mInitialEdgesTouched;
122
+ private int[] mEdgeDragsInProgress;
123
+ private int[] mEdgeDragsLocked;
124
+ private int mPointersDown;
125
+
126
+ private VelocityTracker mVelocityTracker;
127
+ private float mMaxVelocity;
128
+ private float mMinVelocity;
129
+
130
+ private int mEdgeSize;
131
+ private int mTrackingEdges;
132
+
133
+ private ScrollerCompat mScroller;
134
+
135
+ private final Callback mCallback;
136
+
137
+ private View mCapturedView;
138
+ private boolean mReleaseInProgress;
139
+
140
+ private final ViewGroup mParentView;
141
+
142
+ /**
143
+ * A Callback is used as a communication channel with the ViewDragHelper back to the
144
+ * parent view using it. <code>on*</code>methods are invoked on siginficant events and several
145
+ * accessor methods are expected to provide the ViewDragHelper with more information
146
+ * about the state of the parent view upon request. The callback also makes decisions
147
+ * governing the range and draggability of child views.
148
+ */
149
+ public static abstract class Callback {
150
+ /**
151
+ * Called when the drag state changes. See the <code>STATE_*</code> constants
152
+ * for more information.
153
+ *
154
+ * @param state The new drag state
155
+ *
156
+ * @see #STATE_IDLE
157
+ * @see #STATE_DRAGGING
158
+ * @see #STATE_SETTLING
159
+ */
160
+ public void onViewDragStateChanged(int state) {}
161
+
162
+ /**
163
+ * Called when the captured view's position changes as the result of a drag or settle.
164
+ *
165
+ * @param changedView View whose position changed
166
+ * @param left New X coordinate of the left edge of the view
167
+ * @param top New Y coordinate of the top edge of the view
168
+ * @param dx Change in X position from the last call
169
+ * @param dy Change in Y position from the last call
170
+ */
171
+ public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {}
172
+
173
+ /**
174
+ * Called when a child view is captured for dragging or settling. The ID of the pointer
175
+ * currently dragging the captured view is supplied. If activePointerId is
176
+ * identified as {@link #INVALID_POINTER} the capture is programmatic instead of
177
+ * pointer-initiated.
178
+ *
179
+ * @param capturedChild Child view that was captured
180
+ * @param activePointerId Pointer id tracking the child capture
181
+ */
182
+ public void onViewCaptured(View capturedChild, int activePointerId) {}
183
+
184
+ /**
185
+ * Called when the child view is no longer being actively dragged.
186
+ * The fling velocity is also supplied, if relevant. The velocity values may
187
+ * be clamped to system minimums or maximums.
188
+ *
189
+ * <p>Calling code may decide to fling or otherwise release the view to let it
190
+ * settle into place. It should do so using {@link #settleCapturedViewAt(int, int)}
191
+ * or {@link #flingCapturedView(int, int, int, int)}. If the Callback invokes
192
+ * one of these methods, the ViewDragHelper will enter {@link #STATE_SETTLING}
193
+ * and the view capture will not fully end until it comes to a complete stop.
194
+ * If neither of these methods is invoked before <code>onViewReleased</code> returns,
195
+ * the view will stop in place and the ViewDragHelper will return to
196
+ * {@link #STATE_IDLE}.</p>
197
+ *
198
+ * @param releasedChild The captured child view now being released
199
+ * @param xvel X velocity of the pointer as it left the screen in pixels per second.
200
+ * @param yvel Y velocity of the pointer as it left the screen in pixels per second.
201
+ */
202
+ public void onViewReleased(View releasedChild, float xvel, float yvel) {}
203
+
204
+ /**
205
+ * Called when one of the subscribed edges in the parent view has been touched
206
+ * by the user while no child view is currently captured.
207
+ *
208
+ * @param edgeFlags A combination of edge flags describing the edge(s) currently touched
209
+ * @param pointerId ID of the pointer touching the described edge(s)
210
+ * @see #EDGE_LEFT
211
+ * @see #EDGE_TOP
212
+ * @see #EDGE_RIGHT
213
+ * @see #EDGE_BOTTOM
214
+ */
215
+ public void onEdgeTouched(int edgeFlags, int pointerId) {}
216
+
217
+ /**
218
+ * Called when the given edge may become locked. This can happen if an edge drag
219
+ * was preliminarily rejected before beginning, but after {@link #onEdgeTouched(int, int)}
220
+ * was called. This method should return true to lock this edge or false to leave it
221
+ * unlocked. The default behavior is to leave edges unlocked.
222
+ *
223
+ * @param edgeFlags A combination of edge flags describing the edge(s) locked
224
+ * @return true to lock the edge, false to leave it unlocked
225
+ */
226
+ public boolean onEdgeLock(int edgeFlags) {
227
+ return false;
228
+ }
229
+
230
+ /**
231
+ * Called when the user has started a deliberate drag away from one
232
+ * of the subscribed edges in the parent view while no child view is currently captured.
233
+ *
234
+ * @param edgeFlags A combination of edge flags describing the edge(s) dragged
235
+ * @param pointerId ID of the pointer touching the described edge(s)
236
+ * @see #EDGE_LEFT
237
+ * @see #EDGE_TOP
238
+ * @see #EDGE_RIGHT
239
+ * @see #EDGE_BOTTOM
240
+ */
241
+ public void onEdgeDragStarted(int edgeFlags, int pointerId) {}
242
+
243
+ /**
244
+ * Called to determine the Z-order of child views.
245
+ *
246
+ * @param index the ordered position to query for
247
+ * @return index of the view that should be ordered at position <code>index</code>
248
+ */
249
+ public int getOrderedChildIndex(int index) {
250
+ return index;
251
+ }
252
+
253
+ /**
254
+ * Return the magnitude of a draggable child view's horizontal range of motion in pixels.
255
+ * This method should return 0 for views that cannot move horizontally.
256
+ *
257
+ * @param child Child view to check
258
+ * @return range of horizontal motion in pixels
259
+ */
260
+ public int getViewHorizontalDragRange(View child) {
261
+ return 0;
262
+ }
263
+
264
+ /**
265
+ * Return the magnitude of a draggable child view's vertical range of motion in pixels.
266
+ * This method should return 0 for views that cannot move vertically.
267
+ *
268
+ * @param child Child view to check
269
+ * @return range of vertical motion in pixels
270
+ */
271
+ public int getViewVerticalDragRange(View child) {
272
+ return 0;
273
+ }
274
+
275
+ /**
276
+ * Called when the user's input indicates that they want to capture the given child view
277
+ * with the pointer indicated by pointerId. The callback should return true if the user
278
+ * is permitted to drag the given view with the indicated pointer.
279
+ *
280
+ * <p>ViewDragHelper may call this method multiple times for the same view even if
281
+ * the view is already captured; this indicates that a new pointer is trying to take
282
+ * control of the view.</p>
283
+ *
284
+ * <p>If this method returns true, a call to {@link #onViewCaptured(View, int)}
285
+ * will follow if the capture is successful.</p>
286
+ *
287
+ * @param child Child the user is attempting to capture
288
+ * @param pointerId ID of the pointer attempting the capture
289
+ * @return true if capture should be allowed, false otherwise
290
+ */
291
+ public abstract boolean tryCaptureView(View child, int pointerId);
292
+
293
+ /**
294
+ * Restrict the motion of the dragged child view along the horizontal axis.
295
+ * The default implementation does not allow horizontal motion; the extending
296
+ * class must override this method and provide the desired clamping.
297
+ *
298
+ *
299
+ * @param child Child view being dragged
300
+ * @param left Attempted motion along the X axis
301
+ * @param dx Proposed change in position for left
302
+ * @return The new clamped position for left
303
+ */
304
+ public int clampViewPositionHorizontal(View child, int left, int dx) {
305
+ return 0;
306
+ }
307
+
308
+ /**
309
+ * Restrict the motion of the dragged child view along the vertical axis.
310
+ * The default implementation does not allow vertical motion; the extending
311
+ * class must override this method and provide the desired clamping.
312
+ *
313
+ *
314
+ * @param child Child view being dragged
315
+ * @param top Attempted motion along the Y axis
316
+ * @param dy Proposed change in position for top
317
+ * @return The new clamped position for top
318
+ */
319
+ public int clampViewPositionVertical(View child, int top, int dy) {
320
+ return 0;
321
+ }
322
+ }
323
+
324
+ /**
325
+ * Interpolator defining the animation curve for mScroller
326
+ */
327
+ private static final Interpolator sInterpolator = new Interpolator() {
328
+ public float getInterpolation(float t) {
329
+ t -= 1.0f;
330
+ return t * t * t * t * t + 1.0f;
331
+ }
332
+ };
333
+
334
+ private final Runnable mSetIdleRunnable = new Runnable() {
335
+ public void run() {
336
+ setDragState(STATE_IDLE);
337
+ }
338
+ };
339
+
340
+ /**
341
+ * Factory method to create a new ViewDragHelper.
342
+ *
343
+ * @param forParent Parent view to monitor
344
+ * @param cb Callback to provide information and receive events
345
+ * @return a new ViewDragHelper instance
346
+ */
347
+ public static ViewDragHelper create(ViewGroup forParent, Callback cb) {
348
+ return new ViewDragHelper(forParent.getContext(), forParent, cb);
349
+ }
350
+
351
+ /**
352
+ * Factory method to create a new ViewDragHelper.
353
+ *
354
+ * @param forParent Parent view to monitor
355
+ * @param sensitivity Multiplier for how sensitive the helper should be about detecting
356
+ * the start of a drag. Larger values are more sensitive. 1.0f is normal.
357
+ * @param cb Callback to provide information and receive events
358
+ * @return a new ViewDragHelper instance
359
+ */
360
+ public static ViewDragHelper create(ViewGroup forParent, float sensitivity, Callback cb) {
361
+ final ViewDragHelper helper = create(forParent, cb);
362
+ helper.mTouchSlop = (int) (helper.mTouchSlop * (1 / sensitivity));
363
+ return helper;
364
+ }
365
+
366
+ /**
367
+ * Apps should use ViewDragHelper.create() to get a new instance.
368
+ * This will allow VDH to use internal compatibility implementations for different
369
+ * platform versions.
370
+ *
371
+ * @param context Context to initialize config-dependent params from
372
+ * @param forParent Parent view to monitor
373
+ */
374
+ private ViewDragHelper(Context context, ViewGroup forParent, Callback cb) {
375
+ if (forParent == null) {
376
+ throw new IllegalArgumentException("Parent view may not be null");
377
+ }
378
+ if (cb == null) {
379
+ throw new IllegalArgumentException("Callback may not be null");
380
+ }
381
+
382
+ mParentView = forParent;
383
+ mCallback = cb;
384
+
385
+ final ViewConfiguration vc = ViewConfiguration.get(context);
386
+ final float density = context.getResources().getDisplayMetrics().density;
387
+ mEdgeSize = (int) (EDGE_SIZE * density + 0.5f);
388
+
389
+ mTouchSlop = vc.getScaledTouchSlop();
390
+ mMaxVelocity = vc.getScaledMaximumFlingVelocity();
391
+ mMinVelocity = vc.getScaledMinimumFlingVelocity();
392
+ mScroller = ScrollerCompat.create(context, sInterpolator);
393
+ }
394
+
395
+ /**
396
+ * Set the minimum velocity that will be detected as having a magnitude greater than zero
397
+ * in pixels per second. Callback methods accepting a velocity will be clamped appropriately.
398
+ *
399
+ * @param minVel Minimum velocity to detect
400
+ */
401
+ public void setMinVelocity(float minVel) {
402
+ mMinVelocity = minVel;
403
+ }
404
+
405
+ /**
406
+ * Return the currently configured minimum velocity. Any flings with a magnitude less
407
+ * than this value in pixels per second. Callback methods accepting a velocity will receive
408
+ * zero as a velocity value if the real detected velocity was below this threshold.
409
+ *
410
+ * @return the minimum velocity that will be detected
411
+ */
412
+ public float getMinVelocity() {
413
+ return mMinVelocity;
414
+ }
415
+
416
+ /**
417
+ * Retrieve the current drag state of this helper. This will return one of
418
+ * {@link #STATE_IDLE}, {@link #STATE_DRAGGING} or {@link #STATE_SETTLING}.
419
+ * @return The current drag state
420
+ */
421
+ public int getViewDragState() {
422
+ return mDragState;
423
+ }
424
+
425
+ /**
426
+ * Enable edge tracking for the selected edges of the parent view.
427
+ * The callback's {@link Callback#onEdgeTouched(int, int)} and
428
+ * {@link Callback#onEdgeDragStarted(int, int)} methods will only be invoked
429
+ * for edges for which edge tracking has been enabled.
430
+ *
431
+ * @param edgeFlags Combination of edge flags describing the edges to watch
432
+ * @see #EDGE_LEFT
433
+ * @see #EDGE_TOP
434
+ * @see #EDGE_RIGHT
435
+ * @see #EDGE_BOTTOM
436
+ */
437
+ public void setEdgeTrackingEnabled(int edgeFlags) {
438
+ mTrackingEdges = edgeFlags;
439
+ }
440
+
441
+ /**
442
+ * Return the size of an edge. This is the range in pixels along the edges of this view
443
+ * that will actively detect edge touches or drags if edge tracking is enabled.
444
+ *
445
+ * @return The size of an edge in pixels
446
+ * @see #setEdgeTrackingEnabled(int)
447
+ */
448
+ public int getEdgeSize() {
449
+ return mEdgeSize;
450
+ }
451
+
452
+ /**
453
+ * Capture a specific child view for dragging within the parent. The callback will be notified
454
+ * but {@link Callback#tryCaptureView(View, int)} will not be asked permission to
455
+ * capture this view.
456
+ *
457
+ * @param childView Child view to capture
458
+ * @param activePointerId ID of the pointer that is dragging the captured child view
459
+ */
460
+ public void captureChildView(View childView, int activePointerId) {
461
+ if (childView.getParent() != mParentView) {
462
+ throw new IllegalArgumentException("captureChildView: parameter must be a descendant " +
463
+ "of the ViewDragHelper's tracked parent view (" + mParentView + ")");
464
+ }
465
+
466
+ mCapturedView = childView;
467
+ mActivePointerId = activePointerId;
468
+ mCallback.onViewCaptured(childView, activePointerId);
469
+ setDragState(STATE_DRAGGING);
470
+ }
471
+
472
+ /**
473
+ * @return The currently captured view, or null if no view has been captured.
474
+ */
475
+ public View getCapturedView() {
476
+ return mCapturedView;
477
+ }
478
+
479
+ /**
480
+ * @return The ID of the pointer currently dragging the captured view,
481
+ * or {@link #INVALID_POINTER}.
482
+ */
483
+ public int getActivePointerId() {
484
+ return mActivePointerId;
485
+ }
486
+
487
+ /**
488
+ * @return The minimum distance in pixels that the user must travel to initiate a drag
489
+ */
490
+ public int getTouchSlop() {
491
+ return mTouchSlop;
492
+ }
493
+
494
+ /**
495
+ * The result of a call to this method is equivalent to
496
+ * {@link #processTouchEvent(MotionEvent)} receiving an ACTION_CANCEL event.
497
+ */
498
+ public void cancel() {
499
+ mActivePointerId = INVALID_POINTER;
500
+ clearMotionHistory();
501
+
502
+ if (mVelocityTracker != null) {
503
+ mVelocityTracker.recycle();
504
+ mVelocityTracker = null;
505
+ }
506
+ }
507
+
508
+ /**
509
+ * {@link #cancel()}, but also abort all motion in progress and snap to the end of any
510
+ * animation.
511
+ */
512
+ public void abort() {
513
+ cancel();
514
+ if (mDragState == STATE_SETTLING) {
515
+ final int oldX = mScroller.getCurrX();
516
+ final int oldY = mScroller.getCurrY();
517
+ mScroller.abortAnimation();
518
+ final int newX = mScroller.getCurrX();
519
+ final int newY = mScroller.getCurrY();
520
+ mCallback.onViewPositionChanged(mCapturedView, newX, newY, newX - oldX, newY - oldY);
521
+ }
522
+ setDragState(STATE_IDLE);
523
+ }
524
+
525
+ /**
526
+ * Animate the view <code>child</code> to the given (left, top) position.
527
+ * If this method returns true, the caller should invoke {@link #continueSettling(boolean)}
528
+ * on each subsequent frame to continue the motion until it returns false. If this method
529
+ * returns false there is no further work to do to complete the movement.
530
+ *
531
+ * <p>This operation does not count as a capture event, though {@link #getCapturedView()}
532
+ * will still report the sliding view while the slide is in progress.</p>
533
+ *
534
+ * @param child Child view to capture and animate
535
+ * @param finalLeft Final left position of child
536
+ * @param finalTop Final top position of child
537
+ * @return true if animation should continue through {@link #continueSettling(boolean)} calls
538
+ */
539
+ public boolean smoothSlideViewTo(View child, int finalLeft, int finalTop) {
540
+ mCapturedView = child;
541
+ mActivePointerId = INVALID_POINTER;
542
+
543
+ return forceSettleCapturedViewAt(finalLeft, finalTop, 0, 0);
544
+ }
545
+
546
+ /**
547
+ * Settle the captured view at the given (left, top) position.
548
+ * The appropriate velocity from prior motion will be taken into account.
549
+ * If this method returns true, the caller should invoke {@link #continueSettling(boolean)}
550
+ * on each subsequent frame to continue the motion until it returns false. If this method
551
+ * returns false there is no further work to do to complete the movement.
552
+ *
553
+ * @param finalLeft Settled left edge position for the captured view
554
+ * @param finalTop Settled top edge position for the captured view
555
+ * @return true if animation should continue through {@link #continueSettling(boolean)} calls
556
+ */
557
+ public boolean settleCapturedViewAt(int finalLeft, int finalTop) {
558
+ if (!mReleaseInProgress) {
559
+ throw new IllegalStateException("Cannot settleCapturedViewAt outside of a call to " +
560
+ "Callback#onViewReleased");
561
+ }
562
+
563
+ return forceSettleCapturedViewAt(finalLeft, finalTop,
564
+ (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
565
+ (int) VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId));
566
+ }
567
+
568
+ /**
569
+ * Settle the captured view at the given (left, top) position.
570
+ *
571
+ * @param finalLeft Target left position for the captured view
572
+ * @param finalTop Target top position for the captured view
573
+ * @param xvel Horizontal velocity
574
+ * @param yvel Vertical velocity
575
+ * @return true if animation should continue through {@link #continueSettling(boolean)} calls
576
+ */
577
+ private boolean forceSettleCapturedViewAt(int finalLeft, int finalTop, int xvel, int yvel) {
578
+ final int startLeft = mCapturedView.getLeft();
579
+ final int startTop = mCapturedView.getTop();
580
+ final int dx = finalLeft - startLeft;
581
+ final int dy = finalTop - startTop;
582
+
583
+ if (dx == 0 && dy == 0) {
584
+ // Nothing to do. Send callbacks, be done.
585
+ mScroller.abortAnimation();
586
+ setDragState(STATE_IDLE);
587
+ return false;
588
+ }
589
+
590
+ final int duration = computeSettleDuration(mCapturedView, dx, dy, xvel, yvel);
591
+ mScroller.startScroll(startLeft, startTop, dx, dy, duration);
592
+
593
+ setDragState(STATE_SETTLING);
594
+ return true;
595
+ }
596
+
597
+ private int computeSettleDuration(View child, int dx, int dy, int xvel, int yvel) {
598
+ xvel = clampMag(xvel, (int) mMinVelocity, (int) mMaxVelocity);
599
+ yvel = clampMag(yvel, (int) mMinVelocity, (int) mMaxVelocity);
600
+ final int absDx = Math.abs(dx);
601
+ final int absDy = Math.abs(dy);
602
+ final int absXVel = Math.abs(xvel);
603
+ final int absYVel = Math.abs(yvel);
604
+ final int addedVel = absXVel + absYVel;
605
+ final int addedDistance = absDx + absDy;
606
+
607
+ final float xweight = xvel != 0 ? (float) absXVel / addedVel :
608
+ (float) absDx / addedDistance;
609
+ final float yweight = yvel != 0 ? (float) absYVel / addedVel :
610
+ (float) absDy / addedDistance;
611
+
612
+ int xduration = computeAxisDuration(dx, xvel, mCallback.getViewHorizontalDragRange(child));
613
+ int yduration = computeAxisDuration(dy, yvel, mCallback.getViewVerticalDragRange(child));
614
+
615
+ return (int) (xduration * xweight + yduration * yweight);
616
+ }
617
+
618
+ private int computeAxisDuration(int delta, int velocity, int motionRange) {
619
+ if (delta == 0) {
620
+ return 0;
621
+ }
622
+
623
+ final int width = mParentView.getWidth();
624
+ final int halfWidth = width / 2;
625
+ final float distanceRatio = Math.min(1f, (float) Math.abs(delta) / width);
626
+ final float distance = halfWidth + halfWidth *
627
+ distanceInfluenceForSnapDuration(distanceRatio);
628
+
629
+ int duration;
630
+ velocity = Math.abs(velocity);
631
+ if (velocity > 0) {
632
+ duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
633
+ } else {
634
+ final float range = (float) Math.abs(delta) / motionRange;
635
+ duration = (int) ((range + 1) * BASE_SETTLE_DURATION);
636
+ }
637
+ return Math.min(duration, MAX_SETTLE_DURATION);
638
+ }
639
+
640
+ /**
641
+ * Clamp the magnitude of value for absMin and absMax.
642
+ * If the value is below the minimum, it will be clamped to zero.
643
+ * If the value is above the maximum, it will be clamped to the maximum.
644
+ *
645
+ * @param value Value to clamp
646
+ * @param absMin Absolute value of the minimum significant value to return
647
+ * @param absMax Absolute value of the maximum value to return
648
+ * @return The clamped value with the same sign as <code>value</code>
649
+ */
650
+ private int clampMag(int value, int absMin, int absMax) {
651
+ final int absValue = Math.abs(value);
652
+ if (absValue < absMin) return 0;
653
+ if (absValue > absMax) return value > 0 ? absMax : -absMax;
654
+ return value;
655
+ }
656
+
657
+ /**
658
+ * Clamp the magnitude of value for absMin and absMax.
659
+ * If the value is below the minimum, it will be clamped to zero.
660
+ * If the value is above the maximum, it will be clamped to the maximum.
661
+ *
662
+ * @param value Value to clamp
663
+ * @param absMin Absolute value of the minimum significant value to return
664
+ * @param absMax Absolute value of the maximum value to return
665
+ * @return The clamped value with the same sign as <code>value</code>
666
+ */
667
+ private float clampMag(float value, float absMin, float absMax) {
668
+ final float absValue = Math.abs(value);
669
+ if (absValue < absMin) return 0;
670
+ if (absValue > absMax) return value > 0 ? absMax : -absMax;
671
+ return value;
672
+ }
673
+
674
+ private float distanceInfluenceForSnapDuration(float f) {
675
+ f -= 0.5f; // center the values about 0.
676
+ f *= 0.3f * Math.PI / 2.0f;
677
+ return (float) Math.sin(f);
678
+ }
679
+
680
+ /**
681
+ * Settle the captured view based on standard free-moving fling behavior.
682
+ * The caller should invoke {@link #continueSettling(boolean)} on each subsequent frame
683
+ * to continue the motion until it returns false.
684
+ *
685
+ * @param minLeft Minimum X position for the view's left edge
686
+ * @param minTop Minimum Y position for the view's top edge
687
+ * @param maxLeft Maximum X position for the view's left edge
688
+ * @param maxTop Maximum Y position for the view's top edge
689
+ */
690
+ public void flingCapturedView(int minLeft, int minTop, int maxLeft, int maxTop) {
691
+ if (!mReleaseInProgress) {
692
+ throw new IllegalStateException("Cannot flingCapturedView outside of a call to " +
693
+ "Callback#onViewReleased");
694
+ }
695
+
696
+ mScroller.fling(mCapturedView.getLeft(), mCapturedView.getTop(),
697
+ (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
698
+ (int) VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId),
699
+ minLeft, maxLeft, minTop, maxTop);
700
+
701
+ setDragState(STATE_SETTLING);
702
+ }
703
+
704
+ /**
705
+ * Move the captured settling view by the appropriate amount for the current time.
706
+ * If <code>continueSettling</code> returns true, the caller should call it again
707
+ * on the next frame to continue.
708
+ *
709
+ * @param deferCallbacks true if state callbacks should be deferred via posted message.
710
+ * Set this to true if you are calling this method from
711
+ * {@link View#computeScroll()} or similar methods
712
+ * invoked as part of layout or drawing.
713
+ * @return true if settle is still in progress
714
+ */
715
+ public boolean continueSettling(boolean deferCallbacks) {
716
+ // Make sure, there is a captured view
717
+ if (mCapturedView == null) {
718
+ return false;
719
+ }
720
+ if (mDragState == STATE_SETTLING) {
721
+ boolean keepGoing = mScroller.computeScrollOffset();
722
+ final int x = mScroller.getCurrX();
723
+ final int y = mScroller.getCurrY();
724
+ final int dx = x - mCapturedView.getLeft();
725
+ final int dy = y - mCapturedView.getTop();
726
+
727
+ if (dx != 0) {
728
+ mCapturedView.offsetLeftAndRight(dx);
729
+ }
730
+ if (dy != 0) {
731
+ mCapturedView.offsetTopAndBottom(dy);
732
+ }
733
+
734
+ if (dx != 0 || dy != 0) {
735
+ mCallback.onViewPositionChanged(mCapturedView, x, y, dx, dy);
736
+ }
737
+
738
+ if (keepGoing && x == mScroller.getFinalX() && y == mScroller.getFinalY()) {
739
+ // Close enough. The interpolator/scroller might think we're still moving
740
+ // but the user sure doesn't.
741
+ mScroller.abortAnimation();
742
+ keepGoing = mScroller.isFinished();
743
+ }
744
+
745
+ if (!keepGoing) {
746
+ if (deferCallbacks) {
747
+ mParentView.post(mSetIdleRunnable);
748
+ } else {
749
+ setDragState(STATE_IDLE);
750
+ }
751
+ }
752
+ }
753
+
754
+ return mDragState == STATE_SETTLING;
755
+ }
756
+
757
+ /**
758
+ * Like all callback events this must happen on the UI thread, but release
759
+ * involves some extra semantics. During a release (mReleaseInProgress)
760
+ * is the only time it is valid to call {@link #settleCapturedViewAt(int, int)}
761
+ * or {@link #flingCapturedView(int, int, int, int)}.
762
+ */
763
+ private void dispatchViewReleased(float xvel, float yvel) {
764
+ mReleaseInProgress = true;
765
+ mCallback.onViewReleased(mCapturedView, xvel, yvel);
766
+ mReleaseInProgress = false;
767
+
768
+ if (mDragState == STATE_DRAGGING) {
769
+ // onViewReleased didn't call a method that would have changed this. Go idle.
770
+ setDragState(STATE_IDLE);
771
+ }
772
+ }
773
+
774
+ private void clearMotionHistory() {
775
+ if (mInitialMotionX == null) {
776
+ return;
777
+ }
778
+ Arrays.fill(mInitialMotionX, 0);
779
+ Arrays.fill(mInitialMotionY, 0);
780
+ Arrays.fill(mLastMotionX, 0);
781
+ Arrays.fill(mLastMotionY, 0);
782
+ Arrays.fill(mInitialEdgesTouched, 0);
783
+ Arrays.fill(mEdgeDragsInProgress, 0);
784
+ Arrays.fill(mEdgeDragsLocked, 0);
785
+ mPointersDown = 0;
786
+ }
787
+
788
+ private void clearMotionHistory(int pointerId) {
789
+ if (mInitialMotionX == null) {
790
+ return;
791
+ }
792
+ mInitialMotionX[pointerId] = 0;
793
+ mInitialMotionY[pointerId] = 0;
794
+ mLastMotionX[pointerId] = 0;
795
+ mLastMotionY[pointerId] = 0;
796
+ mInitialEdgesTouched[pointerId] = 0;
797
+ mEdgeDragsInProgress[pointerId] = 0;
798
+ mEdgeDragsLocked[pointerId] = 0;
799
+ mPointersDown &= ~(1 << pointerId);
800
+ }
801
+
802
+ private void ensureMotionHistorySizeForId(int pointerId) {
803
+ if (mInitialMotionX == null || mInitialMotionX.length <= pointerId) {
804
+ float[] imx = new float[pointerId + 1];
805
+ float[] imy = new float[pointerId + 1];
806
+ float[] lmx = new float[pointerId + 1];
807
+ float[] lmy = new float[pointerId + 1];
808
+ int[] iit = new int[pointerId + 1];
809
+ int[] edip = new int[pointerId + 1];
810
+ int[] edl = new int[pointerId + 1];
811
+
812
+ if (mInitialMotionX != null) {
813
+ System.arraycopy(mInitialMotionX, 0, imx, 0, mInitialMotionX.length);
814
+ System.arraycopy(mInitialMotionY, 0, imy, 0, mInitialMotionY.length);
815
+ System.arraycopy(mLastMotionX, 0, lmx, 0, mLastMotionX.length);
816
+ System.arraycopy(mLastMotionY, 0, lmy, 0, mLastMotionY.length);
817
+ System.arraycopy(mInitialEdgesTouched, 0, iit, 0, mInitialEdgesTouched.length);
818
+ System.arraycopy(mEdgeDragsInProgress, 0, edip, 0, mEdgeDragsInProgress.length);
819
+ System.arraycopy(mEdgeDragsLocked, 0, edl, 0, mEdgeDragsLocked.length);
820
+ }
821
+
822
+ mInitialMotionX = imx;
823
+ mInitialMotionY = imy;
824
+ mLastMotionX = lmx;
825
+ mLastMotionY = lmy;
826
+ mInitialEdgesTouched = iit;
827
+ mEdgeDragsInProgress = edip;
828
+ mEdgeDragsLocked = edl;
829
+ }
830
+ }
831
+
832
+ private void saveInitialMotion(float x, float y, int pointerId) {
833
+ ensureMotionHistorySizeForId(pointerId);
834
+ mInitialMotionX[pointerId] = mLastMotionX[pointerId] = x;
835
+ mInitialMotionY[pointerId] = mLastMotionY[pointerId] = y;
836
+ mInitialEdgesTouched[pointerId] = getEdgesTouched((int) x, (int) y);
837
+ mPointersDown |= 1 << pointerId;
838
+ }
839
+
840
+ private void saveLastMotion(MotionEvent ev) {
841
+ final int pointerCount = MotionEventCompat.getPointerCount(ev);
842
+ for (int i = 0; i < pointerCount; i++) {
843
+ final int pointerId = MotionEventCompat.getPointerId(ev, i);
844
+ final float x = MotionEventCompat.getX(ev, i);
845
+ final float y = MotionEventCompat.getY(ev, i);
846
+ // Sometimes we can try and save last motion for a pointer never recorded in initial motion. In this case we just discard it.
847
+ if (mLastMotionX != null && mLastMotionY != null
848
+ && mLastMotionX.length > pointerId && mLastMotionY.length > pointerId) {
849
+ mLastMotionX[pointerId] = x;
850
+ mLastMotionY[pointerId] = y;
851
+ }
852
+ }
853
+ }
854
+
855
+ /**
856
+ * Check if the given pointer ID represents a pointer that is currently down (to the best
857
+ * of the ViewDragHelper's knowledge).
858
+ *
859
+ * <p>The state used to report this information is populated by the methods
860
+ * {@link #shouldInterceptTouchEvent(MotionEvent)} or
861
+ * {@link #processTouchEvent(MotionEvent)}. If one of these methods has not
862
+ * been called for all relevant MotionEvents to track, the information reported
863
+ * by this method may be stale or incorrect.</p>
864
+ *
865
+ * @param pointerId pointer ID to check; corresponds to IDs provided by MotionEvent
866
+ * @return true if the pointer with the given ID is still down
867
+ */
868
+ public boolean isPointerDown(int pointerId) {
869
+ return (mPointersDown & 1 << pointerId) != 0;
870
+ }
871
+
872
+ void setDragState(int state) {
873
+ if (mDragState != state) {
874
+ mDragState = state;
875
+ mCallback.onViewDragStateChanged(state);
876
+ if (mDragState == STATE_IDLE) {
877
+ mCapturedView = null;
878
+ }
879
+ }
880
+ }
881
+
882
+ /**
883
+ * Attempt to capture the view with the given pointer ID. The callback will be involved.
884
+ * This will put us into the "dragging" state. If we've already captured this view with
885
+ * this pointer this method will immediately return true without consulting the callback.
886
+ *
887
+ * @param toCapture View to capture
888
+ * @param pointerId Pointer to capture with
889
+ * @return true if capture was successful
890
+ */
891
+ boolean tryCaptureViewForDrag(View toCapture, int pointerId) {
892
+ if (toCapture == mCapturedView && mActivePointerId == pointerId) {
893
+ // Already done!
894
+ return true;
895
+ }
896
+ if (toCapture != null && mCallback.tryCaptureView(toCapture, pointerId)) {
897
+ mActivePointerId = pointerId;
898
+ captureChildView(toCapture, pointerId);
899
+ return true;
900
+ }
901
+ return false;
902
+ }
903
+
904
+ /**
905
+ * Tests scrollability within child views of v given a delta of dx.
906
+ *
907
+ * @param v View to test for horizontal scrollability
908
+ * @param checkV Whether the view v passed should itself be checked for scrollability (true),
909
+ * or just its children (false).
910
+ * @param dx Delta scrolled in pixels along the X axis
911
+ * @param dy Delta scrolled in pixels along the Y axis
912
+ * @param x X coordinate of the active touch point
913
+ * @param y Y coordinate of the active touch point
914
+ * @return true if child views of v can be scrolled by delta of dx.
915
+ */
916
+ protected boolean canScroll(View v, boolean checkV, int dx, int dy, int x, int y) {
917
+ if (v instanceof ViewGroup) {
918
+ final ViewGroup group = (ViewGroup) v;
919
+ final int scrollX = v.getScrollX();
920
+ final int scrollY = v.getScrollY();
921
+ final int count = group.getChildCount();
922
+ // Count backwards - let topmost views consume scroll distance first.
923
+ for (int i = count - 1; i >= 0; i--) {
924
+ // TODO: Add versioned support here for transformed views.
925
+ // This will not work for transformed views in Honeycomb+
926
+ final View child = group.getChildAt(i);
927
+ if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
928
+ y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&
929
+ canScroll(child, true, dx, dy, x + scrollX - child.getLeft(),
930
+ y + scrollY - child.getTop())) {
931
+ return true;
932
+ }
933
+ }
934
+ }
935
+
936
+ return checkV && (ViewCompat.canScrollHorizontally(v, -dx) ||
937
+ ViewCompat.canScrollVertically(v, -dy));
938
+ }
939
+
940
+ /**
941
+ * Check if this event as provided to the parent view's onInterceptTouchEvent should
942
+ * cause the parent to intercept the touch event stream.
943
+ *
944
+ * @param ev MotionEvent provided to onInterceptTouchEvent
945
+ * @return true if the parent view should return true from onInterceptTouchEvent
946
+ */
947
+ public boolean shouldInterceptTouchEvent(MotionEvent ev) {
948
+ final int action = MotionEventCompat.getActionMasked(ev);
949
+ final int actionIndex = MotionEventCompat.getActionIndex(ev);
950
+
951
+ if (action == MotionEvent.ACTION_DOWN) {
952
+ // Reset things for a new event stream, just in case we didn't get
953
+ // the whole previous stream.
954
+ cancel();
955
+ }
956
+
957
+ if (mVelocityTracker == null) {
958
+ mVelocityTracker = VelocityTracker.obtain();
959
+ }
960
+ mVelocityTracker.addMovement(ev);
961
+
962
+ switch (action) {
963
+ case MotionEvent.ACTION_DOWN: {
964
+ final float x = ev.getX();
965
+ final float y = ev.getY();
966
+ final int pointerId = MotionEventCompat.getPointerId(ev, 0);
967
+ saveInitialMotion(x, y, pointerId);
968
+
969
+ final View toCapture = findTopChildUnder((int) x, (int) y);
970
+
971
+ // Catch a settling view if possible.
972
+ if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
973
+ tryCaptureViewForDrag(toCapture, pointerId);
974
+ }
975
+
976
+ final int edgesTouched = mInitialEdgesTouched[pointerId];
977
+ if ((edgesTouched & mTrackingEdges) != 0) {
978
+ mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
979
+ }
980
+ break;
981
+ }
982
+
983
+ case MotionEventCompat.ACTION_POINTER_DOWN: {
984
+ final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
985
+ final float x = MotionEventCompat.getX(ev, actionIndex);
986
+ final float y = MotionEventCompat.getY(ev, actionIndex);
987
+
988
+ saveInitialMotion(x, y, pointerId);
989
+
990
+ // A ViewDragHelper can only manipulate one view at a time.
991
+ if (mDragState == STATE_IDLE) {
992
+ final int edgesTouched = mInitialEdgesTouched[pointerId];
993
+ if ((edgesTouched & mTrackingEdges) != 0) {
994
+ mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
995
+ }
996
+ } else if (mDragState == STATE_SETTLING) {
997
+ // Catch a settling view if possible.
998
+ final View toCapture = findTopChildUnder((int) x, (int) y);
999
+ if (toCapture == mCapturedView) {
1000
+ tryCaptureViewForDrag(toCapture, pointerId);
1001
+ }
1002
+ }
1003
+ break;
1004
+ }
1005
+
1006
+ case MotionEvent.ACTION_MOVE: {
1007
+ // First to cross a touch slop over a draggable view wins. Also report edge drags.
1008
+ final int pointerCount = MotionEventCompat.getPointerCount(ev);
1009
+ for (int i = 0; i < pointerCount && mInitialMotionX != null && mInitialMotionY != null; i++) {
1010
+ final int pointerId = MotionEventCompat.getPointerId(ev, i);
1011
+ if (pointerId >= mInitialMotionX.length || pointerId >= mInitialMotionY.length) {
1012
+ continue;
1013
+ }
1014
+ final float x = MotionEventCompat.getX(ev, i);
1015
+ final float y = MotionEventCompat.getY(ev, i);
1016
+ final float dx = x - mInitialMotionX[pointerId];
1017
+ final float dy = y - mInitialMotionY[pointerId];
1018
+
1019
+ reportNewEdgeDrags(dx, dy, pointerId);
1020
+ if (mDragState == STATE_DRAGGING) {
1021
+ // Callback might have started an edge drag
1022
+ break;
1023
+ }
1024
+
1025
+ final View toCapture = findTopChildUnder((int)mInitialMotionX[pointerId], (int)mInitialMotionY[pointerId]);
1026
+ if (toCapture != null && checkTouchSlop(toCapture, dx, dy) &&
1027
+ tryCaptureViewForDrag(toCapture, pointerId)) {
1028
+ break;
1029
+ }
1030
+ }
1031
+ saveLastMotion(ev);
1032
+ break;
1033
+ }
1034
+
1035
+ case MotionEventCompat.ACTION_POINTER_UP: {
1036
+ final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
1037
+ clearMotionHistory(pointerId);
1038
+ break;
1039
+ }
1040
+
1041
+ case MotionEvent.ACTION_UP:
1042
+ case MotionEvent.ACTION_CANCEL: {
1043
+ cancel();
1044
+ break;
1045
+ }
1046
+ }
1047
+
1048
+ return mDragState == STATE_DRAGGING;
1049
+ }
1050
+
1051
+ /**
1052
+ * Process a touch event received by the parent view. This method will dispatch callback events
1053
+ * as needed before returning. The parent view's onTouchEvent implementation should call this.
1054
+ *
1055
+ * @param ev The touch event received by the parent view
1056
+ */
1057
+ public void processTouchEvent(MotionEvent ev) {
1058
+ final int action = MotionEventCompat.getActionMasked(ev);
1059
+ final int actionIndex = MotionEventCompat.getActionIndex(ev);
1060
+
1061
+ if (action == MotionEvent.ACTION_DOWN) {
1062
+ // Reset things for a new event stream, just in case we didn't get
1063
+ // the whole previous stream.
1064
+ cancel();
1065
+ }
1066
+
1067
+ if (mVelocityTracker == null) {
1068
+ mVelocityTracker = VelocityTracker.obtain();
1069
+ }
1070
+ mVelocityTracker.addMovement(ev);
1071
+
1072
+ switch (action) {
1073
+ case MotionEvent.ACTION_DOWN: {
1074
+ final float x = ev.getX();
1075
+ final float y = ev.getY();
1076
+ final int pointerId = MotionEventCompat.getPointerId(ev, 0);
1077
+ final View toCapture = findTopChildUnder((int) x, (int) y);
1078
+
1079
+ saveInitialMotion(x, y, pointerId);
1080
+
1081
+ // Since the parent is already directly processing this touch event,
1082
+ // there is no reason to delay for a slop before dragging.
1083
+ // Start immediately if possible.
1084
+ tryCaptureViewForDrag(toCapture, pointerId);
1085
+
1086
+ final int edgesTouched = mInitialEdgesTouched[pointerId];
1087
+ if ((edgesTouched & mTrackingEdges) != 0) {
1088
+ mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
1089
+ }
1090
+ break;
1091
+ }
1092
+
1093
+ case MotionEventCompat.ACTION_POINTER_DOWN: {
1094
+ final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
1095
+ final float x = MotionEventCompat.getX(ev, actionIndex);
1096
+ final float y = MotionEventCompat.getY(ev, actionIndex);
1097
+
1098
+ saveInitialMotion(x, y, pointerId);
1099
+
1100
+ // A ViewDragHelper can only manipulate one view at a time.
1101
+ if (mDragState == STATE_IDLE) {
1102
+ // If we're idle we can do anything! Treat it like a normal down event.
1103
+
1104
+ final View toCapture = findTopChildUnder((int) x, (int) y);
1105
+ tryCaptureViewForDrag(toCapture, pointerId);
1106
+
1107
+ final int edgesTouched = mInitialEdgesTouched[pointerId];
1108
+ if ((edgesTouched & mTrackingEdges) != 0) {
1109
+ mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
1110
+ }
1111
+ } else if (isCapturedViewUnder((int) x, (int) y)) {
1112
+ // We're still tracking a captured view. If the same view is under this
1113
+ // point, we'll swap to controlling it with this pointer instead.
1114
+ // (This will still work if we're "catching" a settling view.)
1115
+
1116
+ tryCaptureViewForDrag(mCapturedView, pointerId);
1117
+ }
1118
+ break;
1119
+ }
1120
+
1121
+ case MotionEvent.ACTION_MOVE: {
1122
+ if (mDragState == STATE_DRAGGING) {
1123
+ final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
1124
+ final float x = MotionEventCompat.getX(ev, index);
1125
+ final float y = MotionEventCompat.getY(ev, index);
1126
+ final int idx = (int) (x - mLastMotionX[mActivePointerId]);
1127
+ final int idy = (int) (y - mLastMotionY[mActivePointerId]);
1128
+
1129
+ dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);
1130
+
1131
+ saveLastMotion(ev);
1132
+ } else {
1133
+ // Check to see if any pointer is now over a draggable view.
1134
+ final int pointerCount = MotionEventCompat.getPointerCount(ev);
1135
+ for (int i = 0; i < pointerCount; i++) {
1136
+ final int pointerId = MotionEventCompat.getPointerId(ev, i)
1137
+ ;
1138
+ final float x = MotionEventCompat.getX(ev, i);
1139
+ final float y = MotionEventCompat.getY(ev, i);
1140
+ final float dx = x - mInitialMotionX[pointerId];
1141
+ final float dy = y - mInitialMotionY[pointerId];
1142
+
1143
+ reportNewEdgeDrags(dx, dy, pointerId);
1144
+ if (mDragState == STATE_DRAGGING) {
1145
+ // Callback might have started an edge drag.
1146
+ break;
1147
+ }
1148
+
1149
+ final View toCapture = findTopChildUnder((int) mInitialMotionX[pointerId], (int) mInitialMotionY[pointerId]);
1150
+ if (checkTouchSlop(toCapture, dx, dy) &&
1151
+ tryCaptureViewForDrag(toCapture, pointerId)) {
1152
+ break;
1153
+ }
1154
+ }
1155
+ saveLastMotion(ev);
1156
+ }
1157
+ break;
1158
+ }
1159
+
1160
+ case MotionEventCompat.ACTION_POINTER_UP: {
1161
+ final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
1162
+ if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) {
1163
+ // Try to find another pointer that's still holding on to the captured view.
1164
+ int newActivePointer = INVALID_POINTER;
1165
+ final int pointerCount = MotionEventCompat.getPointerCount(ev);
1166
+ for (int i = 0; i < pointerCount; i++) {
1167
+ final int id = MotionEventCompat.getPointerId(ev, i);
1168
+ if (id == mActivePointerId) {
1169
+ // This one's going away, skip.
1170
+ continue;
1171
+ }
1172
+
1173
+ final float x = MotionEventCompat.getX(ev, i);
1174
+ final float y = MotionEventCompat.getY(ev, i);
1175
+ if (findTopChildUnder((int) x, (int) y) == mCapturedView &&
1176
+ tryCaptureViewForDrag(mCapturedView, id)) {
1177
+ newActivePointer = mActivePointerId;
1178
+ break;
1179
+ }
1180
+ }
1181
+
1182
+ if (newActivePointer == INVALID_POINTER) {
1183
+ // We didn't find another pointer still touching the view, release it.
1184
+ releaseViewForPointerUp();
1185
+ }
1186
+ }
1187
+ clearMotionHistory(pointerId);
1188
+ break;
1189
+ }
1190
+
1191
+ case MotionEvent.ACTION_UP: {
1192
+ if (mDragState == STATE_DRAGGING) {
1193
+ releaseViewForPointerUp();
1194
+ }
1195
+ cancel();
1196
+ break;
1197
+ }
1198
+
1199
+ case MotionEvent.ACTION_CANCEL: {
1200
+ if (mDragState == STATE_DRAGGING) {
1201
+ dispatchViewReleased(0, 0);
1202
+ }
1203
+ cancel();
1204
+ break;
1205
+ }
1206
+ }
1207
+ }
1208
+
1209
+ private void reportNewEdgeDrags(float dx, float dy, int pointerId) {
1210
+ int dragsStarted = 0;
1211
+ if (checkNewEdgeDrag(dx, dy, pointerId, EDGE_LEFT)) {
1212
+ dragsStarted |= EDGE_LEFT;
1213
+ }
1214
+ if (checkNewEdgeDrag(dy, dx, pointerId, EDGE_TOP)) {
1215
+ dragsStarted |= EDGE_TOP;
1216
+ }
1217
+ if (checkNewEdgeDrag(dx, dy, pointerId, EDGE_RIGHT)) {
1218
+ dragsStarted |= EDGE_RIGHT;
1219
+ }
1220
+ if (checkNewEdgeDrag(dy, dx, pointerId, EDGE_BOTTOM)) {
1221
+ dragsStarted |= EDGE_BOTTOM;
1222
+ }
1223
+
1224
+ if (dragsStarted != 0) {
1225
+ mEdgeDragsInProgress[pointerId] |= dragsStarted;
1226
+ mCallback.onEdgeDragStarted(dragsStarted, pointerId);
1227
+ }
1228
+ }
1229
+
1230
+ private boolean checkNewEdgeDrag(float delta, float odelta, int pointerId, int edge) {
1231
+ final float absDelta = Math.abs(delta);
1232
+ final float absODelta = Math.abs(odelta);
1233
+
1234
+ if ((mInitialEdgesTouched[pointerId] & edge) != edge || (mTrackingEdges & edge) == 0 ||
1235
+ (mEdgeDragsLocked[pointerId] & edge) == edge ||
1236
+ (mEdgeDragsInProgress[pointerId] & edge) == edge ||
1237
+ (absDelta <= mTouchSlop && absODelta <= mTouchSlop)) {
1238
+ return false;
1239
+ }
1240
+ if (absDelta < absODelta * 0.5f && mCallback.onEdgeLock(edge)) {
1241
+ mEdgeDragsLocked[pointerId] |= edge;
1242
+ return false;
1243
+ }
1244
+ return (mEdgeDragsInProgress[pointerId] & edge) == 0 && absDelta > mTouchSlop;
1245
+ }
1246
+
1247
+ /**
1248
+ * Check if we've crossed a reasonable touch slop for the given child view.
1249
+ * If the child cannot be dragged along the horizontal or vertical axis, motion
1250
+ * along that axis will not count toward the slop check.
1251
+ *
1252
+ * @param child Child to check
1253
+ * @param dx Motion since initial position along X axis
1254
+ * @param dy Motion since initial position along Y axis
1255
+ * @return true if the touch slop has been crossed
1256
+ */
1257
+ private boolean checkTouchSlop(View child, float dx, float dy) {
1258
+ if (child == null) {
1259
+ return false;
1260
+ }
1261
+ final boolean checkHorizontal = mCallback.getViewHorizontalDragRange(child) > 0;
1262
+ final boolean checkVertical = mCallback.getViewVerticalDragRange(child) > 0;
1263
+
1264
+ if (checkHorizontal && checkVertical) {
1265
+ return dx * dx + dy * dy > mTouchSlop * mTouchSlop;
1266
+ } else if (checkHorizontal) {
1267
+ return Math.abs(dx) > mTouchSlop;
1268
+ } else if (checkVertical) {
1269
+ return Math.abs(dy) > mTouchSlop;
1270
+ }
1271
+ return false;
1272
+ }
1273
+
1274
+ /**
1275
+ * Check if any pointer tracked in the current gesture has crossed
1276
+ * the required slop threshold.
1277
+ *
1278
+ * <p>This depends on internal state populated by
1279
+ * {@link #shouldInterceptTouchEvent(MotionEvent)} or
1280
+ * {@link #processTouchEvent(MotionEvent)}. You should only rely on
1281
+ * the results of this method after all currently available touch data
1282
+ * has been provided to one of these two methods.</p>
1283
+ *
1284
+ * @param directions Combination of direction flags, see {@link #DIRECTION_HORIZONTAL},
1285
+ * {@link #DIRECTION_VERTICAL}, {@link #DIRECTION_ALL}
1286
+ * @return true if the slop threshold has been crossed, false otherwise
1287
+ */
1288
+ public boolean checkTouchSlop(int directions) {
1289
+ final int count = mInitialMotionX.length;
1290
+ for (int i = 0; i < count; i++) {
1291
+ if (checkTouchSlop(directions, i)) {
1292
+ return true;
1293
+ }
1294
+ }
1295
+ return false;
1296
+ }
1297
+
1298
+ /**
1299
+ * Check if the specified pointer tracked in the current gesture has crossed
1300
+ * the required slop threshold.
1301
+ *
1302
+ * <p>This depends on internal state populated by
1303
+ * {@link #shouldInterceptTouchEvent(MotionEvent)} or
1304
+ * {@link #processTouchEvent(MotionEvent)}. You should only rely on
1305
+ * the results of this method after all currently available touch data
1306
+ * has been provided to one of these two methods.</p>
1307
+ *
1308
+ * @param directions Combination of direction flags, see {@link #DIRECTION_HORIZONTAL},
1309
+ * {@link #DIRECTION_VERTICAL}, {@link #DIRECTION_ALL}
1310
+ * @param pointerId ID of the pointer to slop check as specified by MotionEvent
1311
+ * @return true if the slop threshold has been crossed, false otherwise
1312
+ */
1313
+ public boolean checkTouchSlop(int directions, int pointerId) {
1314
+ if (!isPointerDown(pointerId)) {
1315
+ return false;
1316
+ }
1317
+
1318
+ final boolean checkHorizontal = (directions & DIRECTION_HORIZONTAL) == DIRECTION_HORIZONTAL;
1319
+ final boolean checkVertical = (directions & DIRECTION_VERTICAL) == DIRECTION_VERTICAL;
1320
+
1321
+ final float dx = mLastMotionX[pointerId] - mInitialMotionX[pointerId];
1322
+ final float dy = mLastMotionY[pointerId] - mInitialMotionY[pointerId];
1323
+
1324
+ if (checkHorizontal && checkVertical) {
1325
+ return dx * dx + dy * dy > mTouchSlop * mTouchSlop;
1326
+ } else if (checkHorizontal) {
1327
+ return Math.abs(dx) > mTouchSlop;
1328
+ } else if (checkVertical) {
1329
+ return Math.abs(dy) > mTouchSlop;
1330
+ }
1331
+ return false;
1332
+ }
1333
+
1334
+ /**
1335
+ * Check if any of the edges specified were initially touched in the currently active gesture.
1336
+ * If there is no currently active gesture this method will return false.
1337
+ *
1338
+ * @param edges Edges to check for an initial edge touch. See {@link #EDGE_LEFT},
1339
+ * {@link #EDGE_TOP}, {@link #EDGE_RIGHT}, {@link #EDGE_BOTTOM} and
1340
+ * {@link #EDGE_ALL}
1341
+ * @return true if any of the edges specified were initially touched in the current gesture
1342
+ */
1343
+ public boolean isEdgeTouched(int edges) {
1344
+ final int count = mInitialEdgesTouched.length;
1345
+ for (int i = 0; i < count; i++) {
1346
+ if (isEdgeTouched(edges, i)) {
1347
+ return true;
1348
+ }
1349
+ }
1350
+ return false;
1351
+ }
1352
+
1353
+ /**
1354
+ * Check if any of the edges specified were initially touched by the pointer with
1355
+ * the specified ID. If there is no currently active gesture or if there is no pointer with
1356
+ * the given ID currently down this method will return false.
1357
+ *
1358
+ * @param edges Edges to check for an initial edge touch. See {@link #EDGE_LEFT},
1359
+ * {@link #EDGE_TOP}, {@link #EDGE_RIGHT}, {@link #EDGE_BOTTOM} and
1360
+ * {@link #EDGE_ALL}
1361
+ * @return true if any of the edges specified were initially touched in the current gesture
1362
+ */
1363
+ public boolean isEdgeTouched(int edges, int pointerId) {
1364
+ return isPointerDown(pointerId) && (mInitialEdgesTouched[pointerId] & edges) != 0;
1365
+ }
1366
+
1367
+ public boolean isDragging() {
1368
+ return mDragState == STATE_DRAGGING;
1369
+ }
1370
+
1371
+ private void releaseViewForPointerUp() {
1372
+ mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
1373
+ final float xvel = clampMag(
1374
+ VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
1375
+ mMinVelocity, mMaxVelocity);
1376
+ final float yvel = clampMag(
1377
+ VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId),
1378
+ mMinVelocity, mMaxVelocity);
1379
+ dispatchViewReleased(xvel, yvel);
1380
+ }
1381
+
1382
+ private void dragTo(int left, int top, int dx, int dy) {
1383
+ int clampedX = left;
1384
+ int clampedY = top;
1385
+ final int oldLeft = mCapturedView.getLeft();
1386
+ final int oldTop = mCapturedView.getTop();
1387
+ if (dx != 0) {
1388
+ clampedX = mCallback.clampViewPositionHorizontal(mCapturedView, left, dx);
1389
+ mCapturedView.offsetLeftAndRight(clampedX - oldLeft);
1390
+ }
1391
+ if (dy != 0) {
1392
+ clampedY = mCallback.clampViewPositionVertical(mCapturedView, top, dy);
1393
+ mCapturedView.offsetTopAndBottom(clampedY - oldTop);
1394
+ }
1395
+
1396
+ if (dx != 0 || dy != 0) {
1397
+ final int clampedDx = clampedX - oldLeft;
1398
+ final int clampedDy = clampedY - oldTop;
1399
+ mCallback.onViewPositionChanged(mCapturedView, clampedX, clampedY,
1400
+ clampedDx, clampedDy);
1401
+ }
1402
+ }
1403
+
1404
+ /**
1405
+ * Determine if the currently captured view is under the given point in the
1406
+ * parent view's coordinate system. If there is no captured view this method
1407
+ * will return false.
1408
+ *
1409
+ * @param x X position to test in the parent's coordinate system
1410
+ * @param y Y position to test in the parent's coordinate system
1411
+ * @return true if the captured view is under the given point, false otherwise
1412
+ */
1413
+ public boolean isCapturedViewUnder(int x, int y) {
1414
+ return isViewUnder(mCapturedView, x, y);
1415
+ }
1416
+
1417
+ /**
1418
+ * Determine if the supplied view is under the given point in the
1419
+ * parent view's coordinate system.
1420
+ *
1421
+ * @param view Child view of the parent to hit test
1422
+ * @param x X position to test in the parent's coordinate system
1423
+ * @param y Y position to test in the parent's coordinate system
1424
+ * @return true if the supplied view is under the given point, false otherwise
1425
+ */
1426
+ public boolean isViewUnder(View view, int x, int y) {
1427
+ if (view == null) {
1428
+ return false;
1429
+ }
1430
+ return x >= view.getLeft() &&
1431
+ x < view.getRight() &&
1432
+ y >= view.getTop() &&
1433
+ y < view.getBottom();
1434
+ }
1435
+
1436
+ /**
1437
+ * Find the topmost child under the given point within the parent view's coordinate system.
1438
+ * The child order is determined using {@link Callback#getOrderedChildIndex(int)}.
1439
+ *
1440
+ * @param x X position to test in the parent's coordinate system
1441
+ * @param y Y position to test in the parent's coordinate system
1442
+ * @return The topmost child view under (x, y) or null if none found.
1443
+ */
1444
+ public View findTopChildUnder(int x, int y) {
1445
+ final int childCount = mParentView.getChildCount();
1446
+ for (int i = childCount - 1; i >= 0; i--) {
1447
+ final View child = mParentView.getChildAt(mCallback.getOrderedChildIndex(i));
1448
+ if (x >= child.getLeft() && x < child.getRight() &&
1449
+ y >= child.getTop() && y < child.getBottom()) {
1450
+ return child;
1451
+ }
1452
+ }
1453
+ return null;
1454
+ }
1455
+
1456
+ private int getEdgesTouched(int x, int y) {
1457
+ int result = 0;
1458
+
1459
+ if (x < mParentView.getLeft() + mEdgeSize) result |= EDGE_LEFT;
1460
+ if (y < mParentView.getTop() + mEdgeSize) result |= EDGE_TOP;
1461
+ if (x > mParentView.getRight() - mEdgeSize) result |= EDGE_RIGHT;
1462
+ if (y > mParentView.getBottom() - mEdgeSize) result |= EDGE_BOTTOM;
1463
+
1464
+ return result;
1465
+ }
1466
+ }