blob: 4a17d4364119911721a9d4795abcf27a5ff9ee28 [file] [log] [blame]
Chih-Chung Chang762f8e22012-03-14 17:39:42 +08001/*
2 * Copyright (C) 2012 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
17package com.android.gallery3d.ui;
18
19import android.content.Context;
Chih-Chung Chang95860d22012-03-21 19:01:30 +080020import android.os.SystemClock;
Chih-Chung Chang762f8e22012-03-14 17:39:42 +080021import android.view.GestureDetector;
22import android.view.MotionEvent;
23import android.view.ScaleGestureDetector;
24
25// This class aggregates three gesture detectors: GestureDetector,
26// ScaleGestureDetector, and DownUpDetector.
27public class GestureRecognizer {
28 private static final String TAG = "GestureRecognizer";
29
30 public interface Listener {
31 boolean onSingleTapUp(float x, float y);
32 boolean onDoubleTap(float x, float y);
33 boolean onScroll(float dx, float dy);
34 boolean onFling(float velocityX, float velocityY);
35 boolean onScaleBegin(float focusX, float focusY);
36 boolean onScale(float focusX, float focusY, float scale);
37 void onScaleEnd();
38 void onDown();
39 void onUp();
40 }
41
42 private final GestureDetector mGestureDetector;
43 private final ScaleGestureDetector mScaleDetector;
44 private final DownUpDetector mDownUpDetector;
45 private final Listener mListener;
46
47 public GestureRecognizer(Context context, Listener listener) {
48 mListener = listener;
49 mGestureDetector = new GestureDetector(context, new MyGestureListener(),
50 null, true /* ignoreMultitouch */);
51 mScaleDetector = new ScaleGestureDetector(
52 context, new MyScaleListener());
53 mDownUpDetector = new DownUpDetector(new MyDownUpListener());
54 }
55
56 public void onTouchEvent(MotionEvent event) {
57 mGestureDetector.onTouchEvent(event);
58 mScaleDetector.onTouchEvent(event);
59 mDownUpDetector.onTouchEvent(event);
60 }
61
62 public boolean isDown() {
63 return mDownUpDetector.isDown();
64 }
65
Chih-Chung Chang95860d22012-03-21 19:01:30 +080066 public void cancelScale() {
67 long now = SystemClock.uptimeMillis();
68 MotionEvent cancelEvent = MotionEvent.obtain(
69 now, now, MotionEvent.ACTION_CANCEL, 0, 0, 0);
70 mScaleDetector.onTouchEvent(cancelEvent);
71 cancelEvent.recycle();
72 }
73
Chih-Chung Chang762f8e22012-03-14 17:39:42 +080074 private class MyGestureListener
75 extends GestureDetector.SimpleOnGestureListener {
76 @Override
77 public boolean onSingleTapUp(MotionEvent e) {
78 return mListener.onSingleTapUp(e.getX(), e.getY());
79 }
80
81 @Override
82 public boolean onDoubleTap(MotionEvent e) {
83 return mListener.onDoubleTap(e.getX(), e.getY());
84 }
85
86 @Override
87 public boolean onScroll(
88 MotionEvent e1, MotionEvent e2, float dx, float dy) {
89 return mListener.onScroll(dx, dy);
90 }
91
92 @Override
93 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
94 float velocityY) {
95 return mListener.onFling(velocityX, velocityY);
96 }
97 }
98
99 private class MyScaleListener
100 extends ScaleGestureDetector.SimpleOnScaleGestureListener {
101 @Override
102 public boolean onScaleBegin(ScaleGestureDetector detector) {
103 return mListener.onScaleBegin(
104 detector.getFocusX(), detector.getFocusY());
105 }
106
107 @Override
108 public boolean onScale(ScaleGestureDetector detector) {
109 return mListener.onScale(detector.getFocusX(),
110 detector.getFocusY(), detector.getScaleFactor());
111 }
112
113 @Override
114 public void onScaleEnd(ScaleGestureDetector detector) {
115 mListener.onScaleEnd();
116 }
117 }
118
119 private class MyDownUpListener implements DownUpDetector.DownUpListener {
120 @Override
121 public void onDown(MotionEvent e) {
122 mListener.onDown();
123 }
124
125 @Override
126 public void onUp(MotionEvent e) {
127 mListener.onUp();
128 }
129 }
130}