Java Code Examples for android.view.MotionEvent#getY()

The following examples show how to use android.view.MotionEvent#getY() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PhotoViewAttacher.java    From Dashboard with MIT License 6 votes vote down vote up
@Override
public final boolean onDoubleTap(MotionEvent ev) {
    try {
        float scale = getScale();
        float x = ev.getX();
        float y = ev.getY();

        if (scale < mMidScale) {
            setScale(mMidScale, x, y, true);
        } else if (scale >= mMidScale && scale < mMaxScale) {
            setScale(mMaxScale, x, y, true);
        } else {
            setScale(mMinScale, x, y, true);
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        // Can sometimes happen when getX() and getY() is called
    }

    return true;
}
 
Example 2
Source File: KeyboardActivity.java    From Android-UtilCode with Apache License 2.0 5 votes vote down vote up
private boolean isShouldHideKeyboard(View v, MotionEvent event) {
    if (v != null && (v instanceof EditText)) {
        int[] l = {0, 0};
        v.getLocationInWindow(l);
        int left = l[0],
                top = l[1],
                bottom = top + v.getHeight(),
                right = left + v.getWidth();
        return !(event.getX() > left && event.getX() < right
                && event.getY() > top && event.getY() < bottom);
    }
    return false;
}
 
Example 3
Source File: QueueView.java    From DS4Android with MIT License 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            float downX = event.getX() - mCoo.x;
            float downY = event.getY() - mCoo.y;

            for (int i = 0; i < CTRL_POS.length; i++) {
                //区域判定
                if (JudgeMan.judgeCircleArea(
                        CTRL_POS[i].x, CTRL_POS[i].y,
                        downX, downY, CTRL_RADIUS * 1.2f)) {
                    if (mOnCtrlClickListener != null) {
                        switch (i) {
                            case 0://插入尾部
                                mOnCtrlClickListener.onAdd(this);
                                break;
                            case 1://更新
                                mOnCtrlClickListener.onRemove(this);
                                break;
                            case 2://查找
                                mOnCtrlClickListener.onFind(this);
                                break;
                        }
                        CTRL_COLOR[i] = 0xff54E1F8;//点击更换颜色
                    }
                }
            }
            break;
        case MotionEvent.ACTION_UP://还原颜色
            CTRL_COLOR[0] = 0xff1EF519;
            CTRL_COLOR[1] = 0xff2992F2;
            CTRL_COLOR[2] = 0xffB946F4;
            break;
    }
    invalidate();
    return true;
}
 
Example 4
Source File: FlingViewGroup.java    From VCL-Android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (getChildCount() == 0)
        return false;

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

    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mLastX = x;
            mLastInterceptDownY = ev.getY();
            mInitialMotionX = x;
            mInitialMotionY = y;
            mTouchState = mScroller.isFinished() ?
                    TOUCH_STATE_REST : TOUCH_STATE_MOVE;
            mInterceptTouchState = TOUCH_STATE_REST;
            break;
        case MotionEvent.ACTION_MOVE:
            if (mInterceptTouchState == TOUCH_STATE_MOVE)
                return false;
            if (Math.abs(mLastInterceptDownY - y) > mTouchSlop)
                mInterceptTouchState = TOUCH_STATE_MOVE;
            if (Math.abs(mLastX - x) > mTouchSlop)
                mTouchState = TOUCH_STATE_MOVE;
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mInterceptTouchState = TOUCH_STATE_REST;
            break;
    }

    return mTouchState == TOUCH_STATE_MOVE;
}
 
Example 5
Source File: LinkifyTextView.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    // Let the parent or grandparent of TextView to handles click aciton.
    // Otherwise click effect like ripple will not work, and if touch area
    // do not contain a url, the TextView will still get MotionEvent.
    // onTouchEven must be called with MotionEvent.ACTION_DOWN for each touch
    // action on it, so we analyze touched url here.
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        mCurrentSpan = null;

        if (getText() instanceof Spanned) {
            // Get this code from android.text.method.LinkMovementMethod.
            // Work fine !
            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= getTotalPaddingLeft();
            y -= getTotalPaddingTop();

            x += getScrollX();
            y += getScrollY();

            Layout layout = getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            Object[] spans = ((Spanned)getText()).getSpans(off, off, Object.class);

            for (Object span : spans) {
                if (Utilities.contain(SUPPORTED_SPAN_TYPE, span.getClass())) {
                    mCurrentSpan = span;
                    break;
                }
            }
        }
    }

    return super.onTouchEvent(event);
}
 
Example 6
Source File: EclairGestureDetector.java    From MoeGallery with GNU General Public License v3.0 5 votes vote down vote up
@Override
float getActiveY(MotionEvent ev) {
    try {
        return ev.getY(mActivePointerIndex);
    } catch (Exception e) {
        return ev.getY();
    }
}
 
Example 7
Source File: PZSImageView.java    From Zom-Android-XMPP with GNU General Public License v3.0 5 votes vote down vote up
/** Determine the space between the first two fingers */
private float spacing(MotionEvent event) {
	// ...
	float x = event.getX(0) - event.getX(1);
	float y = event.getY(0) - event.getY(1);
	return (float)Math.sqrt(x * x + y * y);
}
 
Example 8
Source File: DefaultOnDoubleTapListener.java    From PicKing with Apache License 2.0 5 votes vote down vote up
@Override public boolean onSingleTapConfirmed(MotionEvent e) {

        if (mAttacher == null) {
            return false;
        }
        DraweeView<GenericDraweeHierarchy> draweeView = mAttacher.getDraweeView();
        if (draweeView == null) {
            return false;
        }

        if (mAttacher.getOnPhotoTapListener() != null) {
            final RectF displayRect = mAttacher.getDisplayRect();

            if (null != displayRect) {
                final float x = e.getX(), y = e.getY();
                if (displayRect.contains(x, y)) {
                    float xResult = (x - displayRect.left) / displayRect.width();
                    float yResult = (y - displayRect.top) / displayRect.height();
                    mAttacher.getOnPhotoTapListener().onPhotoTap(draweeView, xResult, yResult);
                    return true;
                }
            }
        }

        if (mAttacher.getOnViewTapListener() != null) {
            mAttacher.getOnViewTapListener().onViewTap(draweeView, e.getX(), e.getY());
            return true;
        }

        return false;
    }
 
Example 9
Source File: SideBar.java    From CloudPan with Apache License 2.0 5 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
	final int action = event.getAction();
	final float y = event.getY();// ���y����
	final int oldChoose = choose;
	final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
	final int c = (int) (y / getHeight() * b.length);// ���y������ռ�ܸ߶ȵı���*b����ij��Ⱦ͵��ڵ��b�еĸ���.

	switch (action) {
	case MotionEvent.ACTION_UP:
		setBackgroundDrawable(new ColorDrawable(0x00000000));
		choose = -1;//
		invalidate();
		if (mTextDialog != null) {
			mTextDialog.setVisibility(View.INVISIBLE);
		}
		break;

	default:
		setBackgroundResource(R.drawable.sidebar_background);
		if (oldChoose != c) {
			if (c >= 0 && c < b.length) {
				if (listener != null) {
					listener.onTouchingLetterChanged(b[c]);
				}
				if (mTextDialog != null) {
					mTextDialog.setText(b[c]);
					mTextDialog.setVisibility(View.VISIBLE);
				}
				
				choose = c;
				invalidate();
			}
		}

		break;
	}
	return true;
}
 
Example 10
Source File: WaveSideBarView.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    final float y = event.getY();
    final float x = event.getX();

    final int oldChoose = mChoose;
    final int newChoose = (int) (y / mHeight * mLetters.size());

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            if (x < mWidth - 2 * mRadius) {
                return false;
            }
            startAnimator(mRatio, 1.0f);
            break;
        case MotionEvent.ACTION_MOVE:

            mCenterY = (int) y;
            if (oldChoose != newChoose) {
                if (newChoose >= 0 && newChoose < mLetters.size()) {
                    mChoose = newChoose;
                    if (listener != null) {
                        listener.onLetterChange(mLetters.get(newChoose));
                    }
                }
            }
            invalidate();
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:

            startAnimator(mRatio, 0f);
            mChoose = -1;
            break;
        default:
            break;
    }
    return true;
}
 
Example 11
Source File: ResolverDrawerLayout.java    From android-bottomsheet with ISC License 5 votes vote down vote up
private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = ev.getActionIndex();
    final int pointerId = ev.getPointerId(pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mInitialTouchX = ev.getX(newPointerIndex);
        mInitialTouchY = mLastTouchY = ev.getY(newPointerIndex);
        mActivePointerId = ev.getPointerId(newPointerIndex);
    }
}
 
Example 12
Source File: BarGraph.java    From android-open-project-demo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(@NotNull MotionEvent event) {

    Point point = new Point();
    point.x = (int) event.getX();
    point.y = (int) event.getY();

    int count = 0;
    for (Bar bar : points) {
        Region r = new Region();
        r.setPath(bar.getPath(), bar.getRegion());
        if (r.contains(point.x, point.y) && event.getAction() == MotionEvent.ACTION_DOWN) {
            indexSelected = count;
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (r.contains(point.x, point.y) && listener != null) {
                listener.onClick(indexSelected);
            }
            indexSelected = -1;
        }
        count++;
    }

    if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP) {
        shouldUpdate = true;
        postInvalidate();
    }


    return true;
}
 
Example 13
Source File: VersionedGestureDetector.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            mActivePointerId = ev.getPointerId(0);
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            mActivePointerId = INVALID_POINTER_ID;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mActivePointerId = ev.getPointerId(newPointerIndex);
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
            }
            break;
    }

    mActivePointerIndex = ev.findPointerIndex(mActivePointerId != INVALID_POINTER_ID ? mActivePointerId : 0);
    return super.onTouchEvent(ev);
}
 
Example 14
Source File: ScaleDragDetector.java    From PicKing with Apache License 2.0 5 votes vote down vote up
private float getActiveY(MotionEvent ev) {
    try {
        return MotionEventCompat.getY(ev, mActivePointerIndex);
    } catch (Exception e) {
        return ev.getY();
    }
}
 
Example 15
Source File: Frame_ListView_OutConfict1.java    From zone-sdk with MIT License 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
	boolean use=false;
	switch (ev.getAction()) {
	case MotionEvent.ACTION_DOWN :
		downx=(int) ev.getX();
		downy=(int) ev.getY();
		Log.i(TAG, "onTouchEvent ACTION_DOWN");
		break;
	case MotionEvent.ACTION_MOVE:
		Log.i(TAG, "onTouchEvent ACTION_MOVE");
		int deltaX=(int)ev.getX()-downx;
		int deltaY=(int)ev.getY()-downy;
		if(Math.abs(deltaX)<Math.abs(deltaY)){
			Log.i(TAG, "ACTION_MOVE use   true \tX:"+ev.getX()+"  \ty:"+ev.getY());
			use=true;
		}else{
			use=false;
			Log.i(TAG, " ACTION_MOVE use   false");
		}
		break;
	case MotionEvent.ACTION_UP:
		break;

	default:
		break;
	}
	if(use){
		return super.onTouchEvent(ev);	
	}else
		return false;
	
}
 
Example 16
Source File: Slidr.java    From android-slidr with Apache License 2.0 4 votes vote down vote up
public boolean clicked(MotionEvent e) {
    return e.getX() >= x && e.getX() <= x + width
            && e.getY() >= y && e.getY() < y + height;
}
 
Example 17
Source File: SlideListView2.java    From Gizwits-SmartBuld_Android with MIT License 4 votes vote down vote up
/**
 * 处理我们拖动ListView item的逻辑
 */
@Override
public boolean onTouchEvent(MotionEvent ev) {

	final int action = ev.getAction();
	int lastX = (int) ev.getX();

	switch (action) {
	case MotionEvent.ACTION_DOWN:
		System.out.println("touch-->" + "down");

		/* 当前模式不允许滑动,则直接返回,交给ListView自身去处理 */
		if (this.mode == MOD_FORBID) {
			return super.onTouchEvent(ev);
		}

		// 如果处于侧滑完成状态,侧滑回去,并直接返回
		if (isSlided) {
			scrollBack();
			return false;
		}
		// 假如scroller滚动还没有结束,我们直接返回
		if (!scroller.isFinished()) {
			return false;
		}
		downX = (int) ev.getX();
		downY = (int) ev.getY();

		slidePosition = pointToPosition(downX, downY);

		// 无效的position, 不做任何处理
		if (slidePosition == AdapterView.INVALID_POSITION) {
			return super.onTouchEvent(ev);
		}

		// 获取我们点击的item view
		itemView = getChildAt(slidePosition - getFirstVisiblePosition());

		/* 此处根据设置的滑动模式,自动获取左侧或右侧菜单的长度 */
		if (this.mode == MOD_BOTH) {
			this.leftLength = -itemView.getPaddingLeft();
			this.rightLength = -itemView.getPaddingRight();
		} else if (this.mode == MOD_LEFT) {
			this.leftLength = -itemView.getPaddingLeft();
		} else if (this.mode == MOD_RIGHT) {
			this.rightLength = -itemView.getPaddingRight();
		}

		break;
	case MotionEvent.ACTION_MOVE:
		System.out.println("touch-->" + "move");

		if (!canMove && slidePosition != AdapterView.INVALID_POSITION
				&& (Math.abs(ev.getX() - downX) > mTouchSlop && Math.abs(ev.getY() - downY) < mTouchSlop)) {
			int offsetX = downX - lastX;
			if (offsetX > 0 && (this.mode == MOD_BOTH || this.mode == MOD_RIGHT)) {
				/* 从右向左滑 */
				canMove = true;
			} /*
				 * else if(offsetX < 0 && (this.mode == MOD_BOTH ||
				 * this.mode == MOD_LEFT)){ 从左向右滑 canMove = true; }
				 */else {
				canMove = false;
			}
			/* 此段代码是为了避免我们在侧向滑动时同时出发ListView的OnItemClickListener时间 */
			MotionEvent cancelEvent = MotionEvent.obtain(ev);
			cancelEvent.setAction(
					MotionEvent.ACTION_CANCEL | (ev.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
			onTouchEvent(cancelEvent);
		}
		if (canMove) {
			/* 设置此属性,可以在侧向滑动时,保持ListView不会上下滚动 */
			requestDisallowInterceptTouchEvent(true);

			// 手指拖动itemView滚动, deltaX大于0向左滚动,小于0向右滚
			int deltaX = downX - lastX;
			if (deltaX < 0 && (this.mode == MOD_BOTH || this.mode == MOD_LEFT)) {
				/* 向左滑 */
				itemView.scrollTo(deltaX, 0);
			} else if (deltaX > 0 && (this.mode == MOD_BOTH || this.mode == MOD_RIGHT)) {
				/* 向右滑 */
				itemView.scrollTo(deltaX, 0);
			} else {
				itemView.scrollTo(0, 0);
			}
			return true; // 拖动的时候ListView不滚动
		}
	case MotionEvent.ACTION_UP:
		System.out.println("touch-->" + "up");
		if (canMove) {
			canMove = false;
			scrollByDistanceX();
		}
		break;
	}

	// 否则直接交给ListView来处理onTouchEvent事件
	return super.onTouchEvent(ev);
}
 
Example 18
Source File: MaterialRangeSlider.java    From MaterialRangeSlider with Apache License 2.0 4 votes vote down vote up
private boolean isTouchingMinTarget(int pointerIndex, MotionEvent event) {
    return event.getX(pointerIndex) > minPosition - DEFAULT_TOUCH_TARGET_SIZE
            && event.getX(pointerIndex) < minPosition + DEFAULT_TOUCH_TARGET_SIZE
            && event.getY(pointerIndex) > midY - DEFAULT_TOUCH_TARGET_SIZE
            && event.getY(pointerIndex) < midY + DEFAULT_TOUCH_TARGET_SIZE;
}
 
Example 19
Source File: Main.java    From Simple-Brick-Games with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onTouch(View v, MotionEvent event) {                                             //for button input, it is with multi touch but my program code doesn't work very good
    int pointerID = event.getPointerId(0) + 1;
    int pointerIndex = event.findPointerIndex(pointerID - 1);

    int[] location1 = new int[2];
    v.getLocationOnScreen(location1);

    float X = event.getX(pointerIndex) + location1[0];
    float Y = event.getY(pointerIndex) + location1[1];

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
            for (int i = 0; i < 5; i++) {
                int[] location = new int[2];
                mButton[i].getLocationOnScreen(location);

                if (mButtonPressed[i] != pointerID && Game.sEvent == 0 && mPause!=1
                        && X >= location[0]
                        && X <= location[0] + mButton[i].getWidth()
                        && Y >= location[1]
                        && Y <= location[1] + mButton[i].getHeight()) {
                    for (int j = 0; j < 5; j++) {
                        if (mButtonPressed[j] == pointerID) {
                            mButtonPressed[j] = 0;
                            mButtonPressedCounter[j] = 0;
                        }
                    }

                    vibrate();
                    mButtonPressed[i] = pointerID;
                    input = i + 1;
                    getCurrentGame().input();
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            for (int i = 0; i < 5; i++) {
                if (mButtonPressed[i] == pointerID) {
                    mButtonPressed[i] = 0;
                    mButtonPressedCounter[i] = 0;
                }
            }
            break;
    }

    return true;
}
 
Example 20
Source File: GestureDetector.java    From Carbon with Apache License 2.0 4 votes vote down vote up
private void handleTwoPointers(MotionEvent event) {
    clicks = 0;
    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:
        case MotionEvent.ACTION_POINTER_2_DOWN:
        case MotionEvent.ACTION_MOVE:
            if (!transforming) {
                transforming = true;
                handler.removeCallbacks(pressHandler);
                pressHandler = null;
                handler.removeCallbacks(longPressHandler);
                longPressHandler = null;
                handler.removeCallbacks(tapHandler);
                tapHandler = null;

                prevRotation = (float) Math.atan2(event.getY(0) - event.getY(1), event.getX(0) - event.getX(1));
                prevDist = (float) Math.sqrt(Math.pow(event.getX(0) - event.getX(1), 2) +
                        Math.pow(event.getY(0) - event.getY(1), 2));
                prevCenterX = (event.getX(0) + event.getX(1)) / 2;
                prevCenterY = (event.getY(0) + event.getY(1)) / 2;
            } else {
                float dist = (float) Math.sqrt(Math.pow(event.getX(0) - event.getX(1), 2) +
                        Math.pow(event.getY(0) - event.getY(1), 2));
                float cx = (event.getX(0) + event.getX(1)) / 2;
                float cy = (event.getY(0) + event.getY(1)) / 2;
                float dx = cx - prevCenterX;
                float dy = cy - prevCenterY;

                float rotation = (float) Math.atan2(event.getY(0) - event.getY(1), event.getX(0) - event.getX(1));
                float scale = dist / prevDist;
                float rx = rotation - prevRotation;

                fireTransformEvent(event, cx, cy, dx, dy, rx, scale);

                prevCenterX = cx;
                prevCenterY = cy;
                prevRotation = rotation;
                prevDist = dist;
            }
            break;
    }
}