1 /* 2 * Copyright (C) 2010 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.photoeditor.actions; 18 19 import android.content.Context; 20 import android.graphics.PointF; 21 import android.util.AttributeSet; 22 import android.view.GestureDetector; 23 import android.view.MotionEvent; 24 25 /** 26 * A view that detects user gestures and touch motions. 27 */ 28 class TouchView extends FullscreenToolView { 29 30 /** 31 * Listener of swipes. 32 */ 33 public interface SwipeListener { 34 35 void onSwipeLeft(); 36 37 void onSwipeRight(); 38 39 void onSwipeUp(); 40 41 void onSwipeDown(); 42 } 43 44 /** 45 * Listener of single tap on a point (relative to photo coordinates). 46 */ 47 public interface SingleTapListener { 48 49 void onSingleTap(PointF point); 50 } 51 52 private final GestureDetector gestureDetector; 53 54 private SwipeListener swipeListener; 55 private SingleTapListener singleTapListener; 56 57 public TouchView(Context context, AttributeSet attrs) { 58 super(context, attrs); 59 60 final int swipeThreshold = (int) (500 * getResources().getDisplayMetrics().density); 61 gestureDetector = new GestureDetector( 62 context, new GestureDetector.SimpleOnGestureListener() { 63 64 @Override 65 public boolean onDown(MotionEvent e) { 66 // GestureDetector onTouchEvent returns true for fling events only when their 67 // preceding down events are consumed. 68 return true; 69 } 70 71 @Override 72 public boolean onSingleTapUp(MotionEvent e) { 73 if (singleTapListener != null) { 74 PointF point = new PointF(); 75 mapPhotoPoint(e.getX(), e.getY(), point); 76 singleTapListener.onSingleTap(point); 77 } 78 return true; 79 } 80 81 @Override 82 public boolean onFling( 83 MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) { 84 if (swipeListener != null) { 85 float absX = Math.abs(velocityX); 86 float absY = Math.abs(velocityY); 87 float deltaX = me2.getX() - me1.getX(); 88 float deltaY = me2.getY() - me1.getY(); 89 int travelX = getWidth() / 4; 90 int travelY = getHeight() / 4; 91 if (velocityX > swipeThreshold && absY < absX && deltaX > travelX) { 92 swipeListener.onSwipeRight(); 93 } else if (velocityX < -swipeThreshold && absY < absX && deltaX < -travelX) { 94 swipeListener.onSwipeLeft(); 95 } else if (velocityY < -swipeThreshold && absX < absY && deltaY < -travelY) { 96 swipeListener.onSwipeUp(); 97 } else if (velocityY > swipeThreshold && absX < absY / 2 && deltaY > travelY) { 98 swipeListener.onSwipeDown(); 99 } 100 } 101 return true; 102 } 103 }); 104 gestureDetector.setIsLongpressEnabled(false); 105 } 106 107 public void setSwipeListener(SwipeListener listener) { 108 swipeListener = listener; 109 } 110 111 public void setSingleTapListener(SingleTapListener listener) { 112 singleTapListener = listener; 113 } 114 115 @Override 116 public boolean onTouchEvent(MotionEvent event) { 117 return isEnabled() && gestureDetector.onTouchEvent(event); 118 } 119 } 120