com.xgleng.androiddemo.touchscreen.CommonGestures.java Source code

Java tutorial

Introduction

Here is the source code for com.xgleng.androiddemo.touchscreen.CommonGestures.java

Source

/*
 * Copyright (C) 2012 YIXIA.COM
 *
 * 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.xgleng.androiddemo.touchscreen;

import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

public class CommonGestures {
    private float mDownX, mDownY;
    private boolean isVerticalScroll;

    private long mDownTime;
    private boolean mIsScrolling = false;
    private boolean mIsFristScroll = true;

    private GestureDetectorCompat mTapGestureDetector;
    private ScaleGestureDetector mScaleDetector;
    private TouchListener mListener;
    private Context mContext;

    public CommonGestures(Context ctx) {
        mContext = ctx;
        mTapGestureDetector = new GestureDetectorCompat(mContext, new TapGestureListener());
        mScaleDetector = new ScaleGestureDetector(mContext, new ScaleDetectorListener());
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (mListener == null)
            return false;

        if (mTapGestureDetector.onTouchEvent(event))
            return true;

        if (event.getPointerCount() > 1) {
            if (mScaleDetector != null && mScaleDetector.onTouchEvent(event)) {
                return true;
            }
        }

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_UP:
            mIsFristScroll = true;
            if (mIsScrolling) {
                mListener.onGestureEnd();
                if (Math.abs(event.getY() - mDownY) > Math.abs(event.getX() - mDownX)) {
                    mListener.onVerticalScrollEnd();
                } else {
                    mListener.onHorizontalScrollEnd();
                }
            }
            break;
        }
        return false;
    }

    private class ScaleDetectorListener implements ScaleGestureDetector.OnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            if (mListener != null)
                mListener.onScale(detector.getScaleFactor());
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            if (mListener != null)
                mListener.onScale(0F);
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            if (mListener != null)
                mListener.onScale(0F);
            return true;
        }
    }

    private class TapGestureListener extends SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent event) {
            if (mListener != null) {
                mListener.onGestureBegin();
                mIsScrolling = false;
                mDownTime = System.currentTimeMillis();
                mDownX = event.getX();
                mDownY = event.getY();
            }
            return super.onDown(event);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            if (mListener != null)
                mListener.onSingleTap();
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            if (mListener != null)
                mListener.onLongPress();
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if ((System.currentTimeMillis() - mDownTime) < 100) {
                return super.onScroll(e1, e2, distanceX, distanceY);
            }

            if (mListener != null) {
                mIsScrolling = true;
                if (!mIsFristScroll) {
                    if (isVerticalScroll) {
                        mListener.onVerticalSlide(distanceY);
                    } else {
                        mListener.onHorizontalSlide(distanceX);

                    }

                    return super.onScroll(e1, e2, distanceX, distanceY);
                }

                if ((Math.abs(e2.getY() - e1.getY()) * 2 > Math.abs(e2.getX() - e1.getX()))) {
                    isVerticalScroll = true;
                    mListener.onVerticalScrollBegin();
                } else {
                    isVerticalScroll = false;
                    mListener.onHorizontalScrollBegin();
                }
                mIsFristScroll = false;
            }
            return super.onScroll(e1, e2, distanceX, distanceY);
        }

        @Override
        public boolean onDoubleTap(MotionEvent event) {
            if (mListener != null)
                mListener.onDoubleTap();
            return super.onDoubleTap(event);
        }
    }

    public void setTouchListener(TouchListener l) {
        mListener = l;
    }

    public interface TouchListener {

        /**
         * Gesture begin
         */
        public void onGestureBegin();

        /**
         * Gesture end
         */
        public void onGestureEnd();

        /**
         * single tap
         */
        public void onSingleTap();

        /**
         * double tap
         */
        public void onDoubleTap();

        /**
         * scroll
         * 
         * @param scaleFactor
         *            The current scaling factor
         */
        public void onScale(float scaleFactor);

        /**
         * long press
         */
        public void onLongPress();

        /**
         * the horizontal scroll begin
         */
        public void onHorizontalScrollBegin();

        /**
         * scroll horizontally
         * 
         * @param distance
         *            The distance along the X axis that has been scrolled since
         *            the last call to onHorizontalSlide
         */
        public void onHorizontalSlide(float distance);

        /**
         * the horizontal scroll end
         */
        public void onHorizontalScrollEnd();

        /**
         * the vertical scroll begin
         */
        public void onVerticalScrollBegin();

        /**
         * scroll vertically
         * 
         * @param distance
         *            The distance along the Y axis that has been scrolled since
         *            the last call to onVerticalSlide
         */
        public void onVerticalSlide(float distance);

        /**
         * the vertical scroll end
         */
        public void onVerticalScrollEnd();

    }
}