Search in sources :

Example 16 with ScaleGestureDetector

use of android.view.ScaleGestureDetector in project android-vision by googlesamples.

the class OcrCaptureActivity method onCreate.

/**
     * Initializes the UI and creates the detector pipeline.
     */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.ocr_capture);
    mPreview = (CameraSourcePreview) findViewById(R.id.preview);
    mGraphicOverlay = (GraphicOverlay<OcrGraphic>) findViewById(R.id.graphicOverlay);
    // read parameters from the intent used to launch the activity.
    boolean autoFocus = getIntent().getBooleanExtra(AutoFocus, false);
    boolean useFlash = getIntent().getBooleanExtra(UseFlash, false);
    // Check for the camera permission before accessing the camera.  If the
    // permission is not granted yet, request permission.
    int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
    if (rc == PackageManager.PERMISSION_GRANTED) {
        createCameraSource(autoFocus, useFlash);
    } else {
        requestCameraPermission();
    }
    gestureDetector = new GestureDetector(this, new CaptureGestureListener());
    scaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());
    Snackbar.make(mGraphicOverlay, "Tap to capture. Pinch/Stretch to zoom", Snackbar.LENGTH_LONG).show();
}
Also used : GestureDetector(android.view.GestureDetector) ScaleGestureDetector(android.view.ScaleGestureDetector) ScaleGestureDetector(android.view.ScaleGestureDetector) SuppressLint(android.annotation.SuppressLint)

Example 17 with ScaleGestureDetector

use of android.view.ScaleGestureDetector in project android_frameworks_base by DirtyUnicorns.

the class OverlayDisplayWindow method createWindow.

private void createWindow() {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    mWindowContent = inflater.inflate(com.android.internal.R.layout.overlay_display_window, null);
    mWindowContent.setOnTouchListener(mOnTouchListener);
    mTextureView = (TextureView) mWindowContent.findViewById(com.android.internal.R.id.overlay_display_window_texture);
    mTextureView.setPivotX(0);
    mTextureView.setPivotY(0);
    mTextureView.getLayoutParams().width = mWidth;
    mTextureView.getLayoutParams().height = mHeight;
    mTextureView.setOpaque(false);
    mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
    mTitleTextView = (TextView) mWindowContent.findViewById(com.android.internal.R.id.overlay_display_window_title);
    mTitleTextView.setText(mTitle);
    mWindowParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_DISPLAY_OVERLAY);
    mWindowParams.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
    if (mSecure) {
        mWindowParams.flags |= WindowManager.LayoutParams.FLAG_SECURE;
    }
    if (DISABLE_MOVE_AND_RESIZE) {
        mWindowParams.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
    }
    mWindowParams.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED;
    mWindowParams.alpha = WINDOW_ALPHA;
    mWindowParams.gravity = Gravity.TOP | Gravity.LEFT;
    mWindowParams.setTitle(mTitle);
    mGestureDetector = new GestureDetector(mContext, mOnGestureListener);
    mScaleGestureDetector = new ScaleGestureDetector(mContext, mOnScaleGestureListener);
    // Set the initial position and scale.
    // The position and scale will be clamped when the display is first shown.
    mWindowX = (mGravity & Gravity.LEFT) == Gravity.LEFT ? 0 : mDefaultDisplayInfo.logicalWidth;
    mWindowY = (mGravity & Gravity.TOP) == Gravity.TOP ? 0 : mDefaultDisplayInfo.logicalHeight;
    mWindowScale = INITIAL_SCALE;
}
Also used : LayoutInflater(android.view.LayoutInflater) GestureDetector(android.view.GestureDetector) ScaleGestureDetector(android.view.ScaleGestureDetector) ScaleGestureDetector(android.view.ScaleGestureDetector) WindowManager(android.view.WindowManager)

Example 18 with ScaleGestureDetector

use of android.view.ScaleGestureDetector in project android_frameworks_base by ParanoidAndroid.

the class WebViewClassic method handleTouchEventCommon.

/*
    * Common code for single touch and multi-touch.
    * (x, y) denotes current focus point, which is the touch point for single touch
    * and the middle point for multi-touch.
    */
private void handleTouchEventCommon(MotionEvent event, int action, int x, int y) {
    ScaleGestureDetector detector = mZoomManager.getScaleGestureDetector();
    long eventTime = event.getEventTime();
    // Due to the touch screen edge effect, a touch closer to the edge
    // always snapped to the edge. As getViewWidth() can be different from
    // getWidth() due to the scrollbar, adjusting the point to match
    // getViewWidth(). Same applied to the height.
    x = Math.min(x, getViewWidth() - 1);
    y = Math.min(y, getViewHeightWithTitle() - 1);
    int deltaX = mLastTouchX - x;
    int deltaY = mLastTouchY - y;
    int contentX = viewToContentX(x + getScrollX());
    int contentY = viewToContentY(y + getScrollY());
    switch(action) {
        case MotionEvent.ACTION_DOWN:
            {
                mConfirmMove = false;
                // Channel Scrolling
                mFirstTouchX = x;
                mFirstTouchY = y;
                mDistanceX = mDistanceY = 0;
                if (!mEditTextScroller.isFinished()) {
                    mEditTextScroller.abortAnimation();
                }
                if (!mScroller.isFinished()) {
                    // stop the current scroll animation, but if this is
                    // the start of a fling, allow it to add to the current
                    // fling's velocity
                    mScroller.abortAnimation();
                    mTouchMode = TOUCH_DRAG_START_MODE;
                    mConfirmMove = true;
                    nativeSetIsScrolling(false);
                } else if (mPrivateHandler.hasMessages(RELEASE_SINGLE_TAP)) {
                    mPrivateHandler.removeMessages(RELEASE_SINGLE_TAP);
                    removeTouchHighlight();
                    if (deltaX * deltaX + deltaY * deltaY < mDoubleTapSlopSquare) {
                        mTouchMode = TOUCH_DOUBLE_TAP_MODE;
                    } else {
                        mTouchMode = TOUCH_INIT_MODE;
                    }
                } else {
                    // the normal case
                    mTouchMode = TOUCH_INIT_MODE;
                    if (mLogEvent && eventTime - mLastTouchUpTime < 1000) {
                        EventLog.writeEvent(EventLogTags.BROWSER_DOUBLE_TAP_DURATION, (eventTime - mLastTouchUpTime), eventTime);
                    }
                    mSelectionStarted = false;
                    if (mSelectingText) {
                        ensureSelectionHandles();
                        int shiftedY = y - getTitleHeight() + getScrollY();
                        int shiftedX = x + getScrollX();
                        if (mSelectHandleBaseBounds.contains(shiftedX, shiftedY)) {
                            mSelectionStarted = true;
                            mSelectDraggingCursor = mSelectCursorBase;
                            mSelectDraggingTextQuad = mSelectCursorBaseTextQuad;
                            if (mIsCaretSelection) {
                                mPrivateHandler.removeMessages(CLEAR_CARET_HANDLE);
                                hidePasteButton();
                            }
                        } else if (mSelectHandleExtentBounds.contains(shiftedX, shiftedY)) {
                            mSelectionStarted = true;
                            mSelectDraggingCursor = mSelectCursorExtent;
                            mSelectDraggingTextQuad = mSelectCursorExtentTextQuad;
                        } else if (mIsCaretSelection) {
                            selectionDone();
                        }
                        if (DebugFlags.WEB_VIEW) {
                            Log.v(LOGTAG, "select=" + contentX + "," + contentY);
                        }
                    }
                }
                // Trigger the link
                if (!mSelectingText && (mTouchMode == TOUCH_INIT_MODE || mTouchMode == TOUCH_DOUBLE_TAP_MODE)) {
                    mPrivateHandler.sendEmptyMessageDelayed(SWITCH_TO_SHORTPRESS, TAP_TIMEOUT);
                    mPrivateHandler.sendEmptyMessageDelayed(SWITCH_TO_LONGPRESS, LONG_PRESS_TIMEOUT);
                }
                startTouch(x, y, eventTime);
                if (mIsEditingText) {
                    mTouchInEditText = mEditTextContentBounds.contains(contentX, contentY);
                }
                break;
            }
        case MotionEvent.ACTION_MOVE:
            {
                if (!mConfirmMove && (deltaX * deltaX + deltaY * deltaY) >= mTouchSlopSquare) {
                    mPrivateHandler.removeMessages(SWITCH_TO_SHORTPRESS);
                    mPrivateHandler.removeMessages(SWITCH_TO_LONGPRESS);
                    mConfirmMove = true;
                    if (mTouchMode == TOUCH_DOUBLE_TAP_MODE) {
                        mTouchMode = TOUCH_INIT_MODE;
                    }
                    removeTouchHighlight();
                }
                if (mSelectingText && mSelectionStarted) {
                    if (DebugFlags.WEB_VIEW) {
                        Log.v(LOGTAG, "extend=" + contentX + "," + contentY);
                    }
                    ViewParent parent = mWebView.getParent();
                    if (parent != null) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                    if (deltaX != 0 || deltaY != 0) {
                        int handleX = contentX + viewToContentDimension(mSelectOffset.x);
                        int handleY = contentY + viewToContentDimension(mSelectOffset.y);
                        mSelectDraggingCursor.set(handleX, handleY);
                        boolean inCursorText = mSelectDraggingTextQuad.containsPoint(handleX, handleY);
                        boolean inEditBounds = mEditTextContentBounds.contains(handleX, handleY);
                        if (mIsEditingText && !inEditBounds) {
                            beginScrollEdit();
                        } else {
                            endScrollEdit();
                        }
                        boolean snapped = false;
                        if (inCursorText || (mIsEditingText && !inEditBounds)) {
                            snapDraggingCursor();
                            snapped = true;
                        }
                        updateWebkitSelection(snapped);
                        if (!inCursorText && mIsEditingText && inEditBounds) {
                            // Visually snap even if we have moved the handle.
                            snapDraggingCursor();
                        }
                        mLastTouchX = x;
                        mLastTouchY = y;
                        invalidate();
                    }
                    break;
                }
                if (mTouchMode == TOUCH_DONE_MODE) {
                    // default is yes
                    break;
                }
                if (mVelocityTracker == null) {
                    Log.e(LOGTAG, "Got null mVelocityTracker when " + " mTouchMode = " + mTouchMode);
                } else {
                    mVelocityTracker.addMovement(event);
                }
                if (mTouchMode != TOUCH_DRAG_MODE && mTouchMode != TOUCH_DRAG_LAYER_MODE && mTouchMode != TOUCH_DRAG_TEXT_MODE) {
                    if (!mConfirmMove) {
                        break;
                    }
                    if ((detector == null || !detector.isInProgress()) && SNAP_NONE == mSnapScrollMode) {
                        int ax = Math.abs(x - mFirstTouchX);
                        int ay = Math.abs(y - mFirstTouchY);
                        if (ax < SNAP_BOUND && ay < SNAP_BOUND) {
                            break;
                        } else if (ax < SNAP_BOUND) {
                            mSnapScrollMode = SNAP_Y;
                        } else if (ay < SNAP_BOUND) {
                            mSnapScrollMode = SNAP_X;
                        }
                    }
                    mTouchMode = TOUCH_DRAG_MODE;
                    mLastTouchX = x;
                    mLastTouchY = y;
                    deltaX = 0;
                    deltaY = 0;
                    startScrollingLayer(x, y);
                    startDrag();
                }
                // do pan
                boolean keepScrollBarsVisible = false;
                if (deltaX == 0 && deltaY == 0) {
                    keepScrollBarsVisible = true;
                } else {
                    if (mSnapScrollMode == SNAP_X || mSnapScrollMode == SNAP_Y) {
                        mDistanceX += Math.abs(deltaX);
                        mDistanceY += Math.abs(deltaY);
                        if (mSnapScrollMode == SNAP_X) {
                            if (mDistanceY > sChannelDistance) {
                                mSnapScrollMode = SNAP_NONE;
                            } else if (mDistanceX > sChannelDistance) {
                                mDistanceX = mDistanceY = 0;
                            }
                        } else {
                            if (mDistanceX > sChannelDistance) {
                                mSnapScrollMode = SNAP_NONE;
                            } else if (mDistanceY > sChannelDistance) {
                                mDistanceX = mDistanceY = 0;
                            }
                        }
                    }
                    if (mSnapScrollMode != SNAP_NONE) {
                        if ((mSnapScrollMode & SNAP_X) == SNAP_X) {
                            deltaY = 0;
                        } else {
                            deltaX = 0;
                        }
                    }
                    if (deltaX * deltaX + deltaY * deltaY > mTouchSlopSquare) {
                        mHeldMotionless = MOTIONLESS_FALSE;
                    } else {
                        mHeldMotionless = MOTIONLESS_TRUE;
                        keepScrollBarsVisible = true;
                    }
                    mLastTouchTime = eventTime;
                    boolean allDrag = doDrag(deltaX, deltaY);
                    if (allDrag) {
                        mLastTouchX = x;
                        mLastTouchY = y;
                    } else {
                        int contentDeltaX = (int) Math.floor(deltaX * mZoomManager.getInvScale());
                        int roundedDeltaX = contentToViewDimension(contentDeltaX);
                        int contentDeltaY = (int) Math.floor(deltaY * mZoomManager.getInvScale());
                        int roundedDeltaY = contentToViewDimension(contentDeltaY);
                        mLastTouchX -= roundedDeltaX;
                        mLastTouchY -= roundedDeltaY;
                    }
                }
                break;
            }
        case MotionEvent.ACTION_UP:
            {
                mFirstTouchX = mFirstTouchY = -1;
                if (mIsEditingText && mSelectionStarted) {
                    endScrollEdit();
                    mPrivateHandler.sendEmptyMessageDelayed(SCROLL_HANDLE_INTO_VIEW, TEXT_SCROLL_FIRST_SCROLL_MS);
                    if (!mConfirmMove && mIsCaretSelection) {
                        showPasteWindow();
                        stopTouch();
                        break;
                    }
                }
                mLastTouchUpTime = eventTime;
                if (mSentAutoScrollMessage) {
                    mAutoScrollX = mAutoScrollY = 0;
                }
                switch(mTouchMode) {
                    case // double tap
                    TOUCH_DOUBLE_TAP_MODE:
                        mPrivateHandler.removeMessages(SWITCH_TO_SHORTPRESS);
                        mPrivateHandler.removeMessages(SWITCH_TO_LONGPRESS);
                        mTouchMode = TOUCH_DONE_MODE;
                        break;
                    // tap
                    case TOUCH_INIT_MODE:
                    case TOUCH_SHORTPRESS_START_MODE:
                    case TOUCH_SHORTPRESS_MODE:
                        mPrivateHandler.removeMessages(SWITCH_TO_SHORTPRESS);
                        mPrivateHandler.removeMessages(SWITCH_TO_LONGPRESS);
                        if (!mConfirmMove) {
                            if (mSelectingText) {
                                // tapping on selection or controls does nothing
                                if (!mSelectionStarted) {
                                    selectionDone();
                                }
                                break;
                            }
                            // scalable
                            if (mTouchMode == TOUCH_INIT_MODE && (canZoomIn() || canZoomOut())) {
                                mPrivateHandler.sendEmptyMessageDelayed(RELEASE_SINGLE_TAP, ViewConfiguration.getDoubleTapTimeout());
                            }
                            break;
                        }
                    case TOUCH_DRAG_MODE:
                    case TOUCH_DRAG_LAYER_MODE:
                    case TOUCH_DRAG_TEXT_MODE:
                        mPrivateHandler.removeMessages(DRAG_HELD_MOTIONLESS);
                        // up, we don't want to do a fling
                        if (eventTime - mLastTouchTime <= MIN_FLING_TIME) {
                            if (mVelocityTracker == null) {
                                Log.e(LOGTAG, "Got null mVelocityTracker");
                            } else {
                                mVelocityTracker.addMovement(event);
                            }
                            // set to MOTIONLESS_IGNORE so that it won't keep
                            // removing and sending message in
                            // drawCoreAndCursorRing()
                            mHeldMotionless = MOTIONLESS_IGNORE;
                            doFling();
                            break;
                        } else {
                            if (mScroller.springBack(getScrollX(), getScrollY(), 0, computeMaxScrollX(), 0, computeMaxScrollY())) {
                                invalidate();
                            }
                        }
                        // redraw in high-quality, as we're done dragging
                        mHeldMotionless = MOTIONLESS_TRUE;
                        invalidate();
                    // fall through
                    case TOUCH_DRAG_START_MODE:
                        // TOUCH_DRAG_START_MODE should not happen for the real
                        // device as we almost certain will get a MOVE. But this
                        // is possible on emulator.
                        mLastVelocity = 0;
                        WebViewCore.resumePriority();
                        if (!mSelectingText) {
                            WebViewCore.resumeUpdatePicture(mWebViewCore);
                        }
                        break;
                }
                stopTouch();
                break;
            }
        case MotionEvent.ACTION_CANCEL:
            {
                if (mTouchMode == TOUCH_DRAG_MODE) {
                    mScroller.springBack(getScrollX(), getScrollY(), 0, computeMaxScrollX(), 0, computeMaxScrollY());
                    invalidate();
                }
                cancelTouch();
                break;
            }
    }
}
Also used : ViewParent(android.view.ViewParent) ScaleGestureDetector(android.view.ScaleGestureDetector) Paint(android.graphics.Paint) Point(android.graphics.Point)

Example 19 with ScaleGestureDetector

use of android.view.ScaleGestureDetector in project android_frameworks_base by ParanoidAndroid.

the class WebViewClassic method performLongClick.

@Override
public boolean performLongClick() {
    // view system. In that case, do nothing.
    if (mWebView.getParent() == null)
        return false;
    // A multi-finger gesture can look like a long press; make sure we don't take
    // long press actions if we're scaling.
    final ScaleGestureDetector detector = mZoomManager.getScaleGestureDetector();
    if (detector != null && detector.isInProgress()) {
        return false;
    }
    // long click does nothing on selection
    if (mSelectingText)
        return false;
    /* if long click brings up a context menu, the super function
         * returns true and we're done. Otherwise, nothing happened when
         * the user clicked. */
    if (mWebViewPrivate.super_performLongClick()) {
        return true;
    }
    /* In the case where the application hasn't already handled the long
         * click action, look for a word under the  click. If one is found,
         * animate the text selection into view.
         * FIXME: no animation code yet */
    final boolean isSelecting = selectText();
    if (isSelecting) {
        mWebView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
    } else if (focusCandidateIsEditableText()) {
        mSelectCallback = new SelectActionModeCallback();
        mSelectCallback.setWebView(this);
        mSelectCallback.setTextSelected(false);
        mWebView.startActionMode(mSelectCallback);
    }
    return isSelecting;
}
Also used : ScaleGestureDetector(android.view.ScaleGestureDetector)

Example 20 with ScaleGestureDetector

use of android.view.ScaleGestureDetector in project MultiPhotoPicker by wangeason.

the class TouchImageView method sharedConstructing.

private void sharedConstructing(Context context) {
    super.setClickable(true);
    this.context = context;
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    mGestureDetector = new GestureDetector(context, new GestureListener());
    matrix = new Matrix();
    prevMatrix = new Matrix();
    m = new float[9];
    normalizedScale = 1;
    if (mScaleType == null) {
        mScaleType = ScaleType.FIT_CENTER;
    }
    minScale = 1;
    maxScale = 3;
    superMinScale = SUPER_MIN_MULTIPLIER * minScale;
    superMaxScale = SUPER_MAX_MULTIPLIER * maxScale;
    setImageMatrix(matrix);
    setScaleType(ScaleType.MATRIX);
    setState(State.NONE);
    onDrawReady = false;
    super.setOnTouchListener(new PrivateOnTouchListener());
}
Also used : Matrix(android.graphics.Matrix) GestureDetector(android.view.GestureDetector) ScaleGestureDetector(android.view.ScaleGestureDetector) ScaleGestureDetector(android.view.ScaleGestureDetector)

Aggregations

ScaleGestureDetector (android.view.ScaleGestureDetector)46 GestureDetector (android.view.GestureDetector)23 Paint (android.graphics.Paint)8 Matrix (android.graphics.Matrix)7 LayoutInflater (android.view.LayoutInflater)6 WindowManager (android.view.WindowManager)6 MotionEvent (android.view.MotionEvent)5 Point (android.graphics.Point)4 GestureDetectorCompat (android.support.v4.view.GestureDetectorCompat)4 ImageView (android.widget.ImageView)3 SuppressLint (android.annotation.SuppressLint)2 PackageManager (android.content.pm.PackageManager)2 PointF (android.graphics.PointF)2 Rect (android.graphics.Rect)2 View (android.view.View)2 ViewParent (android.view.ViewParent)2 Scroller (android.widget.Scroller)2 Drawable (android.graphics.drawable.Drawable)1 ScaleGestureDetectorCompat (android.support.v4.view.ScaleGestureDetectorCompat)1 FastOutLinearInInterpolator (android.support.v4.view.animation.FastOutLinearInInterpolator)1