Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 1 | /* |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 2 | * Copyright (C) 2009 The Android Open Source Project |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 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 | |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 17 | package android.gesture; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 20 | import android.content.res.TypedArray; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 21 | import android.graphics.Canvas; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 22 | import android.graphics.Paint; |
| 23 | import android.graphics.Path; |
| 24 | import android.graphics.Rect; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 25 | import android.graphics.RectF; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 26 | import android.util.AttributeSet; |
| 27 | import android.view.MotionEvent; |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 28 | import android.view.animation.AnimationUtils; |
| 29 | import android.view.animation.AccelerateDecelerateInterpolator; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 30 | import android.widget.FrameLayout; |
| 31 | import android.os.SystemClock; |
Romain Guy | 51099c5 | 2009-09-11 14:29:15 -0700 | [diff] [blame] | 32 | import android.annotation.Widget; |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 33 | import com.android.internal.R; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 34 | |
| 35 | import java.util.ArrayList; |
| 36 | |
| 37 | /** |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 38 | * A transparent overlay for gesture input that can be placed on top of other |
| 39 | * widgets or contain other widgets. |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 40 | * |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 41 | * @attr ref android.R.styleable#GestureOverlayView_eventsInterceptionEnabled |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 42 | * @attr ref android.R.styleable#GestureOverlayView_fadeDuration |
| 43 | * @attr ref android.R.styleable#GestureOverlayView_fadeOffset |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 44 | * @attr ref android.R.styleable#GestureOverlayView_fadeEnabled |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 45 | * @attr ref android.R.styleable#GestureOverlayView_gestureStrokeWidth |
| 46 | * @attr ref android.R.styleable#GestureOverlayView_gestureStrokeAngleThreshold |
| 47 | * @attr ref android.R.styleable#GestureOverlayView_gestureStrokeLengthThreshold |
| 48 | * @attr ref android.R.styleable#GestureOverlayView_gestureStrokeSquarenessThreshold |
Romain Guy | 8d78756 | 2009-05-29 15:02:55 -0700 | [diff] [blame] | 49 | * @attr ref android.R.styleable#GestureOverlayView_gestureStrokeType |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 50 | * @attr ref android.R.styleable#GestureOverlayView_gestureColor |
Romain Guy | 8d78756 | 2009-05-29 15:02:55 -0700 | [diff] [blame] | 51 | * @attr ref android.R.styleable#GestureOverlayView_orientation |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 52 | * @attr ref android.R.styleable#GestureOverlayView_uncertainGestureColor |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 53 | */ |
Romain Guy | 51099c5 | 2009-09-11 14:29:15 -0700 | [diff] [blame] | 54 | @Widget |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 55 | public class GestureOverlayView extends FrameLayout { |
| 56 | public static final int GESTURE_STROKE_TYPE_SINGLE = 0; |
| 57 | public static final int GESTURE_STROKE_TYPE_MULTIPLE = 1; |
| 58 | |
Romain Guy | e7c36dd | 2009-05-25 13:51:00 -0700 | [diff] [blame] | 59 | public static final int ORIENTATION_HORIZONTAL = 0; |
| 60 | public static final int ORIENTATION_VERTICAL = 1; |
| 61 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 62 | private static final int FADE_ANIMATION_RATE = 16; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 63 | private static final boolean GESTURE_RENDERING_ANTIALIAS = true; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 64 | private static final boolean DITHER_FLAG = true; |
| 65 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 66 | private final Paint mGesturePaint = new Paint(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 67 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 68 | private long mFadeDuration = 150; |
| 69 | private long mFadeOffset = 420; |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 70 | private long mFadingStart; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 71 | private boolean mFadingHasStarted; |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 72 | private boolean mFadeEnabled = true; |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 73 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 74 | private int mCurrentColor; |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 75 | private int mCertainGestureColor = 0xFFFFFF00; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 76 | private int mUncertainGestureColor = 0x48FFFF00; |
| 77 | private float mGestureStrokeWidth = 12.0f; |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 78 | private int mInvalidateExtraBorder = 10; |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 79 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 80 | private int mGestureStrokeType = GESTURE_STROKE_TYPE_SINGLE; |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 81 | private float mGestureStrokeLengthThreshold = 50.0f; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 82 | private float mGestureStrokeSquarenessTreshold = 0.275f; |
| 83 | private float mGestureStrokeAngleThreshold = 40.0f; |
| 84 | |
Romain Guy | e7c36dd | 2009-05-25 13:51:00 -0700 | [diff] [blame] | 85 | private int mOrientation = ORIENTATION_VERTICAL; |
| 86 | |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 87 | private final Rect mInvalidRect = new Rect(); |
| 88 | private final Path mPath = new Path(); |
Romain Guy | e705f2c | 2009-06-16 17:07:21 -0700 | [diff] [blame] | 89 | private boolean mGestureVisible = true; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 90 | |
| 91 | private float mX; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 92 | private float mY; |
Romain Guy | 8d78756 | 2009-05-29 15:02:55 -0700 | [diff] [blame] | 93 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 94 | private float mCurveEndX; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 95 | private float mCurveEndY; |
| 96 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 97 | private float mTotalLength; |
| 98 | private boolean mIsGesturing = false; |
Romain Guy | 834f039 | 2009-06-05 11:24:09 -0700 | [diff] [blame] | 99 | private boolean mPreviousWasGesturing = false; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 100 | private boolean mInterceptEvents = true; |
| 101 | private boolean mIsListeningForGestures; |
Romain Guy | 73d2589 | 2009-06-09 03:18:10 -0700 | [diff] [blame] | 102 | private boolean mResetGesture; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 103 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 104 | // current gesture |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 105 | private Gesture mCurrentGesture; |
| 106 | private final ArrayList<GesturePoint> mStrokeBuffer = new ArrayList<GesturePoint>(100); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 107 | |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 108 | // TODO: Make this a list of WeakReferences |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 109 | private final ArrayList<OnGestureListener> mOnGestureListeners = |
| 110 | new ArrayList<OnGestureListener>(); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 111 | // TODO: Make this a list of WeakReferences |
| 112 | private final ArrayList<OnGesturePerformedListener> mOnGesturePerformedListeners = |
| 113 | new ArrayList<OnGesturePerformedListener>(); |
Romain Guy | 9af0b4f | 2009-06-02 21:56:27 -0700 | [diff] [blame] | 114 | // TODO: Make this a list of WeakReferences |
| 115 | private final ArrayList<OnGesturingListener> mOnGesturingListeners = |
| 116 | new ArrayList<OnGesturingListener>(); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 117 | |
| 118 | private boolean mHandleGestureActions; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 119 | |
| 120 | // fading out effect |
| 121 | private boolean mIsFadingOut = false; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 122 | private float mFadingAlpha = 1.0f; |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 123 | private final AccelerateDecelerateInterpolator mInterpolator = |
| 124 | new AccelerateDecelerateInterpolator(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 125 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 126 | private final FadeOutRunnable mFadingOut = new FadeOutRunnable(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 127 | |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 128 | public GestureOverlayView(Context context) { |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 129 | super(context); |
| 130 | init(); |
| 131 | } |
| 132 | |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 133 | public GestureOverlayView(Context context, AttributeSet attrs) { |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 134 | this(context, attrs, com.android.internal.R.attr.gestureOverlayViewStyle); |
| 135 | } |
| 136 | |
| 137 | public GestureOverlayView(Context context, AttributeSet attrs, int defStyle) { |
| 138 | super(context, attrs, defStyle); |
| 139 | |
| 140 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 141 | R.styleable.GestureOverlayView, defStyle, 0); |
| 142 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 143 | mGestureStrokeWidth = a.getFloat(R.styleable.GestureOverlayView_gestureStrokeWidth, |
| 144 | mGestureStrokeWidth); |
| 145 | mInvalidateExtraBorder = Math.max(1, ((int) mGestureStrokeWidth) - 1); |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 146 | mCertainGestureColor = a.getColor(R.styleable.GestureOverlayView_gestureColor, |
| 147 | mCertainGestureColor); |
| 148 | mUncertainGestureColor = a.getColor(R.styleable.GestureOverlayView_uncertainGestureColor, |
| 149 | mUncertainGestureColor); |
| 150 | mFadeDuration = a.getInt(R.styleable.GestureOverlayView_fadeDuration, (int) mFadeDuration); |
| 151 | mFadeOffset = a.getInt(R.styleable.GestureOverlayView_fadeOffset, (int) mFadeOffset); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 152 | mGestureStrokeType = a.getInt(R.styleable.GestureOverlayView_gestureStrokeType, |
| 153 | mGestureStrokeType); |
| 154 | mGestureStrokeLengthThreshold = a.getFloat( |
| 155 | R.styleable.GestureOverlayView_gestureStrokeLengthThreshold, |
| 156 | mGestureStrokeLengthThreshold); |
| 157 | mGestureStrokeAngleThreshold = a.getFloat( |
| 158 | R.styleable.GestureOverlayView_gestureStrokeAngleThreshold, |
| 159 | mGestureStrokeAngleThreshold); |
| 160 | mGestureStrokeSquarenessTreshold = a.getFloat( |
| 161 | R.styleable.GestureOverlayView_gestureStrokeSquarenessThreshold, |
| 162 | mGestureStrokeSquarenessTreshold); |
| 163 | mInterceptEvents = a.getBoolean(R.styleable.GestureOverlayView_eventsInterceptionEnabled, |
| 164 | mInterceptEvents); |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 165 | mFadeEnabled = a.getBoolean(R.styleable.GestureOverlayView_fadeEnabled, |
| 166 | mFadeEnabled); |
Romain Guy | e7c36dd | 2009-05-25 13:51:00 -0700 | [diff] [blame] | 167 | mOrientation = a.getInt(R.styleable.GestureOverlayView_orientation, mOrientation); |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 168 | |
| 169 | a.recycle(); |
| 170 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 171 | init(); |
| 172 | } |
| 173 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 174 | private void init() { |
| 175 | setWillNotDraw(false); |
| 176 | |
| 177 | final Paint gesturePaint = mGesturePaint; |
| 178 | gesturePaint.setAntiAlias(GESTURE_RENDERING_ANTIALIAS); |
| 179 | gesturePaint.setColor(mCertainGestureColor); |
| 180 | gesturePaint.setStyle(Paint.Style.STROKE); |
| 181 | gesturePaint.setStrokeJoin(Paint.Join.ROUND); |
| 182 | gesturePaint.setStrokeCap(Paint.Cap.ROUND); |
| 183 | gesturePaint.setStrokeWidth(mGestureStrokeWidth); |
| 184 | gesturePaint.setDither(DITHER_FLAG); |
| 185 | |
| 186 | mCurrentColor = mCertainGestureColor; |
| 187 | setPaintAlpha(255); |
| 188 | } |
| 189 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 190 | public ArrayList<GesturePoint> getCurrentStroke() { |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 191 | return mStrokeBuffer; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Romain Guy | e7c36dd | 2009-05-25 13:51:00 -0700 | [diff] [blame] | 194 | public int getOrientation() { |
| 195 | return mOrientation; |
| 196 | } |
| 197 | |
| 198 | public void setOrientation(int orientation) { |
| 199 | mOrientation = orientation; |
| 200 | } |
| 201 | |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 202 | public void setGestureColor(int color) { |
| 203 | mCertainGestureColor = color; |
| 204 | } |
| 205 | |
| 206 | public void setUncertainGestureColor(int color) { |
| 207 | mUncertainGestureColor = color; |
| 208 | } |
| 209 | |
| 210 | public int getUncertainGestureColor() { |
| 211 | return mUncertainGestureColor; |
| 212 | } |
| 213 | |
| 214 | public int getGestureColor() { |
| 215 | return mCertainGestureColor; |
| 216 | } |
| 217 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 218 | public float getGestureStrokeWidth() { |
| 219 | return mGestureStrokeWidth; |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 222 | public void setGestureStrokeWidth(float gestureStrokeWidth) { |
| 223 | mGestureStrokeWidth = gestureStrokeWidth; |
| 224 | mInvalidateExtraBorder = Math.max(1, ((int) gestureStrokeWidth) - 1); |
| 225 | mGesturePaint.setStrokeWidth(gestureStrokeWidth); |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 228 | public int getGestureStrokeType() { |
| 229 | return mGestureStrokeType; |
| 230 | } |
| 231 | |
| 232 | public void setGestureStrokeType(int gestureStrokeType) { |
| 233 | mGestureStrokeType = gestureStrokeType; |
| 234 | } |
| 235 | |
| 236 | public float getGestureStrokeLengthThreshold() { |
| 237 | return mGestureStrokeLengthThreshold; |
| 238 | } |
| 239 | |
| 240 | public void setGestureStrokeLengthThreshold(float gestureStrokeLengthThreshold) { |
| 241 | mGestureStrokeLengthThreshold = gestureStrokeLengthThreshold; |
| 242 | } |
| 243 | |
| 244 | public float getGestureStrokeSquarenessTreshold() { |
| 245 | return mGestureStrokeSquarenessTreshold; |
| 246 | } |
| 247 | |
| 248 | public void setGestureStrokeSquarenessTreshold(float gestureStrokeSquarenessTreshold) { |
| 249 | mGestureStrokeSquarenessTreshold = gestureStrokeSquarenessTreshold; |
| 250 | } |
| 251 | |
| 252 | public float getGestureStrokeAngleThreshold() { |
| 253 | return mGestureStrokeAngleThreshold; |
| 254 | } |
| 255 | |
| 256 | public void setGestureStrokeAngleThreshold(float gestureStrokeAngleThreshold) { |
| 257 | mGestureStrokeAngleThreshold = gestureStrokeAngleThreshold; |
| 258 | } |
| 259 | |
| 260 | public boolean isEventsInterceptionEnabled() { |
| 261 | return mInterceptEvents; |
| 262 | } |
| 263 | |
| 264 | public void setEventsInterceptionEnabled(boolean enabled) { |
| 265 | mInterceptEvents = enabled; |
| 266 | } |
| 267 | |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 268 | public boolean isFadeEnabled() { |
| 269 | return mFadeEnabled; |
| 270 | } |
| 271 | |
| 272 | public void setFadeEnabled(boolean fadeEnabled) { |
| 273 | mFadeEnabled = fadeEnabled; |
| 274 | } |
| 275 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 276 | public Gesture getGesture() { |
| 277 | return mCurrentGesture; |
| 278 | } |
| 279 | |
| 280 | public void setGesture(Gesture gesture) { |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 281 | if (mCurrentGesture != null) { |
| 282 | clear(false); |
| 283 | } |
| 284 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 285 | setCurrentColor(mCertainGestureColor); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 286 | mCurrentGesture = gesture; |
| 287 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 288 | final Path path = mCurrentGesture.toPath(); |
| 289 | final RectF bounds = new RectF(); |
| 290 | path.computeBounds(bounds, true); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 291 | |
Romain Guy | cfbe8cf | 2009-06-10 01:21:20 -0700 | [diff] [blame] | 292 | // TODO: The path should also be scaled to fit inside this view |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 293 | mPath.rewind(); |
Romain Guy | 03f0b21 | 2009-06-09 04:15:22 -0700 | [diff] [blame] | 294 | mPath.addPath(path, -bounds.left + (getWidth() - bounds.width()) / 2.0f, |
| 295 | -bounds.top + (getHeight() - bounds.height()) / 2.0f); |
| 296 | |
| 297 | mResetGesture = true; |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 298 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 299 | invalidate(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Romain Guy | b973eef | 2009-06-16 15:53:27 -0700 | [diff] [blame] | 302 | public Path getGesturePath() { |
| 303 | return mPath; |
| 304 | } |
| 305 | |
| 306 | public Path getGesturePath(Path path) { |
| 307 | path.set(mPath); |
| 308 | return path; |
| 309 | } |
| 310 | |
| 311 | public boolean isGestureVisible() { |
| 312 | return mGestureVisible; |
| 313 | } |
| 314 | |
| 315 | public void setGestureVisible(boolean visible) { |
| 316 | mGestureVisible = visible; |
| 317 | } |
| 318 | |
| 319 | public long getFadeOffset() { |
| 320 | return mFadeOffset; |
| 321 | } |
| 322 | |
| 323 | public void setFadeOffset(long fadeOffset) { |
| 324 | mFadeOffset = fadeOffset; |
| 325 | } |
| 326 | |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 327 | public void addOnGestureListener(OnGestureListener listener) { |
| 328 | mOnGestureListeners.add(listener); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 331 | public void removeOnGestureListener(OnGestureListener listener) { |
| 332 | mOnGestureListeners.remove(listener); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 335 | public void removeAllOnGestureListeners() { |
| 336 | mOnGestureListeners.clear(); |
| 337 | } |
| 338 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 339 | public void addOnGesturePerformedListener(OnGesturePerformedListener listener) { |
| 340 | mOnGesturePerformedListeners.add(listener); |
| 341 | if (mOnGesturePerformedListeners.size() > 0) { |
| 342 | mHandleGestureActions = true; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 343 | } |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 346 | public void removeOnGesturePerformedListener(OnGesturePerformedListener listener) { |
| 347 | mOnGesturePerformedListeners.remove(listener); |
| 348 | if (mOnGesturePerformedListeners.size() <= 0) { |
| 349 | mHandleGestureActions = false; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | public void removeAllOnGesturePerformedListeners() { |
| 354 | mOnGesturePerformedListeners.clear(); |
| 355 | mHandleGestureActions = false; |
| 356 | } |
| 357 | |
Romain Guy | 9af0b4f | 2009-06-02 21:56:27 -0700 | [diff] [blame] | 358 | public void addOnGesturingListener(OnGesturingListener listener) { |
| 359 | mOnGesturingListeners.add(listener); |
| 360 | } |
| 361 | |
| 362 | public void removeOnGesturingListener(OnGesturingListener listener) { |
| 363 | mOnGesturingListeners.remove(listener); |
| 364 | } |
| 365 | |
| 366 | public void removeAllOnGesturingListeners() { |
| 367 | mOnGesturingListeners.clear(); |
| 368 | } |
| 369 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 370 | public boolean isGesturing() { |
| 371 | return mIsGesturing; |
| 372 | } |
| 373 | |
| 374 | private void setCurrentColor(int color) { |
| 375 | mCurrentColor = color; |
| 376 | if (mFadingHasStarted) { |
| 377 | setPaintAlpha((int) (255 * mFadingAlpha)); |
| 378 | } else { |
| 379 | setPaintAlpha(255); |
| 380 | } |
| 381 | invalidate(); |
| 382 | } |
| 383 | |
Romain Guy | 27a2b50 | 2009-06-09 04:36:14 -0700 | [diff] [blame] | 384 | /** |
| 385 | * @hide |
| 386 | */ |
| 387 | public Paint getGesturePaint() { |
| 388 | return mGesturePaint; |
| 389 | } |
| 390 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 391 | @Override |
| 392 | public void draw(Canvas canvas) { |
| 393 | super.draw(canvas); |
| 394 | |
Romain Guy | b973eef | 2009-06-16 15:53:27 -0700 | [diff] [blame] | 395 | if (mCurrentGesture != null && mGestureVisible) { |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 396 | canvas.drawPath(mPath, mGesturePaint); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | private void setPaintAlpha(int alpha) { |
| 401 | alpha += alpha >> 7; |
| 402 | final int baseAlpha = mCurrentColor >>> 24; |
| 403 | final int useAlpha = baseAlpha * alpha >> 8; |
| 404 | mGesturePaint.setColor((mCurrentColor << 8 >>> 8) | (useAlpha << 24)); |
| 405 | } |
| 406 | |
| 407 | public void clear(boolean animated) { |
Romain Guy | 03f0b21 | 2009-06-09 04:15:22 -0700 | [diff] [blame] | 408 | clear(animated, false, true); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Romain Guy | 03f0b21 | 2009-06-09 04:15:22 -0700 | [diff] [blame] | 411 | private void clear(boolean animated, boolean fireActionPerformed, boolean immediate) { |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 412 | setPaintAlpha(255); |
Romain Guy | 9edc4e8 | 2009-05-25 19:08:31 -0700 | [diff] [blame] | 413 | removeCallbacks(mFadingOut); |
Romain Guy | 73d2589 | 2009-06-09 03:18:10 -0700 | [diff] [blame] | 414 | mResetGesture = false; |
Romain Guy | 9edc4e8 | 2009-05-25 19:08:31 -0700 | [diff] [blame] | 415 | mFadingOut.fireActionPerformed = fireActionPerformed; |
Romain Guy | 73d2589 | 2009-06-09 03:18:10 -0700 | [diff] [blame] | 416 | mFadingOut.resetMultipleStrokes = false; |
Romain Guy | 9edc4e8 | 2009-05-25 19:08:31 -0700 | [diff] [blame] | 417 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 418 | if (animated && mCurrentGesture != null) { |
Romain Guy | 9edc4e8 | 2009-05-25 19:08:31 -0700 | [diff] [blame] | 419 | mFadingAlpha = 1.0f; |
| 420 | mIsFadingOut = true; |
| 421 | mFadingHasStarted = false; |
| 422 | mFadingStart = AnimationUtils.currentAnimationTimeMillis() + mFadeOffset; |
| 423 | |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 424 | postDelayed(mFadingOut, mFadeOffset); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 425 | } else { |
Romain Guy | 9edc4e8 | 2009-05-25 19:08:31 -0700 | [diff] [blame] | 426 | mFadingAlpha = 1.0f; |
| 427 | mIsFadingOut = false; |
| 428 | mFadingHasStarted = false; |
| 429 | |
Romain Guy | 03f0b21 | 2009-06-09 04:15:22 -0700 | [diff] [blame] | 430 | if (immediate) { |
Romain Guy | 9edc4e8 | 2009-05-25 19:08:31 -0700 | [diff] [blame] | 431 | mCurrentGesture = null; |
| 432 | mPath.rewind(); |
| 433 | invalidate(); |
Romain Guy | 03f0b21 | 2009-06-09 04:15:22 -0700 | [diff] [blame] | 434 | } else if (fireActionPerformed) { |
| 435 | postDelayed(mFadingOut, mFadeOffset); |
Romain Guy | 73d2589 | 2009-06-09 03:18:10 -0700 | [diff] [blame] | 436 | } else if (mGestureStrokeType == GESTURE_STROKE_TYPE_MULTIPLE) { |
| 437 | mFadingOut.resetMultipleStrokes = true; |
| 438 | postDelayed(mFadingOut, mFadeOffset); |
Romain Guy | 03f0b21 | 2009-06-09 04:15:22 -0700 | [diff] [blame] | 439 | } else { |
| 440 | mCurrentGesture = null; |
| 441 | mPath.rewind(); |
| 442 | invalidate(); |
Romain Guy | 9edc4e8 | 2009-05-25 19:08:31 -0700 | [diff] [blame] | 443 | } |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 447 | public void cancelClearAnimation() { |
| 448 | setPaintAlpha(255); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 449 | mIsFadingOut = false; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 450 | mFadingHasStarted = false; |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 451 | removeCallbacks(mFadingOut); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 452 | mPath.rewind(); |
| 453 | mCurrentGesture = null; |
| 454 | } |
| 455 | |
| 456 | public void cancelGesture() { |
| 457 | mIsListeningForGestures = false; |
| 458 | |
| 459 | // add the stroke to the current gesture |
| 460 | mCurrentGesture.addStroke(new GestureStroke(mStrokeBuffer)); |
| 461 | |
| 462 | // pass the event to handlers |
| 463 | final long now = SystemClock.uptimeMillis(); |
| 464 | final MotionEvent event = MotionEvent.obtain(now, now, |
| 465 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
| 466 | |
| 467 | final ArrayList<OnGestureListener> listeners = mOnGestureListeners; |
Romain Guy | 9af0b4f | 2009-06-02 21:56:27 -0700 | [diff] [blame] | 468 | int count = listeners.size(); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 469 | for (int i = 0; i < count; i++) { |
| 470 | listeners.get(i).onGestureCancelled(this, event); |
| 471 | } |
| 472 | |
| 473 | event.recycle(); |
| 474 | |
| 475 | clear(false); |
| 476 | mIsGesturing = false; |
Romain Guy | 834f039 | 2009-06-05 11:24:09 -0700 | [diff] [blame] | 477 | mPreviousWasGesturing = false; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 478 | mStrokeBuffer.clear(); |
Romain Guy | 9af0b4f | 2009-06-02 21:56:27 -0700 | [diff] [blame] | 479 | |
| 480 | final ArrayList<OnGesturingListener> otherListeners = mOnGesturingListeners; |
| 481 | count = otherListeners.size(); |
| 482 | for (int i = 0; i < count; i++) { |
| 483 | otherListeners.get(i).onGesturingEnded(this); |
| 484 | } |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | @Override |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 488 | protected void onDetachedFromWindow() { |
Romain Guy | 46bfc48 | 2013-08-16 18:38:29 -0700 | [diff] [blame] | 489 | super.onDetachedFromWindow(); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 490 | cancelClearAnimation(); |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public boolean dispatchTouchEvent(MotionEvent event) { |
| 495 | if (isEnabled()) { |
Romain Guy | 834f039 | 2009-06-05 11:24:09 -0700 | [diff] [blame] | 496 | final boolean cancelDispatch = (mIsGesturing || (mCurrentGesture != null && |
| 497 | mCurrentGesture.getStrokesCount() > 0 && mPreviousWasGesturing)) && |
| 498 | mInterceptEvents; |
| 499 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 500 | processEvent(event); |
| 501 | |
| 502 | if (cancelDispatch) { |
| 503 | event.setAction(MotionEvent.ACTION_CANCEL); |
| 504 | } |
| 505 | |
| 506 | super.dispatchTouchEvent(event); |
Romain Guy | 834f039 | 2009-06-05 11:24:09 -0700 | [diff] [blame] | 507 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 508 | return true; |
| 509 | } |
| 510 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 511 | return super.dispatchTouchEvent(event); |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 514 | private boolean processEvent(MotionEvent event) { |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 515 | switch (event.getAction()) { |
| 516 | case MotionEvent.ACTION_DOWN: |
Romain Guy | e7c36dd | 2009-05-25 13:51:00 -0700 | [diff] [blame] | 517 | touchDown(event); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 518 | invalidate(); |
| 519 | return true; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 520 | case MotionEvent.ACTION_MOVE: |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 521 | if (mIsListeningForGestures) { |
| 522 | Rect rect = touchMove(event); |
| 523 | if (rect != null) { |
| 524 | invalidate(rect); |
| 525 | } |
| 526 | return true; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 527 | } |
| 528 | break; |
| 529 | case MotionEvent.ACTION_UP: |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 530 | if (mIsListeningForGestures) { |
| 531 | touchUp(event, false); |
| 532 | invalidate(); |
| 533 | return true; |
| 534 | } |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 535 | break; |
| 536 | case MotionEvent.ACTION_CANCEL: |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 537 | if (mIsListeningForGestures) { |
| 538 | touchUp(event, true); |
| 539 | invalidate(); |
| 540 | return true; |
| 541 | } |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 542 | } |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 543 | |
| 544 | return false; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Romain Guy | e7c36dd | 2009-05-25 13:51:00 -0700 | [diff] [blame] | 547 | private void touchDown(MotionEvent event) { |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 548 | mIsListeningForGestures = true; |
| 549 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 550 | float x = event.getX(); |
| 551 | float y = event.getY(); |
| 552 | |
| 553 | mX = x; |
| 554 | mY = y; |
| 555 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 556 | mTotalLength = 0; |
| 557 | mIsGesturing = false; |
| 558 | |
Romain Guy | 73d2589 | 2009-06-09 03:18:10 -0700 | [diff] [blame] | 559 | if (mGestureStrokeType == GESTURE_STROKE_TYPE_SINGLE || mResetGesture) { |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 560 | if (mHandleGestureActions) setCurrentColor(mUncertainGestureColor); |
Romain Guy | 73d2589 | 2009-06-09 03:18:10 -0700 | [diff] [blame] | 561 | mResetGesture = false; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 562 | mCurrentGesture = null; |
| 563 | mPath.rewind(); |
| 564 | } else if (mCurrentGesture == null || mCurrentGesture.getStrokesCount() == 0) { |
| 565 | if (mHandleGestureActions) setCurrentColor(mUncertainGestureColor); |
| 566 | } |
| 567 | |
| 568 | // if there is fading out going on, stop it. |
| 569 | if (mFadingHasStarted) { |
| 570 | cancelClearAnimation(); |
| 571 | } else if (mIsFadingOut) { |
| 572 | setPaintAlpha(255); |
| 573 | mIsFadingOut = false; |
| 574 | mFadingHasStarted = false; |
| 575 | removeCallbacks(mFadingOut); |
| 576 | } |
| 577 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 578 | if (mCurrentGesture == null) { |
| 579 | mCurrentGesture = new Gesture(); |
| 580 | } |
| 581 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 582 | mStrokeBuffer.add(new GesturePoint(x, y, event.getEventTime())); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 583 | mPath.moveTo(x, y); |
| 584 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 585 | final int border = mInvalidateExtraBorder; |
| 586 | mInvalidRect.set((int) x - border, (int) y - border, (int) x + border, (int) y + border); |
| 587 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 588 | mCurveEndX = x; |
| 589 | mCurveEndY = y; |
Romain Guy | ff686ce | 2009-05-25 01:33:00 -0700 | [diff] [blame] | 590 | |
| 591 | // pass the event to handlers |
| 592 | final ArrayList<OnGestureListener> listeners = mOnGestureListeners; |
| 593 | final int count = listeners.size(); |
| 594 | for (int i = 0; i < count; i++) { |
| 595 | listeners.get(i).onGestureStarted(this, event); |
Romain Guy | 8d78756 | 2009-05-29 15:02:55 -0700 | [diff] [blame] | 596 | } |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | private Rect touchMove(MotionEvent event) { |
| 600 | Rect areaToRefresh = null; |
Romain Guy | 8d78756 | 2009-05-29 15:02:55 -0700 | [diff] [blame] | 601 | |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 602 | final float x = event.getX(); |
| 603 | final float y = event.getY(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 604 | |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 605 | final float previousX = mX; |
| 606 | final float previousY = mY; |
| 607 | |
| 608 | final float dx = Math.abs(x - previousX); |
| 609 | final float dy = Math.abs(y - previousY); |
Romain Guy | 8d78756 | 2009-05-29 15:02:55 -0700 | [diff] [blame] | 610 | |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 611 | if (dx >= GestureStroke.TOUCH_TOLERANCE || dy >= GestureStroke.TOUCH_TOLERANCE) { |
| 612 | areaToRefresh = mInvalidRect; |
| 613 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 614 | // start with the curve end |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 615 | final int border = mInvalidateExtraBorder; |
| 616 | areaToRefresh.set((int) mCurveEndX - border, (int) mCurveEndY - border, |
| 617 | (int) mCurveEndX + border, (int) mCurveEndY + border); |
Romain Guy | 8d78756 | 2009-05-29 15:02:55 -0700 | [diff] [blame] | 618 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 619 | float cX = mCurveEndX = (x + previousX) / 2; |
| 620 | float cY = mCurveEndY = (y + previousY) / 2; |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 621 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 622 | mPath.quadTo(previousX, previousY, cX, cY); |
Romain Guy | 8d78756 | 2009-05-29 15:02:55 -0700 | [diff] [blame] | 623 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 624 | // union with the control point of the new curve |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 625 | areaToRefresh.union((int) previousX - border, (int) previousY - border, |
| 626 | (int) previousX + border, (int) previousY + border); |
Romain Guy | 8d78756 | 2009-05-29 15:02:55 -0700 | [diff] [blame] | 627 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 628 | // union with the end point of the new curve |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 629 | areaToRefresh.union((int) cX - border, (int) cY - border, |
| 630 | (int) cX + border, (int) cY + border); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 631 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 632 | mX = x; |
| 633 | mY = y; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 634 | |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 635 | mStrokeBuffer.add(new GesturePoint(x, y, event.getEventTime())); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 636 | |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 637 | if (mHandleGestureActions && !mIsGesturing) { |
| 638 | mTotalLength += (float) Math.sqrt(dx * dx + dy * dy); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 639 | |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 640 | if (mTotalLength > mGestureStrokeLengthThreshold) { |
| 641 | final OrientedBoundingBox box = |
Romain Guy | 46c5312 | 2010-02-04 14:19:50 -0800 | [diff] [blame] | 642 | GestureUtils.computeOrientedBoundingBox(mStrokeBuffer); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 643 | |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 644 | float angle = Math.abs(box.orientation); |
| 645 | if (angle > 90) { |
| 646 | angle = 180 - angle; |
| 647 | } |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 648 | |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 649 | if (box.squareness > mGestureStrokeSquarenessTreshold || |
| 650 | (mOrientation == ORIENTATION_VERTICAL ? |
| 651 | angle < mGestureStrokeAngleThreshold : |
| 652 | angle > mGestureStrokeAngleThreshold)) { |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 653 | |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 654 | mIsGesturing = true; |
| 655 | setCurrentColor(mCertainGestureColor); |
Romain Guy | 9af0b4f | 2009-06-02 21:56:27 -0700 | [diff] [blame] | 656 | |
| 657 | final ArrayList<OnGesturingListener> listeners = mOnGesturingListeners; |
| 658 | int count = listeners.size(); |
| 659 | for (int i = 0; i < count; i++) { |
| 660 | listeners.get(i).onGesturingStarted(this); |
| 661 | } |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 662 | } |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 663 | } |
| 664 | } |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 665 | |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 666 | // pass the event to handlers |
| 667 | final ArrayList<OnGestureListener> listeners = mOnGestureListeners; |
| 668 | final int count = listeners.size(); |
| 669 | for (int i = 0; i < count; i++) { |
| 670 | listeners.get(i).onGesture(this, event); |
| 671 | } |
| 672 | } |
Romain Guy | ff686ce | 2009-05-25 01:33:00 -0700 | [diff] [blame] | 673 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 674 | return areaToRefresh; |
| 675 | } |
| 676 | |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 677 | private void touchUp(MotionEvent event, boolean cancel) { |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 678 | mIsListeningForGestures = false; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 679 | |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 680 | // A gesture wasn't started or was cancelled |
| 681 | if (mCurrentGesture != null) { |
| 682 | // add the stroke to the current gesture |
| 683 | mCurrentGesture.addStroke(new GestureStroke(mStrokeBuffer)); |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 684 | |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 685 | if (!cancel) { |
| 686 | // pass the event to handlers |
| 687 | final ArrayList<OnGestureListener> listeners = mOnGestureListeners; |
| 688 | int count = listeners.size(); |
| 689 | for (int i = 0; i < count; i++) { |
| 690 | listeners.get(i).onGestureEnded(this, event); |
| 691 | } |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 692 | |
Romain Guy | 03f0b21 | 2009-06-09 04:15:22 -0700 | [diff] [blame] | 693 | clear(mHandleGestureActions && mFadeEnabled, mHandleGestureActions && mIsGesturing, |
| 694 | false); |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 695 | } else { |
| 696 | cancelGesture(event); |
| 697 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 698 | } |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 699 | } else { |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 700 | cancelGesture(event); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 701 | } |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 702 | |
Romain Guy | 8d78756 | 2009-05-29 15:02:55 -0700 | [diff] [blame] | 703 | mStrokeBuffer.clear(); |
Romain Guy | 834f039 | 2009-06-05 11:24:09 -0700 | [diff] [blame] | 704 | mPreviousWasGesturing = mIsGesturing; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 705 | mIsGesturing = false; |
Romain Guy | 9af0b4f | 2009-06-02 21:56:27 -0700 | [diff] [blame] | 706 | |
| 707 | final ArrayList<OnGesturingListener> listeners = mOnGesturingListeners; |
| 708 | int count = listeners.size(); |
| 709 | for (int i = 0; i < count; i++) { |
| 710 | listeners.get(i).onGesturingEnded(this); |
| 711 | } |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Romain Guy | d1c67d4 | 2009-05-29 13:53:16 -0700 | [diff] [blame] | 714 | private void cancelGesture(MotionEvent event) { |
| 715 | // pass the event to handlers |
| 716 | final ArrayList<OnGestureListener> listeners = mOnGestureListeners; |
| 717 | final int count = listeners.size(); |
| 718 | for (int i = 0; i < count; i++) { |
| 719 | listeners.get(i).onGestureCancelled(this, event); |
| 720 | } |
| 721 | |
| 722 | clear(false); |
| 723 | } |
| 724 | |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 725 | private void fireOnGesturePerformed() { |
Romain Guy | 9af0b4f | 2009-06-02 21:56:27 -0700 | [diff] [blame] | 726 | final ArrayList<OnGesturePerformedListener> actionListeners = mOnGesturePerformedListeners; |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 727 | final int count = actionListeners.size(); |
| 728 | for (int i = 0; i < count; i++) { |
Romain Guy | 9af0b4f | 2009-06-02 21:56:27 -0700 | [diff] [blame] | 729 | actionListeners.get(i).onGesturePerformed(GestureOverlayView.this, mCurrentGesture); |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 730 | } |
Romain Guy | e7c36dd | 2009-05-25 13:51:00 -0700 | [diff] [blame] | 731 | } |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 732 | |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 733 | private class FadeOutRunnable implements Runnable { |
| 734 | boolean fireActionPerformed; |
Romain Guy | 73d2589 | 2009-06-09 03:18:10 -0700 | [diff] [blame] | 735 | boolean resetMultipleStrokes; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 736 | |
| 737 | public void run() { |
| 738 | if (mIsFadingOut) { |
| 739 | final long now = AnimationUtils.currentAnimationTimeMillis(); |
| 740 | final long duration = now - mFadingStart; |
| 741 | |
| 742 | if (duration > mFadeDuration) { |
| 743 | if (fireActionPerformed) { |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 744 | fireOnGesturePerformed(); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Romain Guy | 834f039 | 2009-06-05 11:24:09 -0700 | [diff] [blame] | 747 | mPreviousWasGesturing = false; |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 748 | mIsFadingOut = false; |
| 749 | mFadingHasStarted = false; |
| 750 | mPath.rewind(); |
| 751 | mCurrentGesture = null; |
| 752 | setPaintAlpha(255); |
| 753 | } else { |
| 754 | mFadingHasStarted = true; |
| 755 | float interpolatedTime = Math.max(0.0f, |
| 756 | Math.min(1.0f, duration / (float) mFadeDuration)); |
| 757 | mFadingAlpha = 1.0f - mInterpolator.getInterpolation(interpolatedTime); |
| 758 | setPaintAlpha((int) (255 * mFadingAlpha)); |
| 759 | postDelayed(this, FADE_ANIMATION_RATE); |
| 760 | } |
Romain Guy | 73d2589 | 2009-06-09 03:18:10 -0700 | [diff] [blame] | 761 | } else if (resetMultipleStrokes) { |
| 762 | mResetGesture = true; |
Romain Guy | 9edc4e8 | 2009-05-25 19:08:31 -0700 | [diff] [blame] | 763 | } else { |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 764 | fireOnGesturePerformed(); |
| 765 | |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 766 | mFadingHasStarted = false; |
| 767 | mPath.rewind(); |
| 768 | mCurrentGesture = null; |
Romain Guy | 834f039 | 2009-06-05 11:24:09 -0700 | [diff] [blame] | 769 | mPreviousWasGesturing = false; |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 770 | setPaintAlpha(255); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 771 | } |
Romain Guy | 9edc4e8 | 2009-05-25 19:08:31 -0700 | [diff] [blame] | 772 | |
Romain Guy | 8d78756 | 2009-05-29 15:02:55 -0700 | [diff] [blame] | 773 | invalidate(); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Romain Guy | 9af0b4f | 2009-06-02 21:56:27 -0700 | [diff] [blame] | 777 | public static interface OnGesturingListener { |
| 778 | void onGesturingStarted(GestureOverlayView overlay); |
| 779 | |
| 780 | void onGesturingEnded(GestureOverlayView overlay); |
| 781 | } |
| 782 | |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 783 | public static interface OnGestureListener { |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 784 | void onGestureStarted(GestureOverlayView overlay, MotionEvent event); |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 785 | |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 786 | void onGesture(GestureOverlayView overlay, MotionEvent event); |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 787 | |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 788 | void onGestureEnded(GestureOverlayView overlay, MotionEvent event); |
| 789 | |
| 790 | void onGestureCancelled(GestureOverlayView overlay, MotionEvent event); |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 791 | } |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 792 | |
| 793 | public static interface OnGesturePerformedListener { |
| 794 | void onGesturePerformed(GestureOverlayView overlay, Gesture gesture); |
| 795 | } |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 796 | } |