Example usage for android.view MotionEvent getRawX

List of usage examples for android.view MotionEvent getRawX

Introduction

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

Prototype

public final float getRawX() 

Source Link

Document

Returns the original raw X coordinate of this event.

Usage

From source file:com.codingfeel.sm.views.superrecyclerview.swipe.SwipeDismissRecyclerViewTouchListener.java

private void caseMotionActionDown(MotionEvent motionEvent) {
    // Find the child view that was touched (perform a hit test)
    Rect rect = new Rect();
    int childCount = mRecyclerView.getChildCount();
    int[] listViewCoords = new int[2];
    mRecyclerView.getLocationOnScreen(listViewCoords);
    int x = (int) motionEvent.getRawX() - listViewCoords[0];
    int y = (int) motionEvent.getRawY() - listViewCoords[1];
    View child;//  w w w.  j a  v  a 2  s  . c  om
    for (int i = 0; i < childCount; i++) {
        child = mRecyclerView.getChildAt(i);
        child.getHitRect(rect);
        if (rect.contains(x, y)) {
            mDownView = child;
            break;
        }
    }

    if (mDownView != null) {
        mDownX = motionEvent.getRawX();
        mDownY = motionEvent.getRawY();
        mDownPosition = mRecyclerView.getChildAdapterPosition(mDownView);
        if (mCallbacks.canDismiss(mDownPosition)) {
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(motionEvent);
        } else {
            mDownView = null;
        }
    }
}

From source file:com.malinskiy.superrecyclerview.swipe.SwipeDismissRecyclerViewTouchListener.java

private void caseMotionActionDown(MotionEvent motionEvent) {
    // Find the child view that was touched (perform a hit test)
    Rect rect = new Rect();
    int childCount = mRecyclerView.getChildCount();
    int[] listViewCoords = new int[2];
    mRecyclerView.getLocationOnScreen(listViewCoords);
    int x = (int) motionEvent.getRawX() - listViewCoords[0];
    int y = (int) motionEvent.getRawY() - listViewCoords[1];
    View child;// ww w  .  ja v a 2  s. c  om
    for (int i = 0; i < childCount; i++) {
        child = mRecyclerView.getChildAt(i);
        child.getHitRect(rect);
        if (rect.contains(x, y)) {
            mDownView = child;
            break;
        }
    }

    if (mDownView != null) {
        mDownX = motionEvent.getRawX();
        mDownY = motionEvent.getRawY();
        mDownPosition = mRecyclerView.getChildLayoutPosition(mDownView);
        if (mCallbacks.canDismiss(mDownPosition)) {
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(motionEvent);
        } else {
            mDownView = null;
        }
    }
}

From source file:com.haarman.listviewanimations.itemmanipulation.SwipeDismissListViewTouchListener.java

private boolean handleUpEvent(MotionEvent motionEvent) {
    if (mVelocityTracker == null) {
        return false;
    }//from ww  w . j  av  a 2  s.  c o m

    float deltaX = motionEvent.getRawX() - mDownX;
    mVelocityTracker.addMovement(motionEvent);
    mVelocityTracker.computeCurrentVelocity(1000);
    float velocityX = Math.abs(mVelocityTracker.getXVelocity());
    float velocityY = Math.abs(mVelocityTracker.getYVelocity());
    boolean dismiss = false;
    boolean dismissRight = false;
    if (Math.abs(deltaX) > mViewWidth / 2) {
        dismiss = true;
        dismissRight = deltaX > 0;
    } else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX) {
        dismiss = true;
        dismissRight = mVelocityTracker.getXVelocity() > 0;
    }

    if (mSwiping) {
        if (dismiss) {
            Log.d("SwipeDismissListViewTouchListener", "swipe/confimed");
            // mDownView gets null'd before animation ends
            final PendingDismissData pendingDismissData = mCurrentDismissData;
            ++mDismissAnimationRefCount;

            ArrayList<View> list = new ArrayList<View>(getAllTreeChildViews(mCurrentDismissData.view));
            list.add(mCurrentDismissData.view);

            for (View v : list) {
                ViewPropertyAnimator anim = animate(v).translationX(dismissRight ? mViewWidth : -mViewWidth)
                        .alpha(0).setDuration(mAnimationTime);
                if (v == mCurrentDismissData.view) {
                    anim.setListener(new AnimatorListenerAdapter() {

                        boolean finished = false;

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            if (finished)
                                return;
                            onDismiss(pendingDismissData);
                            finished = true;
                        }
                    });
                }
            }

            mVirtualListCount--;
            mPendingDismisses.add(mCurrentDismissData);
        } else {
            Log.d("SwipeDismissListViewTouchListener", "swipe/cancelled");
            // cancel
            ArrayList<View> list = new ArrayList<View>(getAllTreeChildViews(mCurrentDismissData.view));
            list.add(mCurrentDismissData.view);

            for (View v : list) {
                animate(v).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
            }
        }
    }

    mVelocityTracker.recycle();
    mVelocityTracker = null;
    mDownX = 0;
    mCurrentDismissData = null;
    mSwiping = false;
    return false;
}

From source file:com.appnexus.opensdkapp.MainActivity.java

/**
 * Special handling for our "native" log button
 */// w w  w. j  a  v  a  2 s  .  com

@Override
public boolean dispatchTouchEvent(MotionEvent motionEvent) {
    // don't handle specially if log button is not showing
    if (btnLog.getVisibility() == View.GONE)
        return super.dispatchTouchEvent(motionEvent);

    float x = motionEvent.getRawX() - contentView.getLeft();
    float y = motionEvent.getRawY() - contentView.getTop();

    // if the user presses btnMore, don't handle specially, btnMore will handle it
    if ((btnMore.getTop() < y) && (btnMore.getBottom() > y) && (btnMore.getLeft() < x)
            && (x < btnMore.getRight())) {
        return super.dispatchTouchEvent(motionEvent);
    }

    // if the user presses outside the bounds of btnLog, "close it"
    if (y < (btnLog.getTop()) || (btnLog.getBottom() < y) || (x < btnLog.getLeft())
            || (btnLog.getRight() < x)) {
        btnLog.setVisibility(View.GONE);
    }

    return super.dispatchTouchEvent(motionEvent);
}

From source file:com.brotherpowers.audiojournal.View.SwipeBackLayout.java

private void chkDragable() {
    setOnTouchListener(new OnTouchListener() {
        @Override/*from   www  .j a va  2 s. co m*/
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                lastY = motionEvent.getRawY();
                lastX = motionEvent.getRawX();
            } else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                newY = motionEvent.getRawY();
                lastX = motionEvent.getRawX();

                offsetY = Math.abs(newY - lastY);
                lastY = newY;

                offsetX = Math.abs(newX - lastX);
                lastX = newX;

                switch (dragEdge) {
                case TOP:
                case BOTTOM:
                    setEnablePullToBack(offsetY > offsetX);
                case LEFT:
                case RIGHT:
                    setEnablePullToBack(offsetY < offsetX);
                    break;
                }
            }

            return false;
        }
    });
}

From source file:cn.com.hgh.view.SlideSwitch.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (slideable == false)
        return super.onTouchEvent(event);
    int action = MotionEventCompat.getActionMasked(event);
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        eventStartX = (int) event.getRawX();
        break;//ww  w.  j  a v a  2 s.  c  o m
    case MotionEvent.ACTION_MOVE:
        //         eventLastX = (int) event.getRawX();
        //         diffX = eventLastX - eventStartX;
        //         int tempX = diffX + frontRect_left_begin;
        //         tempX = (tempX > max_left ? max_left : tempX);
        //         tempX = (tempX < min_left ? min_left : tempX);
        //         if (tempX >= min_left && tempX <= max_left) {
        //            frontRect_left = tempX;
        //            alpha = (int) (255 * (float) tempX / (float) max_left);
        //            invalidateView();
        //         }
        break;
    case MotionEvent.ACTION_UP:
        int wholeX = (int) (event.getRawX() - eventStartX);
        frontRect_left_begin = frontRect_left;
        toRight = (frontRect_left_begin > max_left / 2 ? true : false);
        if (Math.abs(wholeX) < 3) {
            toRight = !toRight;
        }
        //x??
        float eventx = event.getX();
        if (isOpen) {
            //?
            if (eventx < width / 2) {
                //            switchBusinessStatus();
                switchStatus();

            }
        } else {
            //?
            if (eventx > width / 2) {
                //               switchBusinessStatus();
                switchStatus();

            }
        }
        break;
    default:
        break;
    }
    return true;
}

From source file:com.freecats.demo.swipebackdemo.view.SwipeBackLayout.java

private boolean isCanFinish(MotionEvent event) {
    boolean result = false;
    float currentX = 0;
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        downX = event.getRawX();
        break;// w w  w  .  j  a  va 2 s.  c o  m
    case MotionEvent.ACTION_MOVE:

        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        currentX = event.getRawX();
        break;
    default:
        break;
    }
    result = currentX - downX > BACK_RANGE;
    return result;
}

From source file:com.qa.perf.emmageeplus.service.EmmageeService.java

/**
 * create a floating window to show real-time data.
 *//*from   w w  w.  j  a va 2  s.c  o m*/
private void createFloatingWindow() {
    getSharedPreferences("float_flag", Activity.MODE_PRIVATE).edit().putInt("float", 1).apply();
    windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    wmParams = ((MyApplication) getApplication()).getMywmParams();
    wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
    wmParams.flags |= 8;
    wmParams.gravity = Gravity.START | Gravity.TOP;
    wmParams.x = 0;
    wmParams.y = 0;
    wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    wmParams.format = 1;
    windowManager.addView(viFloatingWindow, wmParams);
    viFloatingWindow.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            x = event.getRawX();
            y = event.getRawY() - statusBarHeight;
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTouchStartX = event.getX();
                mTouchStartY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                updateViewPosition();
                break;
            case MotionEvent.ACTION_UP:
                updateViewPosition();
                mTouchStartX = mTouchStartY = 0;
                break;
            }
            return true;
        }
    });

    btnWifi.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                btnWifi = (Button) viFloatingWindow.findViewById(R.id.wifi);
                String buttonText = (String) btnWifi.getText();
                String wifiText = getResources().getString(R.string.open_wifi);
                if (buttonText.equals(wifiText)) {
                    wifiManager.setWifiEnabled(true);
                    btnWifi.setText(R.string.close_wifi);
                } else {
                    wifiManager.setWifiEnabled(false);
                    btnWifi.setText(R.string.open_wifi);
                }
            } catch (Exception e) {
                Toast.makeText(viFloatingWindow.getContext(), getString(R.string.wifi_fail_toast),
                        Toast.LENGTH_LONG).show();
                Log.e(LOG_TAG, e.toString());
            }
        }
    });
}

From source file:com.haarman.listviewanimations.itemmanipulation.SwipeDismissListViewTouchListener.java

private boolean handleMoveEvent(MotionEvent motionEvent) {
    if (mPaused || (mVelocityTracker == null)) {
        return false;
    }/*from  ww w . j ava  2s  . c om*/

    mVelocityTracker.addMovement(motionEvent);
    float deltaX = motionEvent.getRawX() - mDownX;
    float deltaY = motionEvent.getRawY() - mDownY;
    if (mTouchChildTouched && !mDisallowSwipe && Math.abs(deltaX) > mSlop
            && Math.abs(deltaX) > Math.abs(deltaY)) {
        mSwiping = true;
        mListView.requestDisallowInterceptTouchEvent(true);

        // Cancel ListView's touch (un-highlighting the item)
        MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
        cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
        mListView.onTouchEvent(cancelEvent);
    }

    if (mSwiping) {
        if (!mSwipeInitiated) {
            Log.d("SwipeDismissListViewTouchListener", "swipe/begin");
        }
        mSwipeInitiated = true;
        ViewHelper.setTranslationX(mCurrentDismissData.view, deltaX);
        ViewHelper.setAlpha(mCurrentDismissData.view,
                Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
        for (View v : getAllTreeChildViews(mCurrentDismissData.view)) {
            ViewHelper.setTranslationX(v, deltaX);
            ViewHelper.setAlpha(v, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
        }
        return true;
    }
    return false;
}

From source file:com.tjw.swipeback.SwipeBackLayout.java

private void chkDragable() {
    setOnTouchListener(new OnTouchListener() {
        @Override/*www. j  a  v a 2  s .c o  m*/
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                lastY = motionEvent.getRawY();
                lastX = motionEvent.getRawX();
            } else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                newY = motionEvent.getRawY();
                lastX = motionEvent.getRawX();

                offsetY = Math.abs(newY - lastY);
                lastY = newY;

                offsetX = Math.abs(newX - lastX);
                lastX = newX;

                switch (dragEdge) {
                case TOP:
                case BOTTOM:
                    setEnablePullToBack(offsetY > offsetX);
                case LEFT:
                case RIGHT:
                    setEnablePullToBack(offsetY < offsetX);
                    break;
                }
            }

            return false;
        }
    });

}