public boolean onTouch(int action, float x, float y) { if (action == MotionEvent.ACTION_DOWN) { int additionWidth = (height - thumbWidth) / 2; if (thumbX - additionWidth <= x && x <= thumbX + thumbWidth + additionWidth && y >= 0 && y <= height) { pressed = true; thumbDX = (int) (x - thumbX); return true; } } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) { if (pressed) { if (action == MotionEvent.ACTION_UP && delegate != null) { delegate.onSeekBarDrag((float) thumbX / (float) (width - thumbWidth)); } pressed = false; return true; } } else if (action == MotionEvent.ACTION_MOVE) { if (pressed) { thumbX = (int) (x - thumbDX); if (thumbX < 0) { thumbX = 0; } else if (thumbX > width - thumbWidth) { thumbX = width - thumbWidth; } return true; } } return false; }
@Override public boolean onTouchEvent(MotionEvent event) { if (event == null) { return false; } float x = event.getX(); float y = event.getY(); float thumbX = (int) ((getMeasuredWidth() - thumbWidth) * progress); if (event.getAction() == MotionEvent.ACTION_DOWN) { int additionWidth = (getMeasuredHeight() - thumbWidth) / 2; if (thumbX - additionWidth <= x && x <= thumbX + thumbWidth + additionWidth && y >= 0 && y <= getMeasuredHeight()) { pressed = true; thumbDX = (int) (x - thumbX); getParent().requestDisallowInterceptTouchEvent(true); invalidate(); return true; } } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) { if (pressed) { if (event.getAction() == MotionEvent.ACTION_UP && delegate != null) { delegate.onSeekBarDrag(thumbX / (float) (getMeasuredWidth() - thumbWidth)); } pressed = false; invalidate(); return true; } } else if (event.getAction() == MotionEvent.ACTION_MOVE) { if (pressed) { thumbX = (int) (x - thumbDX); if (thumbX < 0) { thumbX = 0; } else if (thumbX > getMeasuredWidth() - thumbWidth) { thumbX = getMeasuredWidth() - thumbWidth; } progress = thumbX / (getMeasuredWidth() - thumbWidth); invalidate(); return true; } } return false; }