Code example for MotionEvent

Methods: findPointerIndexgetActiongetPointerCountgetPointerIdgetX

0
		if (!isEnabled())
			return false; 
 
		int pointerIndex;
 
		final int action = event.getAction();
		switch (action & MotionEvent.ACTION_MASK) {
 
		case MotionEvent.ACTION_DOWN:
			// Remember where the motion event started 
			mActivePointerId = event.getPointerId(event.getPointerCount() - 1);
			pointerIndex = event.findPointerIndex(mActivePointerId);
			mDownMotionX = event.getX(pointerIndex);
 
			pressedThumb = evalPressedThumb(mDownMotionX);
 
			// Only handle thumb presses. 
			if (pressedThumb == null)
				return super.onTouchEvent(event);
 
			setPressed(true);
			invalidate();
			onStartTrackingTouch(); 
			trackTouchEvent(event);
			attemptClaimDrag(); 
 
			break; 
		case MotionEvent.ACTION_MOVE:
			if (pressedThumb != null) {
 
				if (mIsDragging) {
					trackTouchEvent(event);
				} else { 
					// Scroll to follow the motion event 
					pointerIndex = event.findPointerIndex(mActivePointerId);
					final float x = event.getX(pointerIndex);
 
					if (Math.abs(x - mDownMotionX) > mScaledTouchSlop) {
						setPressed(true);
						invalidate();
						onStartTrackingTouch(); 
						trackTouchEvent(event);
						attemptClaimDrag(); 
					} 
				} 
 
				if (notifyWhileDragging && listener != null) {
					listener.onRangeSeekBarValuesChanged(this,
							getSelectedMinValue(), getSelectedMaxValue()); 
				} 
			} 
			break; 
		case MotionEvent.ACTION_UP:
			if (mIsDragging) {
				trackTouchEvent(event);
				onStopTrackingTouch(); 
				setPressed(false);
			} else { 
				// Touch up when we never crossed the touch slop threshold 
				// should be interpreted as a tap-seek to that location. 
				onStartTrackingTouch(); 
				trackTouchEvent(event);
				onStopTrackingTouch(); 
			} 
 
			pressedThumb = null;
			invalidate();
			if (listener != null) {
				listener.onRangeSeekBarValuesChanged(this,
						getSelectedMinValue(), getSelectedMaxValue()); 
			} 
			break; 
		case MotionEvent.ACTION_POINTER_DOWN: {
			final int index = event.getPointerCount() - 1;
			// final int index = ev.getActionIndex(); 
			mDownMotionX = event.getX(index);
			mActivePointerId = event.getPointerId(index);
			invalidate();
			break; 
		} 
		case MotionEvent.ACTION_POINTER_UP:
			onSecondaryPointerUp(event);
			invalidate();
			break; 
		case MotionEvent.ACTION_CANCEL:
			if (mIsDragging) {
				onStopTrackingTouch(); 
				setPressed(false);
			} 
			invalidate(); // see above explanation
			break; 
		} 
		return true; 
	} 
 
	private final void onSecondaryPointerUp(MotionEvent ev) {
		final int pointerIndex = (ev.getAction() & ACTION_POINTER_INDEX_MASK) >> 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. 
			// TODO: Make this decision more intelligent. 
			final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
			mDownMotionX = ev.getX(newPointerIndex);
			mActivePointerId = ev.getPointerId(newPointerIndex);
		} 
	} 
 
	private final void trackTouchEvent(MotionEvent event) {
		final int pointerIndex = event.findPointerIndex(mActivePointerId);
		final float x = event.getX(pointerIndex);
 
		if (Thumb.MIN.equals(pressedThumb)) {
			setNormalizedMinValue(screenToNormalized(x));
		} else if (Thumb.MAX.equals(pressedThumb)) {
			setNormalizedMaxValue(screenToNormalized(x));