Example usage for android.view ScaleGestureDetector getCurrentSpan

List of usage examples for android.view ScaleGestureDetector getCurrentSpan

Introduction

In this page you can find the example usage for android.view ScaleGestureDetector getCurrentSpan.

Prototype

public float getCurrentSpan() 

Source Link

Document

Return the average distance between each of the pointers forming the gesture in progress through the focal point.

Usage

From source file:com.maxwen.wallpaper.board.fragments.BaseFragment.java

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ViewHelper.resetViewBottomPadding(mRecyclerView, ((WallpaperBoardActivity) getActivity()).getNavBarHeight(),
            true);/*  w ww .j  a  va 2 s.  c  om*/
    setHasOptionsMenu(true);

    mDefaultSpan = getActivity().getResources().getInteger(R.integer.wallpapers_column_count);
    mMaxSpan = getActivity().getResources().getInteger(R.integer.wallpapers_max_column_count);
    mMinSpan = getActivity().getResources().getInteger(R.integer.wallpapers_min_column_count);
    mCurrentSpan = Math.min(Preferences.getPreferences(getActivity()).getColumnSpanCount(mDefaultSpan),
            mMaxSpan);

    //mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setItemAnimator(new SlideInLeftAnimator());
    mRecyclerView.setLayoutManager(new WallpaperGridLayoutManager(getActivity(), mCurrentSpan));
    mRecyclerView.setHasFixedSize(false);

    //set scale gesture detector
    mScaleGestureDetector = new ScaleGestureDetector(getActivity(),
            new ScaleGestureDetector.SimpleOnScaleGestureListener() {
                @Override
                public boolean onScale(ScaleGestureDetector detector) {
                    final float sf = detector.getScaleFactor();
                    if (detector.getTimeDelta() > 200
                            && Math.abs(detector.getCurrentSpan() - detector.getPreviousSpan()) > 100) {
                        if (detector.getCurrentSpan() - detector.getPreviousSpan() < -1) {
                            int span = Math.min(mCurrentSpan + 1, mMaxSpan);
                            if (span != mCurrentSpan) {
                                mCurrentSpan = span;
                                ViewHelper.setSpanCountToColumns(getActivity(), mRecyclerView, mCurrentSpan);
                                Preferences.getPreferences(getActivity()).setColumnSpanCount(mCurrentSpan);
                                onSpanCountChanged();
                            }
                            return true;
                        } else if (detector.getCurrentSpan() - detector.getPreviousSpan() > 1) {
                            int span = Math.max(mCurrentSpan - 1, mMinSpan);
                            if (span != mCurrentSpan) {
                                mCurrentSpan = span;
                                ViewHelper.setSpanCountToColumns(getActivity(), mRecyclerView, mCurrentSpan);
                                Preferences.getPreferences(getActivity()).setColumnSpanCount(mCurrentSpan);
                                onSpanCountChanged();
                            }
                            ViewHelper.setSpanCountToColumns(getActivity(), mRecyclerView, mCurrentSpan);
                            return true;
                        }
                    }
                    return false;
                }

                @Override
                public void onScaleEnd(ScaleGestureDetector detector) {
                    mRecyclerView.setNestedScrollingEnabled(true);
                }

                @Override
                public boolean onScaleBegin(ScaleGestureDetector detector) {
                    mRecyclerView.setNestedScrollingEnabled(false);
                    return true;
                }
            });
    mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mScaleGestureDetector.onTouchEvent(event);
            return mScaleGestureDetector.isInProgress();
        }
    });
}

From source file:org.mozilla.gecko.ui.PanZoomController.java

@Override
public boolean onScale(ScaleGestureDetector detector) {
    mState = PanZoomState.PINCHING;//from   w ww .j a  va 2s .c  o m
    float newZoom = detector.getCurrentSpan() / mInitialZoomSpan;

    IntSize screenSize = mController.getScreenSize();
    float x = mInitialZoomFocus.x - (detector.getFocusX() / newZoom);
    float y = mInitialZoomFocus.y - (detector.getFocusY() / newZoom);
    float width = screenSize.width / newZoom;
    float height = screenSize.height / newZoom;
    mController.setVisibleRect(x, y, width, height);
    mController.notifyLayerClientOfGeometryChange();
    populatePositionAndLength();
    return true;
}

From source file:com.gmail.walles.johan.batterylogger.BatteryPlotFragment.java

private ScaleGestureDetector getTwoFingerGestureDetector(final XYPlot plot) {
    ScaleGestureDetector.SimpleOnScaleGestureListener gestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        @Override// w w w .j a  v a  2s  .  c o  m
        public boolean onScale(ScaleGestureDetector detector) {
            float factor = detector.getPreviousSpan() / detector.getCurrentSpan();
            float pixelX = detector.getFocusX();
            RectF gridRect = plot.getGraphWidget().getGridRect();
            // getXVal throws IAE if the X value is outside of the rectangle
            if (gridRect.contains(pixelX, gridRect.top)) {
                double pivot = plot.getGraphWidget().getXVal(pixelX);
                zoom(factor, pivot);
            }

            plot.setDomainBoundaries(minX, maxX, BoundaryMode.FIXED);
            redrawPlot(plot);
            return true;
        }
    };

    return new ScaleGestureDetector(getActivity(), gestureListener);
}

From source file:org.mozilla.gecko.ui.PanZoomController.java

@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
    mState = PanZoomState.PINCHING;/*  w ww .  j  a v  a 2s. c om*/
    FloatRect initialZoomRect = mController.getVisibleRect();
    float initialZoom = mController.getZoomFactor();

    mInitialZoomFocus = new FloatPoint(initialZoomRect.x + (detector.getFocusX() / initialZoom),
            initialZoomRect.y + (detector.getFocusY() / initialZoom));
    mInitialZoomSpan = detector.getCurrentSpan() / initialZoom;
    return true;
}

From source file:com.example.nitish.welcomapp.widgetpt.PeriodicTableView.java

/**
 * Create the listener for the ScaleGestureDetector.
 *
 * @return The OnScaleGestureListener//w  w w . ja v a  2 s  .c o  m
 */
@NonNull
private ScaleGestureDetector.OnScaleGestureListener getOnScaleGestureListener() {
    return new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        /**
         * The initial span of the scale gesture
         */
        private float mStartSpan;

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            clearSelection();
            mScaleRect.set(mContentRect);
            mStartSpan = detector.getCurrentSpan();

            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFocalPoint.set(detector.getFocusX() / getWidth(), detector.getFocusY() / getHeight());
            setZoom(mCurrentZoom
                    + mCurrentZoom * (detector.getCurrentSpan() - detector.getPreviousSpan()) / mStartSpan);

            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            if (mPeriodicTableListener != null) {
                mPeriodicTableListener.onZoomEnd(PeriodicTableView.this);
            }
        }
    };
}

From source file:com.ultramegatech.ey.widget.PeriodicTableView.java

/**
 * Create the listener for the ScaleGestureDetector.
 *
 * @return The OnScaleGestureListener//w ww .jav a2s  .  c  om
 */
@NonNull
private ScaleGestureDetector.OnScaleGestureListener getOnScaleGestureListener() {
    //noinspection MethodDoesntCallSuperMethod
    return new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        /**
         * The initial span of the scale gesture
         */
        private float mStartSpan;

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            clearSelection();
            mScaleRect.set(mContentRect);
            mStartSpan = detector.getCurrentSpan();

            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFocalPoint.set(detector.getFocusX() / getWidth(), detector.getFocusY() / getHeight());
            setZoom(mCurrentZoom
                    + mCurrentZoom * (detector.getCurrentSpan() - detector.getPreviousSpan()) / mStartSpan);

            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            if (mPeriodicTableListener != null) {
                mPeriodicTableListener.onZoomEnd(PeriodicTableView.this);
            }
        }
    };
}

From source file:org.appcelerator.titanium.view.TiUIView.java

protected void registerTouchEvents(final View touchable) {

    touchView = new WeakReference<View>(touchable);

    final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(touchable.getContext(),
            new SimpleOnScaleGestureListener() {
                // protect from divide by zero errors
                long minTimeDelta = 1;
                float minStartSpan = 1.0f;
                float startSpan;

                @Override//from w  w w  .j  av a 2 s . c o m
                public boolean onScale(ScaleGestureDetector sgd) {
                    if (proxy.hierarchyHasListener(TiC.EVENT_PINCH)) {
                        float timeDelta = sgd.getTimeDelta() == 0 ? minTimeDelta : sgd.getTimeDelta();

                        // Suppress scale events (and allow for possible two-finger tap events)
                        // until we've moved at least a few pixels. Without this check, two-finger 
                        // taps are very hard to register on some older devices.
                        if (!didScale) {
                            if (Math.abs(sgd.getCurrentSpan() - startSpan) > SCALE_THRESHOLD) {
                                didScale = true;
                            }
                        }

                        if (didScale) {
                            KrollDict data = new KrollDict();
                            data.put(TiC.EVENT_PROPERTY_SCALE, sgd.getCurrentSpan() / startSpan);
                            data.put(TiC.EVENT_PROPERTY_VELOCITY,
                                    (sgd.getScaleFactor() - 1.0f) / timeDelta * 1000);
                            data.put(TiC.EVENT_PROPERTY_SOURCE, proxy);

                            return proxy.fireEvent(TiC.EVENT_PINCH, data);
                        }
                    }
                    return false;
                }

                @Override
                public boolean onScaleBegin(ScaleGestureDetector sgd) {
                    startSpan = sgd.getCurrentSpan() == 0 ? minStartSpan : sgd.getCurrentSpan();
                    return true;
                }
            });

    final GestureDetector detector = new GestureDetector(touchable.getContext(), new SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if (proxy.hierarchyHasListener(TiC.EVENT_DOUBLE_TAP)
                    || proxy.hierarchyHasListener(TiC.EVENT_DOUBLE_CLICK)) {
                boolean handledTap = proxy.fireEvent(TiC.EVENT_DOUBLE_TAP, dictFromEvent(e));
                boolean handledClick = proxy.fireEvent(TiC.EVENT_DOUBLE_CLICK, dictFromEvent(e));
                return handledTap || handledClick;
            }
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.d(TAG, "TAP, TAP, TAP on " + proxy, Log.DEBUG_MODE);
            if (proxy.hierarchyHasListener(TiC.EVENT_SINGLE_TAP)) {
                return proxy.fireEvent(TiC.EVENT_SINGLE_TAP, dictFromEvent(e));
                // Moved click handling to the onTouch listener, because a single tap is not the
                // same as a click. A single tap is a quick tap only, whereas clicks can be held
                // before lifting.
                // boolean handledClick = proxy.fireEvent(TiC.EVENT_CLICK, dictFromEvent(event));
                // Note: this return value is irrelevant in our case. We "want" to use it
                // in onTouch below, when we call detector.onTouchEvent(event); But, in fact,
                // onSingleTapConfirmed is *not* called in the course of onTouchEvent. It's
                // called via Handler in GestureDetector. <-- See its Java source.
                // return handledTap;// || handledClick;
            }
            return false;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.d(TAG, "SWIPE on " + proxy, Log.DEBUG_MODE);
            if (proxy.hierarchyHasListener(TiC.EVENT_SWIPE)) {
                KrollDict data = dictFromEvent(e2);
                if (Math.abs(velocityX) > Math.abs(velocityY)) {
                    data.put(TiC.EVENT_PROPERTY_DIRECTION, velocityX > 0 ? "right" : "left");
                } else {
                    data.put(TiC.EVENT_PROPERTY_DIRECTION, velocityY > 0 ? "down" : "up");
                }
                return proxy.fireEvent(TiC.EVENT_SWIPE, data);
            }
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            Log.d(TAG, "LONGPRESS on " + proxy, Log.DEBUG_MODE);

            if (proxy.hierarchyHasListener(TiC.EVENT_LONGPRESS)) {
                proxy.fireEvent(TiC.EVENT_LONGPRESS, dictFromEvent(e));
            }
        }
    });

    touchable.setOnTouchListener(new OnTouchListener() {
        int pointersDown = 0;

        public boolean onTouch(View view, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                lastUpEvent.put(TiC.EVENT_PROPERTY_X, (double) event.getX());
                lastUpEvent.put(TiC.EVENT_PROPERTY_Y, (double) event.getY());
            }

            scaleDetector.onTouchEvent(event);
            if (scaleDetector.isInProgress()) {
                pointersDown = 0;
                return true;
            }

            boolean handled = detector.onTouchEvent(event);
            if (handled) {
                pointersDown = 0;
                return true;
            }

            if (event.getActionMasked() == MotionEvent.ACTION_POINTER_UP) {
                if (didScale) {
                    didScale = false;
                    pointersDown = 0;
                } else {
                    pointersDown++;
                }
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                if (pointersDown == 1) {
                    proxy.fireEvent(TiC.EVENT_TWOFINGERTAP, dictFromEvent(event));
                    pointersDown = 0;
                    return true;
                }
                pointersDown = 0;
            }

            String motionEvent = motionEvents.get(event.getAction());
            if (motionEvent != null) {
                if (proxy.hierarchyHasListener(motionEvent)) {
                    proxy.fireEvent(motionEvent, dictFromEvent(event));
                }
            }

            // Inside View.java, dispatchTouchEvent() does not call onTouchEvent() if this listener returns true. As
            // a result, click and other motion events do not occur on the native Android side. To prevent this, we
            // always return false and let Android generate click and other motion events.
            return false;
        }
    });

}

From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java

public boolean onScale(ScaleGestureDetector detector) {
    float spanY = Math.max(MIN_Y_SPAN, Math.abs(detector.getCurrentSpan()));//TODO .getCurrentSpanY()

    mCellHeight = (int) (mCellHeightBeforeScaleGesture * spanY / mStartingSpanY);

    if (mCellHeight < mMinCellHeight) {
        // If mStartingSpanY is too small, even a small increase in the
        // gesture can bump the mCellHeight beyond MAX_CELL_HEIGHT
        mStartingSpanY = spanY;/*from w  w w .j  a  v a 2  s.  c o  m*/
        mCellHeight = mMinCellHeight;
        mCellHeightBeforeScaleGesture = mMinCellHeight;
    } else if (mCellHeight > MAX_CELL_HEIGHT) {
        mStartingSpanY = spanY;
        mCellHeight = MAX_CELL_HEIGHT;
        mCellHeightBeforeScaleGesture = MAX_CELL_HEIGHT;
    }

    int gestureCenterInPixels = (int) detector.getFocusY() - DAY_HEADER_HEIGHT;
    mViewStartY = (int) (mGestureCenterHour * (mCellHeight + DAY_GAP)) - gestureCenterInPixels;
    mMaxViewStartY = HOUR_GAP + 24 * (mCellHeight + HOUR_GAP) - mGridAreaHeight;

    if (mViewStartY < 0) {
        mViewStartY = 0;
        mGestureCenterHour = (mViewStartY + gestureCenterInPixels) / (float) (mCellHeight + DAY_GAP);
    } else if (mViewStartY > mMaxViewStartY) {
        mViewStartY = mMaxViewStartY;
        mGestureCenterHour = (mViewStartY + gestureCenterInPixels) / (float) (mCellHeight + DAY_GAP);
    }
    computeFirstHour();

    mRemeasure = true;
    invalidate();
    return true;
}

From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java

public boolean onScaleBegin(ScaleGestureDetector detector) {
    mHandleActionUp = false;/*from w  ww .ja  va2 s.com*/
    float gestureCenterInPixels = detector.getFocusY() - DAY_HEADER_HEIGHT;
    mGestureCenterHour = (mViewStartY + gestureCenterInPixels) / (mCellHeight + DAY_GAP);

    mStartingSpanY = Math.max(MIN_Y_SPAN, Math.abs(detector.getCurrentSpan())); //TODO .getCurrentSpanY()
    mCellHeightBeforeScaleGesture = mCellHeight;

    return true;
}