1 /* 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 17 package com.android.gallery3d.ui; 18 19 import android.content.Context; 20 import android.os.SystemClock; 21 import android.view.GestureDetector; 22 import android.view.MotionEvent; 23 import android.view.ScaleGestureDetector; 24 25 // This class aggregates three gesture detectors: GestureDetector, 26 // ScaleGestureDetector, and DownUpDetector. 27 public 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, float totalX, float totalY); 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(float x, float y); 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 66 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 74 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( 90 dx, dy, e2.getX() - e1.getX(), e2.getY() - e1.getY()); 91 } 92 93 @Override 94 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 95 float velocityY) { 96 return mListener.onFling(velocityX, velocityY); 97 } 98 } 99 100 private class MyScaleListener 101 extends ScaleGestureDetector.SimpleOnScaleGestureListener { 102 @Override 103 public boolean onScaleBegin(ScaleGestureDetector detector) { 104 return mListener.onScaleBegin( 105 detector.getFocusX(), detector.getFocusY()); 106 } 107 108 @Override 109 public boolean onScale(ScaleGestureDetector detector) { 110 return mListener.onScale(detector.getFocusX(), 111 detector.getFocusY(), detector.getScaleFactor()); 112 } 113 114 @Override 115 public void onScaleEnd(ScaleGestureDetector detector) { 116 mListener.onScaleEnd(); 117 } 118 } 119 120 private class MyDownUpListener implements DownUpDetector.DownUpListener { 121 @Override 122 public void onDown(MotionEvent e) { 123 mListener.onDown(e.getX(), e.getY()); 124 } 125 126 @Override 127 public void onUp(MotionEvent e) { 128 mListener.onUp(); 129 } 130 } 131 } 132