Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # virtual-joystick-android
  2. **v1.10.1** _(New version - [support custom images](#image), button & background size, limited direction, normalized coordinate, alpha border)_
  3. _I created this very simple library as a learning process and I have been inspired by this project [JoystickView](https://github.com/zerokol/JoystickView) (the author is a genius!)_
  4. This library provides a very simple and **ready-to-use** custom view which emulates a joystick for Android.
  5. ![Alt text](/misc/virtual-joystick-android.png?raw=true "Double Joystick with custom size and colors")
  6. ### Gist
  7. Here is a very simple snippets to use it. Just set the `onMoveListener` to retrieve its angle and strength.
  8. ```java
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. ...
  14. JoystickView joystick = (JoystickView) findViewById(R.id.joystickView);
  15. joystick.setOnMoveListener(new JoystickView.OnMoveListener() {
  16. @Override
  17. public void onMove(int angle, int strength) {
  18. // do whatever you want
  19. }
  20. });
  21. }
  22. ```
  23. The **angle** follow the rules of a simple **counter-clock** protractor. The **strength is percentage** of how far the button is **from the center to the border**.
  24. ![Alt text](/misc/virtual-joystick.png?raw=true "Explanation")
  25. By default the **refresh rate** to get the data is **20/sec (every 50ms)**. If you want more or less just set the listener with one more parameters to set the refresh rate in milliseconds.
  26. ```java
  27. joystick.setOnMoveListener(new JoystickView.OnMoveListener() { ... }, 17); // around 60/sec
  28. ```
  29. ### Attributes
  30. You can customize the joystick according to these attributes `JV_buttonImage`, `JV_buttonColor`, `JV_buttonSizeRatio`, `JV_borderColor`, `JV_borderAlpha`, `JV_borderWidth`, `JV_backgroundColor`, `JV_backgroundSizeRatio`, `JV_fixedCenter`, `JV_autoReCenterButton`, `JV_buttonStickToBorder`, `JV_enabled` and `JV_buttonDirection`
  31. If you specified `JV_buttonImage` you don't need `JV_buttonColor`
  32. Here is an example for your layout resources:
  33. ```xml
  34. <io.github.controlwear.virtual.joystick.android.JoystickView
  35. xmlns:custom="http://schemas.android.com/apk/res-auto"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. custom:JV_buttonColor="#FF6E40"
  39. custom:JV_buttonSizeRatio="15%"
  40. custom:JV_borderColor="#00796B"
  41. custom:JV_backgroundColor="#009688"
  42. custom:JV_borderWidth="4dp"
  43. custom:JV_fixedCenter="false"/>
  44. ```
  45. #### Image
  46. If you want a more customized joystick, you can use `JV_buttonImage` and the regular `background` attributes to specify drawables. The images will be automatically resized.
  47. ```xml
  48. <io.github.controlwear.virtual.joystick.android.JoystickView
  49. xmlns:custom="http://schemas.android.com/apk/res-auto"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:background="@drawable/joystick_base_blue"
  53. custom:JV_buttonImage="@drawable/ball_pink"/>
  54. ```
  55. ![Alt text](/misc/android-virtual-joystick-custom-image.png?raw=true "Left joystick with custom image")
  56. #### SizeRatio
  57. We can change the default size of the button and background.
  58. The size is calculated as a percentage of the total width/height.
  59. By default, the button is 25% (0.25) and the background 75% (0.25), as the first screenshot above.
  60. If the total (background + button) is above 1.0, the button will probably be a bit cut when on the border.
  61. ```xml
  62. <...
  63. custom:JV_buttonSizeRatio="50%"
  64. custom:JV_backgroundSizeRatio="10%"/>
  65. ```
  66. ```java
  67. joystick.setBackgroundSizeRatio(0.5);
  68. joystick.setButtonSizeRatio(0.1);
  69. ```
  70. _The background size is not working for a custom picture._
  71. #### FixedCenter or Not? (and auto re-center)
  72. If you don’t set up this parameter, it will be FixedCenter by default, which is the regular behavior.
  73. However, sometimes, it is convenient to have an auto-defined center which will be defined each time you touch down the screen with your finger (center position will be limited inside the JoystickView’s width/height).
  74. As every parameter you can set it up in xml (as above) or in Java:
  75. ```java
  76. joystick.setFixedCenter(false); // set up auto-define center
  77. ```
  78. UnfixedCenter (set to false) is particularly convenient when the user can’t (or doesn’t want to) see the screen (e.g. a drone's controller).
  79. We can also remove the automatically re-centered button, just set it to false.
  80. ```java
  81. joystick.setAutoReCenterButton(false);
  82. ```
  83. _(The behavior is a bit weird if we set remove both the FixedCenter and the AutoReCenter.)_
  84. #### Enabled
  85. By default the joystick is enabled (set to True), but you can disable it either in xml or Java. Then, the button will stop moving and `onMove()` won’t be called anymore.
  86. ```java
  87. joystick.setEnabled(false); // disabled the joystick
  88. joystick.isEnabled(); // return enabled state
  89. ```
  90. #### ButtonDirection
  91. By default the button can move in both direction X,Y (regular behavior), but we can limit the movement through one axe horizontal or vertical.
  92. ```xml
  93. <...
  94. custom:JV_buttonDirection="horizontal"/>
  95. ```
  96. In the layout file (xml), this option can be set to `horizontal`, `vertical` or `both`.
  97. We can also set this option in the Java file by setting an integer value:
  98. - any negative value (e.g. -1) for the horizontal axe
  99. - any positive value (e.g. 1) for the vertical axe
  100. - zero (0) for both (which is the default option)
  101. ```java
  102. joystick.setButtonDirection(1); // vertical
  103. ```
  104. ### Wearable
  105. If you use this library in Wearable app, you will probably disable the Swipe-To-Dismiss Gesture and implement the Long Press to Dismiss Pattern, which could be a problem for a Joystick Pattern (because we usually let the user touch the joystick as long as she/he wants), in that case you can set another convenient listener: `OnMultipleLongPressListener` which will be invoked only with multiple pointers (at least two fingers) instead of one.
  106. ```java
  107. joystick.setOnMultiLongPressListener(new JoystickView.OnMultipleLongPressListener() {
  108. @Override
  109. public void onMultipleLongPress() {
  110. ... // eg. mDismissOverlay.show();
  111. }
  112. });
  113. ```
  114. Or better, if you just want a simple Joystick (and few other cool stuff) as a controller for your mobile app you can use the following related project ;)
  115. ## Demo
  116. For those who want more than just a snippet, here is the demo :
  117. - [Basic two joysticks ](https://github.com/controlwear/virtual-joystick-demo) (similar to screenshot)
  118. If you want to add your project here, go ahead :)
  119. ## Required
  120. Minimum API level is 16 (Android 4.1.x - Jelly Bean) which cover 99.5% of the Android platforms as of October 2018 according to the <a href="https://developer.android.com/about/dashboards" class="user-mention">distribution dashboard</a>.
  121. ## Download
  122. ### Gradle
  123. ```java
  124. compile 'io.github.controlwear:virtualjoystick:1.10.1'
  125. ```
  126. ## Contributing
  127. If you would like to contribute code, you can do so through GitHub by forking the repository and sending a pull request.
  128. When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible.
  129. ## License
  130. ```
  131. Licensed under the Apache License, Version 2.0 (the "License");
  132. you may not use this file except in compliance with the License.
  133. You may obtain a copy of the License at
  134. http://www.apache.org/licenses/LICENSE-2.0
  135. Unless required by applicable law or agreed to in writing, software
  136. distributed under the License is distributed on an "AS IS" BASIS,
  137. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  138. See the License for the specific language governing permissions and
  139. limitations under the License.
  140. ```
  141. ## Authors
  142. **virtual-joystick-android** is an open source project created by <a href="https://github.com/makowildcat" class="user-mention">@makowildcat</a> (mostly spare time) and partially funded by [Black Artick](http://blackartick.com/) and [NSERC](http://www.nserc-crsng.gc.ca/index_eng.asp).
  143. Also, thanks to <a href="https://github.com/Bernix01" class="user-mention">Bernix01</a>, <a href="https://github.com/teancake" class="user-mention">teancake</a>, <a href="https://github.com/Spettacolo83" class="user-mention">Spettacolo83</a>, <a href="https://github.com/djjaysmith" class="user-mention">djjaysmith</a>, <a href="https://github.com/jaybkim1" class="user-mention">jaybkim1</a>, <a href="https://github.com/sikrinick" class="user-mention">sikrinick</a>, <a href="https://github.com/AlexandrDavydov" class="user-mention">AlexandrDavydov</a>, <a href="https://github.com/indrek-koue" class="user-mention">indrek-koue</a>, <a href="https://github.com/QitmentX7" class="user-mention">QitmentX7</a>, <a href="https://github.com/esplemea" class="user-mention">esplemea</a>, <a href="https://github.com/FenixGit" class="user-mention">FenixGit</a>, <a href="https://github.com/AlexanderShniperson" class="user-mention">AlexanderShniperson</a>
  144. and <a href="https://github.com/GijsGoudzwaard" class="user-mention">GijsGoudzwaard</a> for contributing.