public void setInterfaceOpacity(float opacity) {
    if (opacity < 0 || opacity > 100.0f) {
      Log.w(TAG, "Can't set interface opacity. Invalid value: " + opacity);
      return;
    }

    joypadOpacity = opacity / 100f;

    Sprite joystick = renderer.getSprite(JOY_ID_LEFT);
    joystick.setAlpha(joypadOpacity);

    joystick = renderer.getSprite(JOY_ID_RIGHT);
    joystick.setAlpha(joypadOpacity);
  }
  public void setJoysticks() {
    JoystickBase rollAndPitchJoystick = joysticks[0];
    JoystickBase rudderAndThrottleJoystick = joysticks[1];

    if (rollAndPitchJoystick != null) {
      if (isLeftHanded) {
        joysticks[0].setAlign(Align.BOTTOM_RIGHT);
        joysticks[0].setAlpha(joypadOpacity);
      } else {
        joysticks[0].setAlign(Align.BOTTOM_LEFT);
        joysticks[0].setAlpha(joypadOpacity);
      }

      rollAndPitchJoystick.setNeedsUpdate();
    }

    if (rudderAndThrottleJoystick != null) {
      if (isLeftHanded) {
        joysticks[1].setAlign(Align.BOTTOM_LEFT);
        joysticks[1].setAlpha(joypadOpacity);
      } else {
        joysticks[1].setAlign(Align.BOTTOM_RIGHT);
        joysticks[1].setAlpha(joypadOpacity);
      }

      rudderAndThrottleJoystick.setNeedsUpdate();
    }

    for (int i = 0; i < joysticks.length; ++i) {
      JoystickBase joystick = joysticks[i];

      if (joystick != null) {
        joystick.setInverseYWhenDraw(true);

        int margin = context.getResources().getDimensionPixelSize(R.dimen.hud_joy_margin);

        joystick.setMargin(0, margin, bottomBarBg.getHeight() + margin, margin);
      }
    }

    renderer.removeSprite(JOY_ID_LEFT);
    renderer.removeSprite(JOY_ID_RIGHT);

    if (rollAndPitchJoystick != null) {
      if (isLeftHanded) {
        renderer.addSprite(JOY_ID_RIGHT, rollAndPitchJoystick);
      } else {
        renderer.addSprite(JOY_ID_LEFT, rollAndPitchJoystick);
      }
    }

    if (rudderAndThrottleJoystick != null) {
      if (isLeftHanded) {
        renderer.addSprite(JOY_ID_LEFT, rudderAndThrottleJoystick);
      } else {
        renderer.addSprite(JOY_ID_RIGHT, rudderAndThrottleJoystick);
      }
    }
  }
 public void onDestroy() {
   renderer.clearSprites();
   deviceOrientationManager.destroy();
 }
  public HudViewController(Activity context, HudViewControllerDelegate delegate) {
    this.delegate = delegate;
    this.context = context;
    Transmitter.sharedTransmitter().setBleConnectionManager(new BleConnectinManager(context));

    settings = ((HexMiniApplication) context.getApplication()).getAppSettings();

    joypadOpacity = settings.getInterfaceOpacity();
    isLeftHanded = settings.isLeftHanded();

    this.context = context;
    gestureDetector = new EnhancedGestureDetector(context, this);

    joysticks = new JoystickBase[2];

    glView = new GLSurfaceView(context);
    glView.setEGLContextClientVersion(2);

    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // LinearLayout hud = (LinearLayout)inflater.inflate(R.layout.hud, null);
    // LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
    // LayoutParams.FILL_PARENT);

    // hud.addView(glView, layoutParams);
    // glView.setBackgroundResource(R.drawable.settings_bg);

    context.setContentView(glView);

    renderer = new UIRenderer(context, null);

    initGLSurfaceView();

    Resources res = context.getResources();

    Image topBarBg = new Image(res, R.drawable.bar_top, Align.TOP_CENTER);
    topBarBg.setSizeParams(SizeParams.FILL_SCREEN, SizeParams.NONE); // Width水平伸缩至全屏,height保持不边
    topBarBg.setAlphaEnabled(false);

    bottomBarBg = new Image(res, R.drawable.bar_bottom, Align.BOTTOM_CENTER);
    bottomBarBg.setSizeParams(SizeParams.FILL_SCREEN, SizeParams.NONE);
    bottomBarBg.setAlphaEnabled(false);

    Image middleBg = new Image(res, R.drawable.bg_tile, Align.CENTER);
    middleBg.setAlpha(1f);
    middleBg.setVisible(false);
    middleBg.setSizeParams(
        SizeParams.FILL_SCREEN, SizeParams.FILL_SCREEN); // Width水平伸缩至全屏,height保持不边
    middleBg.setAlphaEnabled(true);

    Image bottomLeftSkrew = new Image(res, R.drawable.screw, Align.BOTTOM_LEFT);
    Image bottomRightSkrew = new Image(res, R.drawable.screw, Align.BOTTOM_RIGHT);

    Image logo = new Image(res, R.drawable.logo, Align.BOTTOM_LEFT);
    logo.setMargin(
        0,
        0,
        (int) res.getDimension(R.dimen.hud_logo_margin_bottom),
        (int) res.getDimension(R.dimen.hud_logo_margin_left));

    Image statusBar = new Image(res, R.drawable.status_bar, Align.TOP_LEFT);
    statusBar.setMargin(
        (int) res.getDimension(R.dimen.hud_status_bar_margin_top),
        0,
        0,
        (int) res.getDimension(R.dimen.hud_status_bar_margin_left));

    settingsBtn =
        new Button(
            res, R.drawable.btn_settings_normal, R.drawable.btn_settings_hl, Align.TOP_RIGHT);
    settingsBtn.setMargin(
        (int) res.getDimension(R.dimen.hud_btn_settings_margin_top),
        (int) res.getDimension(R.dimen.hud_btn_settings_margin_right),
        0,
        0);

    Button helpBtn =
        new Button(res, R.drawable.btn_help_normal, R.drawable.btn_help_hl, Align.TOP_RIGHT);
    helpBtn.setMargin(
        (int) res.getDimension(R.dimen.hud_btn_settings_margin_top),
        (int) res.getDimension(R.dimen.hud_btn_settings_margin_right) * 4,
        0,
        0);

    takeOffBtn =
        new Button(
            res, R.drawable.btn_take_off_normal, R.drawable.btn_take_off_hl, Align.BOTTOM_CENTER);
    stopBtn = new Button(res, R.drawable.btn_stop_normal, R.drawable.btn_stop_hl, Align.TOP_CENTER);

    String state =
        context.getResources().getString(R.string.settings_item_connection_state_not_conneceted);
    stateTextView = new Text(context, state, Align.TOP_LEFT);
    stateTextView.setMargin(
        (int) res.getDimension(R.dimen.hud_state_text_margin_top),
        0,
        0,
        (int) res.getDimension(R.dimen.hud_state_text_margin_left));
    stateTextView.setTextColor(Color.WHITE);
    stateTextView.setTypeface(FontUtils.TYPEFACE.Helvetica(context));
    stateTextView.setTextSize(res.getDimensionPixelSize(R.dimen.hud_state_text_size));

    altHoldToggleBtn =
        new ToggleButton(
            res,
            R.drawable.alt_hold_off,
            R.drawable.alt_hold_off_hl,
            R.drawable.alt_hold_on,
            R.drawable.alt_hold_on_hl,
            R.drawable.alt_hold_on,
            Align.TOP_LEFT);

    altHoldToggleBtn.setMargin(
        res.getDimensionPixelOffset(R.dimen.hud_alt_hold_toggle_btn_margin_top),
        0,
        0,
        res.getDimensionPixelOffset(R.dimen.hud_alt_hold_toggle_btn_margin_left));
    altHoldToggleBtn.setChecked(settings.isAltHoldMode());

    buttons = new Button[5];
    buttons[0] = settingsBtn;
    buttons[1] = takeOffBtn;
    buttons[2] = stopBtn;
    buttons[3] = altHoldToggleBtn;
    buttons[4] = helpBtn;

    renderer.addSprite(MIDLLE_BG_ID, middleBg);
    renderer.addSprite(TOP_BAR_ID, topBarBg);
    renderer.addSprite(BOTTOM_BAR_ID, bottomBarBg);
    renderer.addSprite(BOTTOM_LEFT_SKREW, bottomLeftSkrew);
    renderer.addSprite(BOTTOM_RIGHT_SKREW, bottomRightSkrew);
    renderer.addSprite(LOGO, logo);
    renderer.addSprite(STATUS_BAR, statusBar);
    renderer.addSprite(TAKE_OFF_BTN_ID, takeOffBtn);
    renderer.addSprite(STOP_BTN_ID, stopBtn);
    renderer.addSprite(SETTINGS_BTN_ID, settingsBtn);
    renderer.addSprite(ALT_HOLD_TOGGLE_BTN, altHoldToggleBtn);
    renderer.addSprite(STATE_TEXT_VIEW, stateTextView);
    renderer.addSprite(HELP_BTN, helpBtn);

    isAccMode = settings.isAccMode();
    deviceOrientationManager =
        new DeviceOrientationManager(new DeviceSensorManagerWrapper(this.context), this);
    deviceOrientationManager.onCreate();

    initJoystickListeners();

    helpBtn.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            Intent intent = new Intent(HudViewController.this.context, HelpActivity.class);
            HudViewController.this.context.startActivity(intent);
          }
        });

    if (isAccMode) {
      initJoysticks(JoystickType.ACCELERO);
    } else {
      initJoysticks(JoystickType.ANALOGUE);
    }

    initListeners();

    initChannels();

    if (settings.isHeadFreeMode()) {
      aux1Channel.setValue(1);
    } else {
      aux1Channel.setValue(-1);
    }

    if (settings.isAltHoldMode()) {
      aux2Channel.setValue(1);
    } else {
      aux2Channel.setValue(-1);
    }

    if (settings.isBeginnerMode()) {
      new AlertDialog.Builder(context)
          .setIcon(android.R.drawable.ic_dialog_alert)
          .setTitle(R.string.dialog_title_info)
          .setMessage(R.string.beginner_mode_info)
          .setPositiveButton(
              R.string.dialog_btn_ok,
              new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {}
              })
          .show();
    }
  }