private void returnHandleToCenter() { if (autoReturnToCenter) { final int numberOfFrames = 5; final double intervalsX = (0 - touchX) / numberOfFrames; final double intervalsY = (0 - touchY) / numberOfFrames; for (int i = 0; i < numberOfFrames; i++) { final int j = i; postDelayed( new Runnable() { @Override public void run() { touchX += intervalsX; touchY += intervalsY; reportOnMoved(); invalidate(); if (moveListener != null && j == numberOfFrames - 1) { moveListener.OnReturnedToCenter(); } } }, i * 40); } if (moveListener != null) { moveListener.OnReleased(); } } }
@Override public boolean onTouchEvent(MotionEvent event) { int actionType = event.getAction(); if (actionType == MotionEvent.ACTION_MOVE) { int px = getMeasuredWidth() / 2; int py = getMeasuredHeight() / 2; int radius = Math.min(px, py) - handleInnerBoundaries; touchX = (event.getX() - px); touchX = Math.max(Math.min(touchX, radius), -radius); touchY = (event.getY() - py); touchY = Math.max(Math.min(touchY, radius), -radius); // Coordinates Log.d(TAG, "X:" + touchX + "|Y:" + touchY); // Pressure if (listener != null) { listener.OnMoved( (int) (touchX / radius * sensitivity), (int) (touchY / radius * sensitivity)); } invalidate(); } else if (actionType == MotionEvent.ACTION_UP) { returnHandleToCenter(); Log.d(TAG, "X:" + touchX + "|Y:" + touchY); } return true; }
private void reportOnMoved() { if (movementConstraint == CONSTRAIN_CIRCLE) constrainCircle(); else constrainBox(); calcUserCoordinates(); if (moveListener != null) { boolean rx = Math.abs(touchX - reportX) >= moveResolution; boolean ry = Math.abs(touchY - reportY) >= moveResolution; if (rx || ry) { this.reportX = touchX; this.reportY = touchY; // Log.d(TAG, String.format("moveListener.OnMoved(%d,%d)", (int)userX, (int)userY)); moveListener.OnMoved(userX, userY); } } }
private void returnHandleToCenter() { Handler handler = new Handler(); int numberOfFrames = 5; final double intervalsX = (0 - touchX) / numberOfFrames; final double intervalsY = (0 - touchY) / numberOfFrames; for (int i = 0; i < numberOfFrames; i++) { handler.postDelayed( new Runnable() { @Override public void run() { touchX += intervalsX; touchY += intervalsY; invalidate(); } }, i * 40); } if (listener != null) { listener.OnReleased(); } }