Esempio n. 1
1
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    xPosition = (int) event.getX();
    yPosition = (int) event.getY();
    double abs =
        Math.sqrt(
            (xPosition - centerX) * (xPosition - centerX)
                + (yPosition - centerY) * (yPosition - centerY));
    if (abs > joystickRadius) {
      xPosition = (int) ((xPosition - centerX) * joystickRadius / abs + centerX);
      yPosition = (int) ((yPosition - centerY) * joystickRadius / abs + centerY);
    }

    invalidate();
    if (event.getAction() == MotionEvent.ACTION_UP) {
      xPosition = (int) centerX;
      yPosition = (int) centerY;
      Log.i(logTag, "ACTION_UP: " + xPosition + ", " + yPosition);
      thread.interrupt();
      onJoystickMoveListener.onValueChanged(getAngle(), getPower(), getDirection());
    }
    if (onJoystickMoveListener != null && event.getAction() == MotionEvent.ACTION_DOWN) {
      if (thread != null && thread.isAlive()) {
        thread.interrupt();
      }
      thread = new Thread(this);
      thread.start();
      onJoystickMoveListener.onValueChanged(getAngle(), getPower(), getDirection());
      Log.i(logTag, "ACTION_DOWN");
    }
    return true;
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    xPosition = (int) event.getX();
    yPosition = (int) event.getY();

    if (firstToruc) {
      offsetX = xPosition - centerX;
      offsetY = yPosition - centerY;
      firstToruc = false;
    }

    xPosition -= offsetX;
    if (tMode == false) {
      yPosition -= offsetY;
    }

    /*/
    double abs = Math.sqrt(((xPosition) - centerX) * ((xPosition) - centerX)
    		+ ((yPosition) - centerY) * ((yPosition) - centerY));


    if (abs > joystickRadius) {
    	xPosition = (int) ((xPosition - centerX) * joystickRadius / abs + centerX);
    	yPosition = (int) ((yPosition - centerY) * joystickRadius / abs + centerY);
    }
    */
    // check join stick out rectan
    if (xPosition > (int) (centerX * 1.68)) {
      xPosition = (int) (centerX * 1.68);
    }
    if (xPosition < (int) (centerX * 0.32)) {
      xPosition = (int) (centerX * 0.32);
    }

    if (yPosition > (int) (centerY * 1.68)) {
      yPosition = (int) (centerY * 1.68);
    }
    if (yPosition < (int) (centerY * 0.32)) {
      yPosition = (int) (centerY * 0.32);
    }
    //
    invalidate();
    if (event.getAction() == MotionEvent.ACTION_UP) {
      xPosition = (int) centerX;
      if (tMode == false) {
        yPosition = (int) centerY;
        // yPosition -= offsetY;
      }

      firstToruc = true;
      thread.interrupt();
      if (onJoystickMoveListener != null)
        onJoystickMoveListener.onValueChanged(getAngle(), getPower(), getDirection());
    }
    if (onJoystickMoveListener != null && event.getAction() == MotionEvent.ACTION_DOWN) {
      if (thread != null && thread.isAlive()) {
        thread.interrupt();
      }
      thread = new Thread(this);
      thread.start();
      if (onJoystickMoveListener != null)
        onJoystickMoveListener.onValueChanged(getAngle(), getPower(), getDirection());
    }
    return true;
  }