Back to project page Android-GUI-Creator.
The source code is released under:
GNU General Public License
If you think the Android project Android-GUI-Creator listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (c) 2012.// ww w . ja v a2 s . c o m * This file is part of Android Interface Toolkit application. * * Android Interface Toolkit is free software: you can redistribute it * and/or modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * Android Interface Toolkit is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * Public License for more details. * * You should have received a copy of the GNU General Public License along * with Android Interface Toolkit. If not, see <http://www.gnu.org/licenses/>. */ package name.wildswift.android.guitool.gesture.recognizers; import android.view.MotionEvent; import name.wildswift.android.guitool.gesture.OnGestureListener; import name.wildswift.android.guitool.gesture.gestures.LongPress; import name.wildswift.android.guitool.gesture.gestures.MotionPoint; import name.wildswift.android.guitool.gesture.gestures.SingleTap; import name.wildswift.android.guitool.gesture.recognizers.simple.DownSimpleGesture; import name.wildswift.android.guitool.gesture.recognizers.simple.LongPressSimpleGesture; import name.wildswift.android.guitool.gesture.recognizers.simple.SimpleGesture; import name.wildswift.android.guitool.gesture.recognizers.simple.SingleTapSimpleGesture; import java.util.ArrayList; import java.util.List; /** * 19.02.12 * * @author Swift */ public class LongPressRecognizer extends GestureRecognizer{ private OnGestureListener listener; private static final long TIME_DELTA = 50; public LongPressRecognizer(OnGestureListener listener) { this.listener = listener; } private long timeStart = -1; private int fingersCount = 0; private List<MotionPoint> points = new ArrayList<MotionPoint>(10); @SuppressWarnings("ConstantConditions") @Override public boolean onNewEvent(SimpleGesture[] gestures) { for (int i = 0; i < gestures.length; i++) { SimpleGesture gesture = gestures[i]; if (gesture == null) continue; if (gesture.getType() != SimpleGesture.DOWN && gesture.getType() != SimpleGesture.LONG_PRESS) return false; if (gesture.getType() == SimpleGesture.DOWN) { if (i == 0) { timeStart = ((DownSimpleGesture) gesture).getEvent().getEventTime(); fingersCount = 1; } if (i > 0) { if (Math.abs(timeStart - ((DownSimpleGesture) gesture).getEvent().getEventTime()) >= TIME_DELTA) { resetRecognizer(); return false; } else { fingersCount++; } } } if (gesture.getType() == SimpleGesture.LONG_PRESS) { MotionEvent event = ((LongPressSimpleGesture) gesture).getEvent(); points.add(new MotionPoint(event.getX(), event.getY())); fingersCount--; if (fingersCount == 0) { listener.onGesture(LongPress.obtain(points.toArray(new MotionPoint[points.size()]))); resetRecognizer(); } } } return true; } private void resetRecognizer() { timeStart = -1; fingersCount = 0; points.clear(); } }