Android Open Source - nxt-remote-controller Joystick View






From Project

Back to project page nxt-remote-controller.

License

The source code is released under:

MIT License

If you think the Android project nxt-remote-controller listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package git.egatuts.nxtremotecontroller.views;
//from   w  w  w  .  j a  v a 2  s .c  om
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import git.egatuts.nxtremotecontroller.R;

public class JoystickView extends View {

  private int WIDTH;
  private int HEIGHT;
  private float RADIUS;
  private float JOYSTICK_RADIUS;
  private int centerX;
  private int centerY;
  private int posX = -1;
  private int posY = -1;
  private float lastPosX;
  private float lastPosY;
  private double angle;
  private double module;
  private int joystickColor;
  private int joystickStroke;
  private Paint paint;
  private Path path;
  private OnChangePosition listener;

  public JoystickView (Context context) {
    super(context);
  }

  public JoystickView (Context context, AttributeSet attrs) {
    super(context, attrs);
    this.init(context, attrs);
  }

  public JoystickView (Context context, AttributeSet attrs, int defaultStyle) {
    super(context, attrs, defaultStyle);
    this.init(context, attrs);
  }

  private void init (Context context, AttributeSet attrs) {
    if (isInEditMode()) return;
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.JoystickView);
    this.joystickColor = typedArray.getInt(R.styleable.JoystickView_joystickColor, 0xBB333333);
    this.joystickStroke = typedArray.getInt(R.styleable.JoystickView_joystickStroke, 0xBB222222);
    this.resize(this.getWidth(), this.getHeight(), false);
    this.paint = new Paint();
    this.paint.setAntiAlias(true);
    this.path = new Path();
  }

  private void resize (int w, int h, boolean ready) {
    this.WIDTH = w;
    this.HEIGHT = h;
    if (this.WIDTH < this.HEIGHT) {
      this.centerX = (this.WIDTH / 2) + this.getLeft();
      this.centerY = (this.WIDTH / 2) + this.getTop() + ((this.HEIGHT - this.WIDTH) / 2);
      this.HEIGHT = this.WIDTH;
    } else {
      this.centerX = (this.HEIGHT / 2) + this.getLeft() + ((this.WIDTH - this.HEIGHT) / 2);
      this.centerY = (this.HEIGHT / 2) + this.getTop();
      this.WIDTH = this.HEIGHT;
    }
    this.RADIUS = (float) ((double) (this.WIDTH / 2) * (1 - 0.2));
    this.JOYSTICK_RADIUS = (float) ((double) (this.WIDTH / 2) * 0.2);
    /*if (ready) {
      ViewGroup.LayoutParams params = this.getLayoutParams();
      params.width = this.WIDTH;
      params.height = this.HEIGHT;
      this.setLayoutParams(params);
    }*/
  }

  public void setOnChangePositionListener (OnChangePosition listener) {
    this.listener = listener;
  }

  public OnChangePosition getOnChangePositionListener () {
    return this.listener;
  }

  public float getXValue () {
    return (float) (this.posX - this.centerX) / this.RADIUS;
  }

  public float getYValue () {
    return (float) (this.centerY - this.posY) / this.RADIUS;
  }

  public double getAngle () {
    return this.angle;
  }

  public double getModule () {
    return this.module;
  }

  public double getMaximumModule () {
    return this.RADIUS;
  }

  @Override
  public void onSizeChanged (int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    this.resize(w, h, true);
  }

  @Override
  public boolean onTouchEvent (MotionEvent event) {
    float x = event.getX(0);
    float y = event.getY(0);
    int action = event.getAction();
    int down = MotionEvent.ACTION_DOWN;
    int up = MotionEvent.ACTION_UP;
    int move = MotionEvent.ACTION_MOVE;
    int cancel = MotionEvent.ACTION_CANCEL;
    if (action == down || action == move) {
      double offsetX = x - this.centerX;
      double offsetY = y - this.centerY;
      this.angle = Math.atan2(offsetY, offsetX);
      this.posX = (int) x;
      this.posY = (int) y;
      double moduleTouch = Math.sqrt(offsetX * offsetX + offsetY * offsetY);
      if (moduleTouch > this.RADIUS) {
        this.posX = (int) (this.centerX + this.RADIUS * Math.cos(this.angle));
        this.posY = (int) (this.centerY + this.RADIUS * Math.sin(this.angle));
      }
      this.module = Math.sqrt(Math.pow(this.posX - this.centerX, 2) + Math.pow(this.posY - this.centerY, 2));
      if (this.listener != null) this.listener.onChange(this.getXValue(), this.getYValue());
      invalidate();
    } else if (action == up || action == cancel) {
      if (this.posX == this.lastPosX || this.posY == this.lastPosY) {
        this.posX = this.centerX;
        this.posY = this.centerY;
      }
      this.module = Math.sqrt(Math.pow(this.posX - this.centerX, 2) + Math.pow(this.posY - this.centerY, 2));
      if (this.listener != null) this.listener.onChange(this.getXValue(), this.getYValue());
      invalidate();
    }
    this.lastPosX = this.posX;
    this.lastPosY = this.posY;
    return true;
  }

  @Override
  public void onDraw (Canvas canvas) {
    super.onDraw(canvas);
    this.path.reset();
    int joystickX = this.posX < 0 ? this.centerX : this.posX;
    int joystickY = this.posY < 0 ? this.centerY : this.posY;
    this.path.addCircle(joystickX, joystickY, this.JOYSTICK_RADIUS, Path.Direction.CW);
    this.paint.setStyle(Paint.Style.FILL);
    this.paint.setColor(this.joystickColor);
    canvas.drawPath(path, this.paint);
    this.paint.setStyle(Paint.Style.STROKE);
    this.paint.setStrokeWidth(3.0f);
    this.paint.setColor(this.joystickStroke);
    this.paint.setAlpha(255);
    canvas.drawPath(path, this.paint);
  }

  public interface OnChangePosition {
    public void onChange (float x, float y);
  }

}




Java Source Code List

.OldPadControllerFragment.java
com.andexert.library.ApplicationTest.java
com.andexert.library.RippleView.java
com.andexert.rippleeffect.ApplicationTest.java
com.andexert.rippleeffect.CustomAdapter.java
com.andexert.rippleeffect.MainActivity.java
com.andexert.rippleeffect.OnTapListener.java
com.gc.materialdesign.utils.Utils.java
com.gc.materialdesign.views.ButtonFlat.java
com.gc.materialdesign.views.ButtonFloatSmall.java
com.gc.materialdesign.views.ButtonFloat.java
com.gc.materialdesign.views.ButtonIcon.java
com.gc.materialdesign.views.ButtonRectangle.java
com.gc.materialdesign.views.Button.java
com.gc.materialdesign.views.Card.java
com.gc.materialdesign.views.CheckBox.java
com.gc.materialdesign.views.CustomView.java
com.gc.materialdesign.views.LayoutRipple.java
com.gc.materialdesign.views.ProgressBarCircularIndeterminate.java
com.gc.materialdesign.views.ProgressBarDeterminate.java
com.gc.materialdesign.views.ProgressBarIndeterminateDeterminate.java
com.gc.materialdesign.views.ProgressBarIndeterminate.java
com.gc.materialdesign.views.RippleView.java
com.gc.materialdesign.views.ScrollView.java
com.gc.materialdesign.views.Slider.java
com.gc.materialdesign.views.Switch.java
com.gc.materialdesign.widgets.ColorSelector.java
com.gc.materialdesign.widgets.Dialog.java
com.gc.materialdesign.widgets.SnackBar.java
git.egatuts.nxtremotecontroller.ApplicationTest.java
git.egatuts.nxtremotecontroller.GlobalUtils.java
git.egatuts.nxtremotecontroller.activity.ActivityPendingTransition.java
git.egatuts.nxtremotecontroller.activity.BaseActivity.java
git.egatuts.nxtremotecontroller.activity.ControllerActivity.java
git.egatuts.nxtremotecontroller.activity.DefaultActivityPendingTransition.java
git.egatuts.nxtremotecontroller.activity.MainActivity.java
git.egatuts.nxtremotecontroller.activity.SettingsActivity.java
git.egatuts.nxtremotecontroller.bluetooth.BluetoothConstants.java
git.egatuts.nxtremotecontroller.bluetooth.BluetoothUtils.java
git.egatuts.nxtremotecontroller.bluetooth.NXTConnector.java
git.egatuts.nxtremotecontroller.device.PairedDeviceAdapter.java
git.egatuts.nxtremotecontroller.device.PairedDeviceItemClickListener.java
git.egatuts.nxtremotecontroller.device.PairedDeviceViewHolder.java
git.egatuts.nxtremotecontroller.device.PairedDevice.java
git.egatuts.nxtremotecontroller.exception.SocketCreationException.java
git.egatuts.nxtremotecontroller.fragment.ActivityBaseFragment.java
git.egatuts.nxtremotecontroller.fragment.BaseFragment.java
git.egatuts.nxtremotecontroller.fragment.BluetoothFragment.java
git.egatuts.nxtremotecontroller.fragment.ControllerBaseFragment.java
git.egatuts.nxtremotecontroller.fragment.DefaultFragmentPendingTransition.java
git.egatuts.nxtremotecontroller.fragment.FragmentPendingTransition.java
git.egatuts.nxtremotecontroller.fragment.HomeFragment.java
git.egatuts.nxtremotecontroller.fragment.LocalControllerFragment.java
git.egatuts.nxtremotecontroller.fragment.OnlineControllerFragment.java
git.egatuts.nxtremotecontroller.fragment.ScanFragment.java
git.egatuts.nxtremotecontroller.fragment.SettingsFragment.java
git.egatuts.nxtremotecontroller.listener.AnimationEndListener.java
git.egatuts.nxtremotecontroller.listener.AppKillerListener.java
git.egatuts.nxtremotecontroller.listener.BaseListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothDiscoveryListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothEnableListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothPairingListener.java
git.egatuts.nxtremotecontroller.navigation.DrawerItemViewHolder.java
git.egatuts.nxtremotecontroller.navigation.DrawerItem.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerAdapter.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerCallback.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerFragment.java
git.egatuts.nxtremotecontroller.preference.PreferencesUtils.java
git.egatuts.nxtremotecontroller.receiver.AppKillerReceiver.java
git.egatuts.nxtremotecontroller.receiver.BaseReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothDiscoveryReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothEnableReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothPairingReceiver.java
git.egatuts.nxtremotecontroller.thread.BaseThread.java
git.egatuts.nxtremotecontroller.thread.ConnectThread.java
git.egatuts.nxtremotecontroller.thread.ConnectedThread.java
git.egatuts.nxtremotecontroller.views.BaseIndeterminateProgressDialog.java
git.egatuts.nxtremotecontroller.views.BaseProgressDialog.java
git.egatuts.nxtremotecontroller.views.JoystickView.java
git.egatuts.nxtremotecontroller.views.LongIndeterminateProgressDialog.java
git.egatuts.nxtremotecontroller.views.ShortIndeterminateProgressDialog.java