A simple Android joystick that can be easily added to activities.
Gradle
- Project level
build.gradle
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
- App level
build.gradle
dependencies {
implementation 'com.github.harry1453:joystick-view:v1.0'
}
Please see the demoApp
directory for a demo app.
- Add the
JoystickView
to your XML:
<com.harrysoft.joystickview.JoystickView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/my_joystick" />
- Make your activity implement
JoystickView.JoystickListener
:
public class MainActivity extends AppCompatActivity implements JoystickView.JoystickListener {
@Override
public void onJoystickMoved(float xValue, float yValue, int id) {
switch (id) {
case R.id.my_joystick:
Log.d("My Joystick App", "Joystick Moved! X value: " + xValue + " Y value: " + yValue);
break;
}
}
}
- Get an instance of the JoystickView and set the listener:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JoystickView joystick = findViewById(R.id.my_joystick);
joystick.setJoystickListener(this);
}