• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.photoeditor.actions;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.GestureDetector;
22 import android.view.MotionEvent;
23 import android.view.View;
24 
25 /**
26  * A view that detects user gestures and touch motions.
27  */
28 class TouchView extends View {
29 
30     /**
31      * Listener of swipes.
32      */
33     public interface SwipeListener {
34 
onSwipeLeft()35         void onSwipeLeft();
36 
onSwipeRight()37         void onSwipeRight();
38 
onSwipeUp()39         void onSwipeUp();
40 
onSwipeDown()41         void onSwipeDown();
42     }
43 
44     /**
45      * Listener of single tap confirmed (click).
46      */
47     public interface SingleTapListener {
48 
onSingleTap(float x, float y)49         void onSingleTap(float x, float y);
50     }
51 
52     private final GestureDetector gestureDetector;
53 
54     private SwipeListener swipeListener;
55     private SingleTapListener singleTapListener;
56 
TouchView(Context context, AttributeSet attrs)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                     singleTapListener.onSingleTap(e.getX(), e.getY());
75                 }
76                 return true;
77             }
78 
79             @Override
80             public boolean onFling(
81                     MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
82                 if (swipeListener != null) {
83                     float absX = Math.abs(velocityX);
84                     float absY = Math.abs(velocityY);
85                     float deltaX = me2.getX() - me1.getX();
86                     float deltaY = me2.getY() - me1.getY();
87                     int travelX = getWidth() / 4;
88                     int travelY = getHeight() / 4;
89                     if (velocityX > swipeThreshold && absY < absX && deltaX > travelX) {
90                         swipeListener.onSwipeRight();
91                     } else if (velocityX < -swipeThreshold && absY < absX && deltaX < -travelX) {
92                         swipeListener.onSwipeLeft();
93                     } else if (velocityY < -swipeThreshold && absX < absY && deltaY < -travelY) {
94                         swipeListener.onSwipeUp();
95                     } else if (velocityY > swipeThreshold && absX < absY / 2 && deltaY > travelY) {
96                         swipeListener.onSwipeDown();
97                     }
98                 }
99                 return true;
100             }
101         });
102         gestureDetector.setIsLongpressEnabled(false);
103     }
104 
setSwipeListener(SwipeListener listener)105     public void setSwipeListener(SwipeListener listener) {
106         swipeListener = listener;
107     }
108 
setSingleTapListener(SingleTapListener listener)109     public void setSingleTapListener(SingleTapListener listener) {
110         singleTapListener = listener;
111     }
112 
113     @Override
onTouchEvent(MotionEvent event)114     public boolean onTouchEvent(MotionEvent event) {
115         return isEnabled() && gestureDetector.onTouchEvent(event);
116     }
117 }
118