Example usage for android.view MotionEvent ACTION_DOWN

List of usage examples for android.view MotionEvent ACTION_DOWN

Introduction

In this page you can find the example usage for android.view MotionEvent ACTION_DOWN.

Prototype

int ACTION_DOWN

To view the source code for android.view MotionEvent ACTION_DOWN.

Click Source Link

Document

Constant for #getActionMasked : A pressed gesture has started, the motion contains the initial starting location.

Usage

From source file:edu.sfsu.cs.orange.ocr.CaptureActivity.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    checkFirstLaunch();//  w w  w .  java 2  s .com

    if (isFirstLaunch) {
        setDefaultPreferences();
    }

    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.capture);
    viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
    cameraButtonView = findViewById(R.id.camera_button_view);
    resultView = findViewById(R.id.result_view);

    statusViewBottom = (TextView) findViewById(R.id.status_view_bottom);
    registerForContextMenu(statusViewBottom);
    statusViewTop = (TextView) findViewById(R.id.status_view_top);
    registerForContextMenu(statusViewTop);

    handler = null;
    lastResult = null;
    hasSurface = false;
    beepManager = new BeepManager(this);

    // Camera shutter button
    if (DISPLAY_SHUTTER_BUTTON) {
        shutterButton = (ShutterButton) findViewById(R.id.shutter_button);
        shutterButton.setOnShutterButtonListener(this);
    }

    ocrResultView = (TextView) findViewById(R.id.ocr_result_text_view);
    registerForContextMenu(ocrResultView);
    translationView = (TextView) findViewById(R.id.translation_text_view);
    registerForContextMenu(translationView);

    progressView = (View) findViewById(R.id.indeterminate_progress_indicator_view);

    cameraManager = new CameraManager(getApplication());
    viewfinderView.setCameraManager(cameraManager);

    // Set listener to change the size of the viewfinder rectangle.
    viewfinderView.setOnTouchListener(new View.OnTouchListener() {
        int lastX = -1;
        int lastY = -1;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = -1;
                lastY = -1;
                return true;
            case MotionEvent.ACTION_MOVE:
                int currentX = (int) event.getX();
                int currentY = (int) event.getY();

                try {
                    Rect rect = cameraManager.getFramingRect();

                    final int BUFFER = 50;
                    final int BIG_BUFFER = 60;
                    if (lastX >= 0) {
                        // Adjust the size of the viewfinder rectangle. Check if the touch event occurs in the corner areas first, because the regions overlap.
                        if (((currentX >= rect.left - BIG_BUFFER && currentX <= rect.left + BIG_BUFFER)
                                || (lastX >= rect.left - BIG_BUFFER && lastX <= rect.left + BIG_BUFFER))
                                && ((currentY <= rect.top + BIG_BUFFER && currentY >= rect.top - BIG_BUFFER)
                                        || (lastY <= rect.top + BIG_BUFFER
                                                && lastY >= rect.top - BIG_BUFFER))) {
                            // Top left corner: adjust both top and left sides
                            cameraManager.adjustFramingRect(2 * (lastX - currentX), 2 * (lastY - currentY));
                            viewfinderView.removeResultText();
                        } else if (((currentX >= rect.right - BIG_BUFFER && currentX <= rect.right + BIG_BUFFER)
                                || (lastX >= rect.right - BIG_BUFFER && lastX <= rect.right + BIG_BUFFER))
                                && ((currentY <= rect.top + BIG_BUFFER && currentY >= rect.top - BIG_BUFFER)
                                        || (lastY <= rect.top + BIG_BUFFER
                                                && lastY >= rect.top - BIG_BUFFER))) {
                            // Top right corner: adjust both top and right sides
                            cameraManager.adjustFramingRect(2 * (currentX - lastX), 2 * (lastY - currentY));
                            viewfinderView.removeResultText();
                        } else if (((currentX >= rect.left - BIG_BUFFER && currentX <= rect.left + BIG_BUFFER)
                                || (lastX >= rect.left - BIG_BUFFER && lastX <= rect.left + BIG_BUFFER))
                                && ((currentY <= rect.bottom + BIG_BUFFER
                                        && currentY >= rect.bottom - BIG_BUFFER)
                                        || (lastY <= rect.bottom + BIG_BUFFER
                                                && lastY >= rect.bottom - BIG_BUFFER))) {
                            // Bottom left corner: adjust both bottom and left sides
                            cameraManager.adjustFramingRect(2 * (lastX - currentX), 2 * (currentY - lastY));
                            viewfinderView.removeResultText();
                        } else if (((currentX >= rect.right - BIG_BUFFER && currentX <= rect.right + BIG_BUFFER)
                                || (lastX >= rect.right - BIG_BUFFER && lastX <= rect.right + BIG_BUFFER))
                                && ((currentY <= rect.bottom + BIG_BUFFER
                                        && currentY >= rect.bottom - BIG_BUFFER)
                                        || (lastY <= rect.bottom + BIG_BUFFER
                                                && lastY >= rect.bottom - BIG_BUFFER))) {
                            // Bottom right corner: adjust both bottom and right sides
                            cameraManager.adjustFramingRect(2 * (currentX - lastX), 2 * (currentY - lastY));
                            viewfinderView.removeResultText();
                        } else if (((currentX >= rect.left - BUFFER && currentX <= rect.left + BUFFER)
                                || (lastX >= rect.left - BUFFER && lastX <= rect.left + BUFFER))
                                && ((currentY <= rect.bottom && currentY >= rect.top)
                                        || (lastY <= rect.bottom && lastY >= rect.top))) {
                            // Adjusting left side: event falls within BUFFER pixels of left side, and between top and bottom side limits
                            cameraManager.adjustFramingRect(2 * (lastX - currentX), 0);
                            viewfinderView.removeResultText();
                        } else if (((currentX >= rect.right - BUFFER && currentX <= rect.right + BUFFER)
                                || (lastX >= rect.right - BUFFER && lastX <= rect.right + BUFFER))
                                && ((currentY <= rect.bottom && currentY >= rect.top)
                                        || (lastY <= rect.bottom && lastY >= rect.top))) {
                            // Adjusting right side: event falls within BUFFER pixels of right side, and between top and bottom side limits
                            cameraManager.adjustFramingRect(2 * (currentX - lastX), 0);
                            viewfinderView.removeResultText();
                        } else if (((currentY <= rect.top + BUFFER && currentY >= rect.top - BUFFER)
                                || (lastY <= rect.top + BUFFER && lastY >= rect.top - BUFFER))
                                && ((currentX <= rect.right && currentX >= rect.left)
                                        || (lastX <= rect.right && lastX >= rect.left))) {
                            // Adjusting top side: event falls within BUFFER pixels of top side, and between left and right side limits
                            cameraManager.adjustFramingRect(0, 2 * (lastY - currentY));
                            viewfinderView.removeResultText();
                        } else if (((currentY <= rect.bottom + BUFFER && currentY >= rect.bottom - BUFFER)
                                || (lastY <= rect.bottom + BUFFER && lastY >= rect.bottom - BUFFER))
                                && ((currentX <= rect.right && currentX >= rect.left)
                                        || (lastX <= rect.right && lastX >= rect.left))) {
                            // Adjusting bottom side: event falls within BUFFER pixels of bottom side, and between left and right side limits
                            cameraManager.adjustFramingRect(0, 2 * (currentY - lastY));
                            viewfinderView.removeResultText();
                        }
                    }
                } catch (NullPointerException e) {
                    Log.e(TAG, "Framing rect not available", e);
                }
                v.invalidate();
                lastX = currentX;
                lastY = currentY;
                return true;
            case MotionEvent.ACTION_UP:
                lastX = -1;
                lastY = -1;
                return true;
            }
            return false;
        }
    });

    isEngineReady = false;
    aq = new AQuery(this);
    beerQuery = new BeerQuery();

}

From source file:com.spoiledmilk.ibikecph.map.MapActivity.java

protected boolean onImgSwiperTouch(View view, MotionEvent event) {
    if (leftContainer.getVisibility() != View.VISIBLE) {
        leftContainer.setVisibility(View.VISIBLE);
    }/*from  ww w  .j  a  va 2s . c om*/
    if (mapDisabledView.getVisibility() != View.VISIBLE) {
        mapDisabledView.setVisibility(View.VISIBLE);
    }
    switch (event.getAction()) {
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        view.setPressed(false);
        if (moveCount <= 3)
            translate(slidden ? -maxSlide : maxSlide, true);
        else
            translate(0, true);
        break;
    case MotionEvent.ACTION_DOWN:
        moveCount = 0;
        view.setPressed(true);
        touchX = event.getX();
        break;
    case MotionEvent.ACTION_MOVE:
        if (moveCount++ < 3)
            break;
        newTouchX = event.getX();
        delta = newTouchX - touchX;
        translate(delta, false);
        touchX = newTouchX;
        break;
    }

    if (slidden && mapDisabledView.getVisibility() != View.GONE) {
        mapDisabledView.setVisibility(View.GONE);
    }

    return true;
}

From source file:com.amitupadhyay.aboutexample.ui.widget.FabOverlapTextView.java

/**
 * This is why you don't implement your own TextView kids; you have to handle everything!
 *//*  w  w  w .j  a v a 2s  .c o  m*/
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!(text instanceof Spanned))
        return super.onTouchEvent(event);

    Spannable spannedText = (Spannable) text;

    boolean handled = false;
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        pressedSpan = getPressedSpan(spannedText, event);
        if (pressedSpan != null) {
            pressedSpan.setPressed(true);
            Selection.setSelection(spannedText, spannedText.getSpanStart(pressedSpan),
                    spannedText.getSpanEnd(pressedSpan));
            handled = true;
            postInvalidateOnAnimation();
        }
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        TouchableUrlSpan touchedSpan = getPressedSpan(spannedText, event);
        if (pressedSpan != null && touchedSpan != pressedSpan) {
            pressedSpan.setPressed(false);
            pressedSpan = null;
            Selection.removeSelection(spannedText);
            postInvalidateOnAnimation();
        }
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        if (pressedSpan != null) {
            pressedSpan.setPressed(false);
            pressedSpan.onClick(this);
            handled = true;
            postInvalidateOnAnimation();
        }
        pressedSpan = null;
        Selection.removeSelection(spannedText);
    } else {
        if (pressedSpan != null) {
            pressedSpan.setPressed(false);
            handled = true;
            postInvalidateOnAnimation();
        }
        pressedSpan = null;
        Selection.removeSelection(spannedText);
    }
    return handled;
}

From source file:com.example.appf.CS3570.java

@Override
public boolean onTouchEvent(MotionEvent e) {
    // MotionEvent reports input details from the touch screen
    // and other input controls. In this case, you are only
    // interested in events where the touch position changed.

    float x = e.getX();
    float y = e.getY();

    switch (e.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if (mother.out != null) {
            mother.out.println("{\"type\": \"DOWN\", \"x\": " + x + ", \"y\": " + y + "}\0");
        }//from   ww  w .  jav  a2s  . co  m
        break;
    case MotionEvent.ACTION_UP:
        if (mother.out != null) {
            mother.out.println("{\"type\": \"UP\", \"x\": " + x + ", \"y\": " + y + "}\0");
        }
        break;
    case MotionEvent.ACTION_MOVE:

        float dx = x - mPreviousX;
        float dy = y - mPreviousY;

        // reverse direction of rotation above the mid-line
        if (y > getHeight() / 2) {
            dx = dx * -1;
        }

        // reverse direction of rotation to left of the mid-line
        if (x < getWidth() / 2) {
            dy = dy * -1;
        }

        //mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;  // = 180.0f / 320
        //mRenderer.mTetra.rotate(dx, 0, 1, 0);
        //mRenderer.mTetra.rotate(dy, 1, 0, 0);
        //mRenderer.mCamera.rotateY((double)(dx * TOUCH_SCALE_FACTOR));
        //mRenderer.mCamera.rotateX((double)(dy * TOUCH_SCALE_FACTOR));
        if (mother.out != null) {
            Log.e("eee", "{\"type\": \"MOVE\", \"x\": " + x + ", \"y\": " + y + "}");
            mother.out.println("{\"type\": \"MOVE\", \"x\": " + x + ",  \"y\": " + y + "}\0");
        }
        requestRender();
    }

    mPreviousX = x;
    mPreviousY = y;
    return true;
}

From source file:cn.ieclipse.af.view.StaggeredGridView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);/*from ww w .j a v a 2s .  c  o  m*/
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mVelocityTracker.clear();
        mScroller.abortAnimation();
        mLastTouchY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderY = 0;
        break;

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(TAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did StaggeredGridView receive an inconsistent " + "event stream?");
            return false;
        }
        final float y = MotionEventCompat.getY(ev, index);
        final float dy = y - mLastTouchY + mTouchRemainderY;
        final int deltaY = (int) dy;
        mTouchRemainderY = dy - deltaY;

        if (Math.abs(dy) > mTouchSlop) {
            mTouchMode = TOUCH_MODE_DRAGGING;
        }

        if (mTouchMode == TOUCH_MODE_DRAGGING) {
            mLastTouchY = y;

            if (!trackMotionScroll(deltaY, true)) {
                // Break fling velocity if we impacted an edge.
                mVelocityTracker.clear();
            }
        }
    }
        break;

    case MotionEvent.ACTION_CANCEL:
        mTouchMode = TOUCH_MODE_IDLE;
        break;

    case MotionEvent.ACTION_UP: {
        mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        final float velocity = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId);
        if (Math.abs(velocity) > mFlingVelocity) { // TODO
            mTouchMode = TOUCH_MODE_FLINGING;
            mScroller.fling(0, 0, 0, (int) velocity, 0, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
            mLastTouchY = 0;
            ViewCompat.postInvalidateOnAnimation(this);
        } else {
            mTouchMode = TOUCH_MODE_IDLE;
        }

    }
        break;
    }
    return true;
}

From source file:net.oschina.app.ui.MainActivity.java

@SuppressLint("ClickableViewAccessibility")
@Override//from   w w  w. j a  va 2 s. co  m
public boolean onTouch(View v, MotionEvent event) {
    super.onTouchEvent(event);
    boolean consumed = false;
    // use getTabHost().getCurrentTabView to decide if the current tab is
    // touched again
    if (event.getAction() == MotionEvent.ACTION_DOWN && v.equals(mTabHost.getCurrentTabView())) {
        // use getTabHost().getCurrentView() to get a handle to the view
        // which is displayed in the tab - and to get this views context
        Fragment currentFragment = getCurrentFragment();
        if (currentFragment != null && currentFragment instanceof OnTabReselectListener) {
            OnTabReselectListener listener = (OnTabReselectListener) currentFragment;
            listener.onTabReselect();
            consumed = true;
        }
    }
    return consumed;
}

From source file:com.cssweb.android.view.FTrendView.java

/**
 * true?,false??//from  w  ww.  ja  v a  2s  .com
 */
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        touchesBegan(event);
        break;

    case MotionEvent.ACTION_MOVE:
        touchesMoved(event);
        break;

    case MotionEvent.ACTION_UP:
        touchesEnded(event);
        break;

    case MotionEvent.ACTION_CANCEL:
        break;
    }
    return true;
}

From source file:com.apptentive.android.sdk.module.messagecenter.view.MessageCenterListView.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

    final float x = ev.getX();
    final float y = ev.getY();
    final int action = ev.getAction();

    if (action == MotionEvent.ACTION_DOWN && touchTarget == null && stickyWrapper != null
            && isStickyViewTouched(stickyWrapper.view, x, y)) {
        touchTarget = stickyWrapper.view;
        touchPt.x = x;//from ww w . j  a  va2 s.  co  m
        touchPt.y = y;

        downEvent = MotionEvent.obtain(ev);
    }

    if (touchTarget != null) {
        if (isStickyViewTouched(touchTarget, x, y)) {
            // forward event to header view
            touchTarget.dispatchTouchEvent(ev);
        }

        if (action == MotionEvent.ACTION_UP) {
            super.dispatchTouchEvent(ev);
            clearTouchTarget();

        } else if (action == MotionEvent.ACTION_CANCEL) {
            clearTouchTarget();

        } else if (action == MotionEvent.ACTION_MOVE) {
            if (Math.abs(y - touchPt.y) > touchSlop) {

                MotionEvent event = MotionEvent.obtain(ev);
                event.setAction(MotionEvent.ACTION_CANCEL);
                touchTarget.dispatchTouchEvent(event);
                event.recycle();

                super.dispatchTouchEvent(downEvent);
                super.dispatchTouchEvent(ev);
                clearTouchTarget();

            }
        }

        return true;
    }

    return super.dispatchTouchEvent(ev);
}

From source file:agricultural.nxt.agriculturalsupervision.Activity.company.CompanyActivity.java

@Override
public void onTouch(MotionEvent motionEvent) {
    int action = motionEvent.getAction();
    switch (action) {
    case MotionEvent.ACTION_UP:
        sl.requestDisallowInterceptTouchEvent(false);
        break;/*from w  w  w .j  a va  2 s .c  o  m*/
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_MOVE:
        sl.requestDisallowInterceptTouchEvent(true);
        break;
    }
}

From source file:com.android.photos.views.GalleryThumbnailView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mVelocityTracker.addMovement(ev);/* w  w w.j ava 2 s.co  m*/
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mVelocityTracker.clear();
        mScroller.abortAnimation();
        mLastTouchX = ev.getX();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderX = 0;
        break;

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(TAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did StaggeredGridView receive an inconsistent " + "event stream?");
            return false;
        }
        final float x = MotionEventCompat.getX(ev, index);
        final float dx = x - mLastTouchX + mTouchRemainderX;
        final int deltaX = (int) dx;
        mTouchRemainderX = dx - deltaX;

        if (Math.abs(dx) > mTouchSlop) {
            mTouchMode = TOUCH_MODE_DRAGGING;
        }

        if (mTouchMode == TOUCH_MODE_DRAGGING) {
            mLastTouchX = x;

            if (!trackMotionScroll(deltaX, true)) {
                // Break fling velocity if we impacted an edge.
                mVelocityTracker.clear();
            }
        }
    }
        break;

    case MotionEvent.ACTION_CANCEL:
        mTouchMode = TOUCH_MODE_IDLE;
        break;

    case MotionEvent.ACTION_UP: {
        mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        final float velocity = VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId);
        if (Math.abs(velocity) > mFlingVelocity) { // TODO
            mTouchMode = TOUCH_MODE_FLINGING;
            mScroller.fling(0, 0, (int) velocity, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
            mLastTouchX = 0;
            ViewCompat.postInvalidateOnAnimation(this);
        } else {
            mTouchMode = TOUCH_MODE_IDLE;
        }

    }
        break;
    }
    return true;
}