Home | History | Annotate | Download | only in actions
      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 single tap on a point (relative to photo coordinates).
     32      */
     33     public interface SingleTapListener {
     34 
     35         void onSingleTap(PointF point);
     36     }
     37 
     38     private final GestureDetector gestureDetector;
     39     private SingleTapListener singleTapListener;
     40 
     41     public TouchView(Context context, AttributeSet attrs) {
     42         super(context, attrs);
     43 
     44         gestureDetector = new GestureDetector(
     45                 context, new GestureDetector.SimpleOnGestureListener() {
     46 
     47             @Override
     48             public boolean onDown(MotionEvent e) {
     49                 // GestureDetector onTouchEvent returns true only for events whose preceding
     50                 // down-events have been consumed.
     51                 return true;
     52             }
     53 
     54             @Override
     55             public boolean onSingleTapUp(MotionEvent e) {
     56                 if (singleTapListener != null) {
     57                     PointF point = new PointF();
     58                     mapPhotoPoint(e.getX(), e.getY(), point);
     59                     singleTapListener.onSingleTap(point);
     60                 }
     61                 return true;
     62             }
     63         });
     64         gestureDetector.setIsLongpressEnabled(false);
     65     }
     66 
     67     public void setSingleTapListener(SingleTapListener listener) {
     68         singleTapListener = listener;
     69     }
     70 
     71     @Override
     72     public boolean onTouchEvent(MotionEvent event) {
     73         return isEnabled() && gestureDetector.onTouchEvent(event);
     74     }
     75 }
     76