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.king.android.common.widget.badge.BGADragBadgeView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    int badgeWidth = (int) mBadgeViewHelper.getBadgeRectF().width();
    int badgeHeight = (int) mBadgeViewHelper.getBadgeRectF().height();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if (!mAnimator.isRunning()) {
            mStartX = (int) event.getRawX() - badgeWidth / 2;
            mStartY = (int) event.getRawY() - badgeHeight / 2 - getStatusBarHeight(getContext());
            mWindowManager.addView(this, mLayoutParams);
            postInvalidate();/*ww  w  . j av  a2 s .c  o  m*/
        }
        break;
    case MotionEvent.ACTION_MOVE:
        if (!mAnimator.isRunning()) {
            int newX = (int) event.getRawX() - badgeWidth / 2;
            int newY = (int) event.getRawY() - badgeHeight / 2 - getStatusBarHeight(getContext());
            if (newX < 0) {
                newX = 0;
            }
            if (newY < 0) {
                newY = 0;
            }
            if (newX > mWindowManager.getDefaultDisplay().getWidth() - badgeWidth) {
                newX = mWindowManager.getDefaultDisplay().getWidth() - badgeWidth;
            }
            if (newY > mWindowManager.getDefaultDisplay().getHeight() - badgeHeight) {
                newY = mWindowManager.getDefaultDisplay().getHeight() - badgeHeight;
            }
            mStartX = newX;
            mStartY = newY;
            postInvalidate();
        }
        break;
    case MotionEvent.ACTION_UP:
        if (!mAnimator.isRunning()) {
            if (mBadgeViewHelper.satisfyMoveDismissCondition(event)) {
                startDismissAnim();
            } else {
                mWindowManager.removeView(this);
            }
        }
        break;
    }
    return true;
}

From source file:com.android2.calculator3.view.CalculatorPadView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = MotionEventCompat.getActionMasked(ev);
    float pos = ev.getRawX();
    mInterceptingTouchEvents = false;//from   www .j  a  v a2 s  .c  om

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mInitialMotion = pos;
        mLastMotion = pos;
        handleDown();
        break;
    case MotionEvent.ACTION_MOVE:
        // Reset initial motion if the user drags in a different direction suddenly
        if ((pos - mInitialMotion) / Math.abs(pos - mInitialMotion) != (pos - mLastMotion)
                / Math.abs(pos - mLastMotion)) {
            mInitialMotion = mLastMotion;
        }

        float delta = Math.abs(pos - mInitialMotion);
        if (delta > mTouchSlop) {
            float dx = pos - mInitialMotion;
            if (dx < 0) {
                mInterceptingTouchEvents = getState() == TranslateState.COLLAPSED;
            } else if (dx > 0) {
                mInterceptingTouchEvents = getState() == TranslateState.EXPANDED;
            }
        }
        mLastMotion = pos;
        break;
    }

    return mInterceptingTouchEvents;
}

From source file:com.kerkr.edu.recycleView.SwipeToDismissTouchListener.java

private void up(MotionEvent motionEvent) {
    if (mPaused || mVelocityTracker == null || mSwipeView == null) {
        return;/*from  w ww .  j  a va2  s.  c  o m*/
    }
    mSwipeView.setPressed(false);
    float deltaX = motionEvent.getRawX() - mDownX;
    mVelocityTracker.addMovement(motionEvent);
    mVelocityTracker.computeCurrentVelocity(1000);
    float velocityX = mVelocityTracker.getXVelocity();
    float absVelocityX = Math.abs(velocityX);
    float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
    boolean dismiss = false;
    boolean dismissRight = false;
    if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
        dismiss = true;
        dismissRight = deltaX > 0;
    } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
            && absVelocityY < absVelocityX && absVelocityY < absVelocityX && mSwiping) {
        // dismiss only if flinging in the same direction as dragging
        dismiss = (velocityX < 0) == (deltaX < 0);
        dismissRight = mVelocityTracker.getXVelocity() > 0;
    }
    if (dismiss) {
        // dismiss
        final int pos = mRecyclerView.getChildPosition(mSwipeView);
        final View swipeViewCopy = mSwipeView;
        final SwipeDirection swipeDirection = dismissRight ? SwipeDirection.RIGHT : SwipeDirection.LEFT;
        ++mDismissCount;
        ViewCompat.animate(mSwipeView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                .setDuration(mAnimationTime);

        //this is instead of unreliable onAnimationEnd callback
        swipeViewCopy.postDelayed(new Runnable() {
            @Override
            public void run() {
                performDismiss(swipeViewCopy, pos, swipeDirection);
                ViewCompat.setTranslationX(swipeViewCopy, 0);
                //                    swipeViewCopy.setAlpha(1);

            }
        }, mAnimationTime + 100);

    } else if (mSwiping) {
        // cancel
        ViewCompat.animate(mSwipeView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
    }

    resetMotion();
}

From source file:com.kerkr.edu.recycleView.SwipeToDismissTouchListener.java

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

    mVelocityTracker.addMovement(motionEvent);
    float deltaX = motionEvent.getRawX() - mDownX;
    float deltaY = motionEvent.getRawY() - mDownY;
    if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
        mSwiping = true;
        mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
        mSwipeView.setPressed(false);

        MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
        cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
        mSwipeView.onTouchEvent(cancelEvent);
    }

    //Prevent swipes to disallowed directions
    if ((deltaX < 0 && mAllowedSwipeDirection == SwipeDirection.RIGHT)
            || (deltaX > 0 && mAllowedSwipeDirection == SwipeDirection.LEFT)) {
        resetMotion();
        return false;
    }

    if (mSwiping) {
        mTranslationX = deltaX;
        ViewCompat.setTranslationX(mSwipeView, deltaX - mSwipingSlop);
        ViewCompat.setAlpha(mSwipeView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
        return true;
    }
    return false;
}

From source file:org.jak_linux.dns66.ItemActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();/*from   w  w w  . j  a va2  s  .c  o  m*/
    if (intent.getIntExtra("STATE_CHOICES", 3) == 2) {
        setContentView(R.layout.activity_item_dns);
        setTitle(R.string.activity_edit_dns_server);
    } else {
        setContentView(R.layout.activity_item);
        setTitle(R.string.activity_edit_filter);
    }

    titleText = (TextInputEditText) findViewById(R.id.title);
    locationText = (TextInputEditText) findViewById(R.id.location);
    stateSpinner = (Spinner) findViewById(R.id.state_spinner);
    stateSwitch = (Switch) findViewById(R.id.state_switch);
    imageView = (ImageView) findViewById(R.id.image_view);

    if (intent.hasExtra("ITEM_TITLE"))
        titleText.setText(intent.getStringExtra("ITEM_TITLE"));
    if (intent.hasExtra("ITEM_LOCATION"))
        locationText.setText(intent.getStringExtra("ITEM_LOCATION"));
    if (intent.hasExtra("ITEM_STATE") && stateSpinner != null)
        stateSpinner.setSelection(intent.getIntExtra("ITEM_STATE", 0));
    if (intent.hasExtra("ITEM_STATE") && stateSwitch != null)
        stateSwitch.setChecked(intent.getIntExtra("ITEM_STATE", 0) % 2 != 0);

    if (stateSpinner != null) {
        stateSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                case Configuration.Item.STATE_ALLOW:
                    imageView.setImageDrawable(
                            ContextCompat.getDrawable(ItemActivity.this, R.drawable.ic_state_allow));
                    break;
                case Configuration.Item.STATE_DENY:
                    imageView.setImageDrawable(
                            ContextCompat.getDrawable(ItemActivity.this, R.drawable.ic_state_deny));
                    break;
                case Configuration.Item.STATE_IGNORE:
                    imageView.setImageDrawable(
                            ContextCompat.getDrawable(ItemActivity.this, R.drawable.ic_state_ignore));
                    break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    // We have an attachment icon for host files
    if (intent.getIntExtra("STATE_CHOICES", 3) == 3) {
        locationText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    boolean isAttachIcon;
                    if (locationText.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR)
                        isAttachIcon = event.getRawX() >= locationText.getRight()
                                - locationText.getTotalPaddingRight();
                    else
                        isAttachIcon = event.getRawX() <= locationText.getTotalPaddingLeft()
                                - locationText.getLeft();

                    if (isAttachIcon) {
                        performFileSearch();
                        return true;
                    }

                }
                return false;
            }
        });

        // Tint the attachment icon, if any.
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(android.R.attr.textColorPrimary, typedValue, true);

        Drawable[] compoundDrawables = locationText.getCompoundDrawablesRelative();
        for (Drawable drawable : compoundDrawables) {
            if (drawable != null) {
                drawable.setTint(ContextCompat.getColor(this, typedValue.resourceId));
                Log.d(TAG, "onCreate: Setting tint");
            }
        }
    }

}

From source file:com.android2.calculator3.view.CalculatorPadView.java

protected void handleMove(MotionEvent event) {
    float percent = getCurrentPercent();
    mAnimator.onUpdate(percent);/*  w w w .  j  av  a2 s  . com*/
    mLastDelta = mLastMotion - event.getRawX();
    mLastMotion = event.getRawX();
    setState(TranslateState.PARTIAL);
    resetAnimator();
    setEnabled(mOverlay, true);
}

From source file:com.example.administrator.iclub21.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;//www .j a  va2  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;
        boolean toRight;
        toRight = (frontRect_left_begin > max_left / 2 ? true : false);
        if (Math.abs(wholeX) < 3) {
            toRight = !toRight;
        }
        moveToDest(toRight);
        break;
    default:
        break;
    }
    return true;
}

From source file:com.tmall.wireless.tangram.view.BannerView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    int action = ev.getAction();
    float x = ev.getRawX();
    float y = ev.getRawY();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        xDown = x;/*w  w w . j a  v a  2s  .c o m*/
        yDown = y;
        break;
    case MotionEvent.ACTION_MOVE:
        int xDiff = (int) (x - xDown);
        int yDiff = (int) (y - yDown);
        //y?x?
        if (Math.abs(xDiff) >= Math.abs(yDiff)) {
            getParent().requestDisallowInterceptTouchEvent(true);
        } else {
            getParent().requestDisallowInterceptTouchEvent(false);
        }
        break;
    }

    return false;
}

From source file:com.astuetz.viewpager.extensions.SwipeyTabsView.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    float x = event.getRawX();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mDragX = x;//from   www .  jav a  2 s .c  o m
        mPager.beginFakeDrag();
        break;
    case MotionEvent.ACTION_MOVE:
        if (!mPager.isFakeDragging())
            break;
        mPager.fakeDragBy((mDragX - x) * (-1));
        mDragX = x;
        break;
    case MotionEvent.ACTION_UP:
        if (!mPager.isFakeDragging())
            break;
        mPager.endFakeDrag();
        break;
    }

    return v.equals(this) ? true : super.onTouchEvent(event);
}

From source file:com.waz.zclient.pages.main.conversationlist.views.listview.SwipeListView.java

/**
 * Retrieves the child of the list view that is hit by the touch event. If it
 * is the first item or a disabled one, targetView is set to null.
 *///from  ww  w  .  ja va 2s.  co  m
private void getHitChild(MotionEvent motionEvent) {
    int[] listViewCoords = new int[2];
    getLocationOnScreen(listViewCoords);
    int x = (int) motionEvent.getRawX() - listViewCoords[0];
    int y = (int) motionEvent.getRawY() - listViewCoords[1];
    int childCount = getChildCount();
    View child;
    for (int i = 0; i < childCount; i++) {
        child = getChildAt(i);
        child.getHitRect(targetRect);
        if (targetRect.contains(x, y)) {
            int position = getPositionForView(child);
            boolean allowSwipe = child instanceof SwipeListRow && ((SwipeListRow) child).isSwipeable();
            if (allowSwipe) {
                if (position != targetChildPosition) {
                    closeItem();
                }
                targetChildPosition = position;

                // TODO: Investigate causes of ClassCastExecption. Perhaps archiving related views?
                try {
                    targetView = (SwipeListRow) child;
                    targetView
                            .setMaxOffset(allowSwipeAway ? viewWidth / 2 : listRowMenuIndicatorMaxSwipeOffset);
                } catch (ClassCastException e) {
                    Timber.e(e, "ClassCastException when swiping");
                }
                downX = motionEvent.getRawX();
            } else {
                closeItem();
                targetView = null;
            }
            return;
        }
    }

    // no child hit
    closeItem();
    targetView = null;
}