Example usage for android.view MotionEvent getAction

List of usage examples for android.view MotionEvent getAction

Introduction

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

Prototype

public final int getAction() 

Source Link

Document

Return the kind of action being performed.

Usage

From source file:android.support.v7.widget.FastScroller.java

@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent me) {
    if (mState == STATE_HIDDEN) {
        return;//  w w w.j a  v a2  s  .  c  o m
    }

    if (me.getAction() == MotionEvent.ACTION_DOWN) {
        boolean insideVerticalThumb = isPointInsideVerticalThumb(me.getX(), me.getY());
        boolean insideHorizontalThumb = isPointInsideHorizontalThumb(me.getX(), me.getY());
        if (insideVerticalThumb || insideHorizontalThumb) {
            if (insideHorizontalThumb) {
                mDragState = DRAG_X;
                mHorizontalDragX = (int) me.getX();
            } else if (insideVerticalThumb) {
                mDragState = DRAG_Y;
                mVerticalDragY = (int) me.getY();
            }
            setState(STATE_DRAGGING);
        }
    } else if (me.getAction() == MotionEvent.ACTION_UP && mState == STATE_DRAGGING) {
        mVerticalDragY = 0;
        mHorizontalDragX = 0;
        setState(STATE_VISIBLE);
        mDragState = DRAG_NONE;
    } else if (me.getAction() == MotionEvent.ACTION_MOVE && mState == STATE_DRAGGING) {
        show();
        if (mDragState == DRAG_X) {
            horizontalScrollTo(me.getX());
        }
        if (mDragState == DRAG_Y) {
            verticalScrollTo(me.getY());
        }
    }
}

From source file:com.appeaser.sublimepickerlibrary.datepicker.DayPickerViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (!mCanPickRange) {
        return super.onInterceptTouchEvent(ev);
    }/*from   ww  w .ja v a  2 s.  c  om*/

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        if (Config.DEBUG) {
            Log.i(TAG, "OITE: DOWN");
        }

        mInitialDownX = ev.getX();
        mInitialDownY = ev.getY();

        if (mCheckForLongPress == null) {
            mCheckForLongPress = new CheckForLongPress();
        }

        postDelayed(mCheckForLongPress, ViewConfiguration.getLongPressTimeout());
    } else if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
        if (Config.DEBUG) {
            Log.i(TAG, "OITE: (UP || CANCEL)");
        }

        if (mCheckForLongPress != null) {
            removeCallbacks(mCheckForLongPress);
        }

        mIsLongPressed = false;
        mInitialDownX = -1;
        mInitialDownY = -1;
    } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        if (Config.DEBUG) {
            Log.i(TAG, "OITE: MOVE");
        }

        if (!isStillALongPress((int) ev.getX(), (int) ev.getY())) {
            if (Config.DEBUG) {
                Log.i(TAG, "OITE: MOVED TOO MUCH, CANCELLING CheckForLongPress Runnable");
            }

            if (mCheckForLongPress != null) {
                removeCallbacks(mCheckForLongPress);
            }
        }
    }

    return mIsLongPressed || super.onInterceptTouchEvent(ev);
}

From source file:com.aosijia.dragonbutler.ui.widget.UnderlinePageIndicator.java

@SuppressLint("ClickableViewAccessibility")
public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }//from   w w  w.j  a  va 2 s .co m
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:cn.bingoogolapple.refreshlayout.BGAStickyNavLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    initVelocityTrackerIfNotExists();/*from   w ww. ja v  a 2  s. c o m*/
    mVelocityTracker.addMovement(event);

    float currentTouchY = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if (!mOverScroller.isFinished()) {
            mOverScroller.abortAnimation();
        }

        mLastTouchY = currentTouchY;
        break;
    case MotionEvent.ACTION_MOVE:
        float differentY = currentTouchY - mLastTouchY;
        mLastTouchY = currentTouchY;
        if (Math.abs(differentY) > 0) {
            scrollBy(0, (int) -differentY);
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        recycleVelocityTracker();
        if (!mOverScroller.isFinished()) {
            mOverScroller.abortAnimation();
        }
        break;
    case MotionEvent.ACTION_UP:
        mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        int initialVelocity = (int) mVelocityTracker.getYVelocity();
        if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
            fling(-initialVelocity);
        }
        recycleVelocityTracker();
        break;
    }
    return true;
}

From source file:com.example.castCambot.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));

    // When the user clicks on the button, use Android voice recognition to
    // get text/*from   w w  w . j a  v a2s  .  c o m*/

    Button voiceButton = (Button) findViewById(R.id.voiceButton);
    voiceButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startVoiceRecognitionActivity();
        }
    });

    Button buttonOK = (Button) findViewById(R.id.buttonOK);
    buttonOK.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //startVoiceRecognitionActivity();
            EditText editText1 = (EditText) findViewById(R.id.editText1);
            cambotIP = editText1.getText().toString();
            Button buttonOK = (Button) findViewById(R.id.buttonOK);
            buttonOK.setVisibility(View.INVISIBLE);

            /*
            WebView   mWebView = (WebView) findViewById(R.id.webView1);
            mWebView.getSettings().setJavaScriptEnabled(true);     
            mWebView.getSettings().setLoadWithOverviewMode(true);
            mWebView.getSettings().setUseWideViewPort(true);     
            //mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
            mWebView.loadUrl("file:///android_asset/cambot.html");
            */
        }
    });

    Button button2 = (Button) findViewById(R.id.button2);
    button2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN)
                //System.out.println("Touch");
                myHttpPost("go_forward");
            else if (action == MotionEvent.ACTION_UP)
                //System.out.println("Release");
                myHttpPost("stop");
            return false; //  the listener has NOT consumed the event, pass it on
        }
    });

    Button button4 = (Button) findViewById(R.id.button4);
    button4.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN)
                //System.out.println("Touch");
                myHttpPost("turn_left");
            else if (action == MotionEvent.ACTION_UP)
                //System.out.println("Release");
                myHttpPost("stop");
            return false; //  the listener has NOT consumed the event, pass it on
        }
    });

    Button button6 = (Button) findViewById(R.id.button6);
    button6.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN)
                //System.out.println("Touch");
                myHttpPost("turn_right");
            else if (action == MotionEvent.ACTION_UP)
                //System.out.println("Release");
                myHttpPost("stop");
            return false; //  the listener has NOT consumed the event, pass it on
        }
    });

    Button button8 = (Button) findViewById(R.id.button8);
    button8.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN)
                //System.out.println("Touch");
                myHttpPost("go_backward");
            else if (action == MotionEvent.ACTION_UP)
                //System.out.println("Release");
                myHttpPost("stop");
            return false; //  the listener has NOT consumed the event, pass it on
        }
    });

    // Configure Cast device discovery
    mMediaRouter = MediaRouter.getInstance(getApplicationContext());
    mMediaRouteSelector = new MediaRouteSelector.Builder().addControlCategory(
            CastMediaControlIntent.categoryForCast(getResources().getString(R.string.app_id))).build();
    mMediaRouterCallback = new MyMediaRouterCallback();
}

From source file:com.androtex.viewpagerindicator.TitlePageIndicator.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        final int count = mViewPager.getAdapter().getCount();
        final int width = getWidth();
        final float halfWidth = width / 2f;
        final float sixthWidth = width / 6f;

        if ((mCurrentPage > 0) && (event.getX() < halfWidth - sixthWidth)) {
            mViewPager.setCurrentItem(mCurrentPage - 1);
            return true;
        } else if ((mCurrentPage < count - 1) && (event.getX() > halfWidth + sixthWidth)) {
            mViewPager.setCurrentItem(mCurrentPage + 1);
            return true;
        }/*from   ww w . jav  a2  s.co m*/
    }

    return super.onTouchEvent(event);
}

From source file:com.appunite.appunitevideoplayer.PlayerActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.player_activity);
    root = (ViewGroup) findViewById(R.id.root);
    root.setOnTouchListener(new OnTouchListener() {
        @Override/* w ww.  ja  v a  2 s . c o m*/
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                toggleControlsVisibility();
            } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                view.performClick();
            }
            return true;
        }
    });
    root.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE
                    || keyCode == KeyEvent.KEYCODE_MENU) {
                return false;
            }
            return mediaController.dispatchKeyEvent(event);
        }
    });

    shutterView = findViewById(R.id.shutter);

    videoFrame = (AspectRatioFrameLayout) findViewById(R.id.video_frame);
    surfaceView = (SurfaceView) findViewById(R.id.surface_view);
    surfaceView.getHolder().addCallback(this);

    subtitleLayout = (SubtitleLayout) findViewById(R.id.subtitles);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setNavigationIcon(R.drawable.ic_arrow_back);
    toolbar.setTitle(getIntent().getStringExtra(TITLE_TEXT_EXTRA));
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    mediaController = new MediaController(this);
    mediaController.setAnchorView(root);
    controllerView = (ViewGroup) findViewById(R.id.controller_view);
    controllerView.addView(mediaController);
    CookieHandler currentHandler = CookieHandler.getDefault();
    if (currentHandler != defaultCookieManager) {
        CookieHandler.setDefault(defaultCookieManager);
    }

    audioCapabilitiesReceiver = new AudioCapabilitiesReceiver(this, this);
    audioCapabilitiesReceiver.register();

    progressBar = (ProgressBar) findViewById(R.id.progress_bar);

    playButton = (ImageView) findViewById(R.id.play_button_icon);
    final int playButtonIconDrawableId = getIntent().getIntExtra(PLAY_BUTTON_EXTRA, 0);
    if (playButtonIconDrawableId != 0) {
        playButton.setImageDrawable(ContextCompat.getDrawable(this, playButtonIconDrawableId));
        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                preparePlayer(true);
            }
        });
    }
}

From source file:com.afayear.android.client.view.TabPageIndicator.java

@Override
public boolean onGenericMotionEvent(final MotionEvent event) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1)
        return false;
    if (mTabProvider == null)
        return false;
    if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_SCROLL: {
            final float vscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
            if (vscroll < 0) {
                if (mCurrentItem + 1 < mTabProvider.getCount()) {
                    setCurrentItem(mCurrentItem + 1);
                }/*from w w w  .  ja  v  a2s. com*/
            } else if (vscroll > 0) {
                if (mCurrentItem - 1 >= 0) {
                    setCurrentItem(mCurrentItem - 1);
                }
            }
        }
        }
    }
    return true;
}

From source file:com.codeim.coxin.LoginActivity.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        View v = getCurrentFocus();
        if (isShouldHideInput(v, ev)) {
            hideSoftInput(v.getWindowToken());
        }/*from  w w  w  .j  a  v a  2  s  .  c  o  m*/
    }
    return super.dispatchTouchEvent(ev);
}

From source file:it.angrydroids.epub3reader.TextSelectionSupport.java

@Override
public boolean onTouch(View v, MotionEvent event) {

    final Context ctx = mActivity;
    float xPoint = getDensityIndependentValue(event.getX(), ctx) / getDensityIndependentValue(mScale, ctx);
    float yPoint = getDensityIndependentValue(event.getY(), ctx) / getDensityIndependentValue(mScale, ctx);

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        final String startTouchUrl = String.format("javascript:android.selection.startTouch(%f, %f);", xPoint,
                yPoint);//from  w w  w .ja v a2 s .  c  o  m
        mLastTouchX = xPoint;
        mLastTouchY = yPoint;
        mWebView.loadUrl(startTouchUrl);
        break;
    case MotionEvent.ACTION_UP:
        if (!mScrolling) {
            endSelectionMode();
            //
            // Fixes 4.4 double selection
            // See:
            // http://stackoverflow.com/questions/20391783/how-to-avoid-default-selection-on-long-press-in-android-kitkat-4-4
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                return false;
            }
        }
        mScrollDiffX = 0;
        mScrollDiffY = 0;
        mScrolling = false;
        //
        // Fixes 4.4 double selection
        // See:
        // http://stackoverflow.com/questions/20391783/how-to-avoid-default-selection-on-long-press-in-android-kitkat-4-4
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            return true;
        }
        break;
    case MotionEvent.ACTION_MOVE:
        mScrollDiffX += (xPoint - mLastTouchX);
        mScrollDiffY += (yPoint - mLastTouchY);
        mLastTouchX = xPoint;
        mLastTouchY = yPoint;
        if (Math.abs(mScrollDiffX) > SCROLLING_THRESHOLD || Math.abs(mScrollDiffY) > SCROLLING_THRESHOLD) {
            mScrolling = true;
        }
        break;
    }

    return false;
}