FilmStripGestureRecognizer.java revision 7234f4d6ff4fdcd47eb428405d2b31a5711d20ce
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
17package com.android.camera.ui;
18
19import android.content.Context;
20import android.view.GestureDetector;
21import android.view.MotionEvent;
22import android.view.ScaleGestureDetector;
23
24// This class aggregates three gesture detectors: GestureDetector,
25// ScaleGestureDetector.
26public class FilmStripGestureRecognizer {
27    @SuppressWarnings("unused")
28    private static final String TAG = "FilmStripGestureRecognizer";
29
30    public interface Listener {
31        boolean onSingleTapUp(float x, float y);
32        boolean onDoubleTap(float x, float y);
33        boolean onScroll(float x, float y, 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        boolean onDown(float x, float y);
38        void onScaleEnd();
39    }
40
41    private final GestureDetector mGestureDetector;
42    private final ScaleGestureDetector mScaleDetector;
43    private final Listener mListener;
44
45    public FilmStripGestureRecognizer(Context context, Listener listener) {
46        mListener = listener;
47        mGestureDetector = new GestureDetector(context, new MyGestureListener(),
48                null, true /* ignoreMultitouch */);
49        mScaleDetector = new ScaleGestureDetector(
50                context, new MyScaleListener());
51    }
52
53    public void onTouchEvent(MotionEvent event) {
54        mGestureDetector.onTouchEvent(event);
55        mScaleDetector.onTouchEvent(event);
56    }
57
58    private class MyGestureListener
59                extends GestureDetector.SimpleOnGestureListener {
60        @Override
61        public boolean onSingleTapUp(MotionEvent e) {
62            return mListener.onSingleTapUp(e.getX(), e.getY());
63        }
64
65        @Override
66        public boolean onDoubleTap(MotionEvent e) {
67            return mListener.onDoubleTap(e.getX(), e.getY());
68        }
69
70        @Override
71        public boolean onScroll(
72                MotionEvent e1, MotionEvent e2, float dx, float dy) {
73            return mListener.onScroll(e2.getX(), e2.getY(), dx, dy);
74        }
75
76        @Override
77        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
78                float velocityY) {
79            return mListener.onFling(velocityX, velocityY);
80        }
81
82        @Override
83        public boolean onDown(MotionEvent e) {
84            mListener.onDown(e.getX(), e.getY());
85            return super.onDown(e);
86        }
87    }
88
89    private class MyScaleListener
90            extends ScaleGestureDetector.SimpleOnScaleGestureListener {
91        @Override
92        public boolean onScaleBegin(ScaleGestureDetector detector) {
93            return mListener.onScaleBegin(
94                    detector.getFocusX(), detector.getFocusY());
95        }
96
97        @Override
98        public boolean onScale(ScaleGestureDetector detector) {
99            return mListener.onScale(detector.getFocusX(),
100                    detector.getFocusY(), detector.getScaleFactor());
101        }
102
103        @Override
104        public void onScaleEnd(ScaleGestureDetector detector) {
105            mListener.onScaleEnd();
106        }
107    }
108}
109