Android Open Source - remoteyourcam-usb Gesture Detector






From Project

Back to project page remoteyourcam-usb.

License

The source code is released under:

Apache License

If you think the Android project remoteyourcam-usb listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright 2013 Nils Assbeck, Guersel Ayaz and Michael Zoech
 */*from  www .j  a  v a  2 s  . c o m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.remoteyourcam.usb;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;

public class GestureDetector {

    public interface GestureHandler {
        void onLongTouch(float posx, float posy);

        void onPinchZoom(float pX, float pY, float distInPixel);

        void onTouchMove(float dx, float dy);

        void onFling(float velx, float vely);

        void onStopFling();
    }

    private static class TouchInfo {
        public int id;
        public float startX;
        public float startY;
        public boolean moved;
        public float lastX;
        public float lastY;
        public float currentX;
        public float currentY;

        public TouchInfo(int id, float x, float y) {
            this.id = id;
            this.startX = this.lastX = this.currentX = x;
            this.startY = this.lastY = this.currentY = y;
        }

        public void moved() {
            lastX = currentX;
            lastY = currentY;
        }
    }

    private static final float TAP_JITTER = 20f;

    private final float pixelScaling;
    private final Handler handler = new Handler();
    private final List<TouchInfo> touches = new ArrayList<TouchInfo>(4);
    private final GestureHandler gestureHandler;
    private final int minimumFlingVelocity;
    private final int maximumFlingVelocity;
    private VelocityTracker velocityTracker;

    private final Runnable onLongTouchHandler = new Runnable() {
        @Override
        public void run() {
            TouchInfo touch = touches.get(0);
            gestureHandler.onLongTouch(touch.startX, touch.startY);
        }
    };

    public GestureDetector(Context context, GestureHandler gestureHandler) {
        this.gestureHandler = gestureHandler;
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        pixelScaling = metrics.density;
        minimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity();
        maximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity();
    }

    public void onTouch(MotionEvent event) {
        int action = event.getActionMasked();
        int pointerIndex = event.getActionIndex();
        int pointerId = event.getPointerId(pointerIndex);
        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) {
            touches.add(new TouchInfo(pointerId, event.getX(pointerIndex), event.getY(pointerIndex)));
            gestureHandler.onStopFling();
        } else {
            for (int i = 0; i < event.getPointerCount(); ++i) {
                TouchInfo t = getTouch(event.getPointerId(i));
                t.currentX = event.getX(i);
                t.currentY = event.getY(i);
                if (!t.moved) {
                    float dx = t.currentX - t.startX;
                    float dy = t.currentY - t.startY;
                    float dist = (float) Math.sqrt(dx * dx + dy * dy);
                    float jitter = TAP_JITTER * pixelScaling;
                    if (dist > jitter) {
                        t.moved = true;
                        handler.removeCallbacks(onLongTouchHandler);

                        // consume initial
                        //t.startX = t.lastX = t.startX + dx * (dist - jitter);
                        //t.startY = t.lastY = t.startY + dy * (dist - jitter);
                    }
                }
            }
        }

        if (action == MotionEvent.ACTION_DOWN && touches.size() == 1) {
            handler.postDelayed(onLongTouchHandler, 600);
            velocityTracker = null;
        }

        if (action == MotionEvent.ACTION_POINTER_DOWN
                || (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP)
                && touches.get(0).id == pointerId) {
            handler.removeCallbacks(onLongTouchHandler);
        }

        if (action == MotionEvent.ACTION_MOVE) {
            if (touches.size() == 2) {
                velocityTracker = null;
                TouchInfo t0 = touches.get(0);
                TouchInfo t1 = touches.get(1);
                if (t0.moved || t1.moved) {
                    float oldDx = t1.lastX - t0.lastX;
                    float oldDy = t1.lastY - t0.lastY;
                    float oldD = (float) Math.sqrt(oldDx * oldDx + oldDy * oldDy);
                    float dx = t1.currentX - t0.currentX;
                    float dy = t1.currentY - t0.currentY;
                    float d = (float) Math.sqrt(dx * dx + dy * dy);
                    gestureHandler.onPinchZoom(t0.currentX + dx / 2, t0.currentY + dy / 2, d - oldD);
                    t0.moved();
                    t1.moved();
                }
            } else if (touches.size() == 1) {
                if (velocityTracker == null) {
                    velocityTracker = VelocityTracker.obtain();
                }
                velocityTracker.addMovement(event);
                TouchInfo t0 = touches.get(0);
                if (t0.moved) {
                    gestureHandler.onTouchMove(t0.currentX - t0.lastX, t0.currentY - t0.lastY);
                    t0.moved();
                }
            }
        }

        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) {
            for (int i=0; i < touches.size(); ++i) {
                if (touches.get(i).id == pointerId) {
                    touches.remove(i);
                    break;
                }
            }

            if (velocityTracker != null) {
                velocityTracker.computeCurrentVelocity(1000, maximumFlingVelocity);
                final float velocityY = velocityTracker.getYVelocity();
                final float velocityX = velocityTracker.getXVelocity();
                if (Math.abs(velocityY) > minimumFlingVelocity || Math.abs(velocityX) > minimumFlingVelocity) {
                    gestureHandler.onFling(velocityX, velocityY);

                }
                velocityTracker = null;
            }
        }
    }


    private TouchInfo getTouch(int pointerId) {
        for (int i = 0; i < touches.size(); ++i) {
            if (touches.get(i).id == pointerId) {
                return touches.get(i);
            }
        }
        return null;
    }
}




Java Source Code List

com.remoteyourcam.usb.AbstractRycApplication.java
com.remoteyourcam.usb.AppConfig.java
com.remoteyourcam.usb.AppSettings.java
com.remoteyourcam.usb.GestureDetector.java
com.remoteyourcam.usb.MainActivity.java
com.remoteyourcam.usb.PictureView.java
com.remoteyourcam.usb.PropertyAdapter.java
com.remoteyourcam.usb.PropertyData.java
com.remoteyourcam.usb.PropertyDisplayer.java
com.remoteyourcam.usb.PropertyToggle.java
com.remoteyourcam.usb.RycApplication.java
com.remoteyourcam.usb.activities.AppSettingsActivity.java
com.remoteyourcam.usb.ptp.Camera.java
com.remoteyourcam.usb.ptp.EosCamera.java
com.remoteyourcam.usb.ptp.EosConstants.java
com.remoteyourcam.usb.ptp.FocusPoint.java
com.remoteyourcam.usb.ptp.NikonCamera.java
com.remoteyourcam.usb.ptp.PacketUtil.java
com.remoteyourcam.usb.ptp.PtpAction.java
com.remoteyourcam.usb.ptp.PtpCamera.java
com.remoteyourcam.usb.ptp.PtpConstants.java
com.remoteyourcam.usb.ptp.PtpPropertyHelper.java
com.remoteyourcam.usb.ptp.PtpService.java
com.remoteyourcam.usb.ptp.PtpUsbConnection.java
com.remoteyourcam.usb.ptp.PtpUsbService.java
com.remoteyourcam.usb.ptp.WorkerNotifier.java
com.remoteyourcam.usb.ptp.commands.CloseSessionCommand.java
com.remoteyourcam.usb.ptp.commands.Command.java
com.remoteyourcam.usb.ptp.commands.GetDeviceInfoCommand.java
com.remoteyourcam.usb.ptp.commands.GetDevicePropDescCommand.java
com.remoteyourcam.usb.ptp.commands.GetDevicePropValueCommand.java
com.remoteyourcam.usb.ptp.commands.GetObjectCommand.java
com.remoteyourcam.usb.ptp.commands.GetObjectHandlesCommand.java
com.remoteyourcam.usb.ptp.commands.GetObjectInfoCommand.java
com.remoteyourcam.usb.ptp.commands.GetStorageIdsCommand.java
com.remoteyourcam.usb.ptp.commands.GetStorageInfoCommand.java
com.remoteyourcam.usb.ptp.commands.GetStorageInfosAction.java
com.remoteyourcam.usb.ptp.commands.GetThumb.java
com.remoteyourcam.usb.ptp.commands.InitiateCaptureCommand.java
com.remoteyourcam.usb.ptp.commands.OpenSessionCommand.java
com.remoteyourcam.usb.ptp.commands.RetrieveAddedObjectInfoAction.java
com.remoteyourcam.usb.ptp.commands.RetrieveImageAction.java
com.remoteyourcam.usb.ptp.commands.RetrieveImageInfoAction.java
com.remoteyourcam.usb.ptp.commands.RetrievePictureAction.java
com.remoteyourcam.usb.ptp.commands.SetDevicePropValueCommand.java
com.remoteyourcam.usb.ptp.commands.SimpleCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosEventCheckCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosGetLiveViewPictureCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosOpenSessionAction.java
com.remoteyourcam.usb.ptp.commands.eos.EosSetExtendedEventInfoCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosSetLiveViewAction.java
com.remoteyourcam.usb.ptp.commands.eos.EosSetPcModeCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosSetPropertyCommand.java
com.remoteyourcam.usb.ptp.commands.eos.EosTakePictureCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonAfDriveCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonAfDriveDeviceReadyCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonCloseSessionAction.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonEventCheckCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonGetLiveViewImageAction.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonGetLiveViewImageCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonGetVendorPropCodesCommand.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonOpenSessionAction.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonStartLiveViewAction.java
com.remoteyourcam.usb.ptp.commands.nikon.NikonStopLiveViewAction.java
com.remoteyourcam.usb.ptp.model.DeviceInfo.java
com.remoteyourcam.usb.ptp.model.DevicePropDesc.java
com.remoteyourcam.usb.ptp.model.LiveViewData.java
com.remoteyourcam.usb.ptp.model.ObjectInfo.java
com.remoteyourcam.usb.ptp.model.StorageInfo.java
com.remoteyourcam.usb.util.DimenUtil.java
com.remoteyourcam.usb.util.NotificationIds.java
com.remoteyourcam.usb.util.PackageUtil.java
com.remoteyourcam.usb.view.AspectRatioImageView.java
com.remoteyourcam.usb.view.BaseFragment.java
com.remoteyourcam.usb.view.GalleryAdapter.java
com.remoteyourcam.usb.view.GalleryFragment.java
com.remoteyourcam.usb.view.PictureFragment.java
com.remoteyourcam.usb.view.SessionActivity.java
com.remoteyourcam.usb.view.SessionFragment.java
com.remoteyourcam.usb.view.SessionView.java
com.remoteyourcam.usb.view.StorageAdapter.java
com.remoteyourcam.usb.view.TabletSessionFragment.java
com.remoteyourcam.usb.view.ThumbnailAdapter.java
com.remoteyourcam.usb.view.WebViewDialogFragment.java