00001 /* 00002 * Copyright (C) 2012 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 00005 * use this file except in compliance with the License. You may obtain a copy of 00006 * the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 00013 * License for the specific language governing permissions and limitations under 00014 * the License. 00015 */ 00016 00017 package org.ros.android.view.visualization; 00018 00019 import android.view.MotionEvent; 00020 import org.ros.math.RosMath; 00021 00025 public class RotateGestureDetector { 00026 00027 public interface OnRotateGestureListener { 00028 boolean onRotate(MotionEvent event1, MotionEvent event2, double deltaAngle); 00029 } 00030 00031 private static final double MAX_DELTA_ANGLE = 1e-1; 00032 00033 private final OnRotateGestureListener listener; 00034 00035 private MotionEvent previousMotionEvent; 00036 00037 public RotateGestureDetector(OnRotateGestureListener listener) { 00038 this.listener = listener; 00039 } 00040 00041 private double angle(MotionEvent event) { 00042 double deltaX = (event.getX(0) - event.getX(1)); 00043 double deltaY = (event.getY(0) - event.getY(1)); 00044 return Math.atan2(deltaY, deltaX); 00045 } 00046 00047 public boolean onTouchEvent(MotionEvent event) { 00048 if (event.getPointerCount() != 2) { 00049 return false; 00050 } 00051 if (previousMotionEvent == null) { 00052 previousMotionEvent = MotionEvent.obtain(event); 00053 return false; 00054 } 00055 double deltaAngle = 00056 RosMath.clamp(angle(previousMotionEvent) - angle(event), -MAX_DELTA_ANGLE, MAX_DELTA_ANGLE); 00057 boolean result = listener.onRotate(previousMotionEvent, event, deltaAngle); 00058 previousMotionEvent.recycle(); 00059 previousMotionEvent = MotionEvent.obtain(event); 00060 return result; 00061 } 00062 }