I want to make a joystick with an ImageView.
The user will touch the view and move the “stick” around.
ivjoystick.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
touch(event);
return true;
}
});
The touch method is called when the user touches the view, the events are detected and the joystick works as desired.
As soon as the finger is moved away from the “starting point”, the onTouch method is not called anymore (at finger movements). The joysick freezes.
This behaviour is independet of what the touch method does. However, here’s its code:
private void touch(MotionEvent event) {
if (!(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_UP))
return;
double oldx = x;
double oldy = y;
x = (event.getX() - centerx) / rpad;
y = (event.getY() - centery) / rpad;
if (Math.sqrt(x * x + y * y) > 1) {
x /= Math.sqrt(x * x + y * y);
y /= Math.sqrt(x * x + y * y);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
x = 0;
y = 0;
}
if (SystemClock.uptimeMillis() - last < minintervall && event.getAction() == MotionEvent.ACTION_MOVE)
return;
if (oldx != x || oldy != y) {
draw();
send();
}
return;
}
The ImageView was inside a ScrollView. The longer movements were somehow taken by the scroll view for scrolling.
I removed the scrollview.