Android Gesture Recognition multiple events
In my application I have a VideoView
that display a video feed coming from a camera pool. I wish to use gesture recognition to change which camera is being shown.
Scrolling on X axis should change camera type, scrolling on Y axis should switch the side of the camera group.
Source (of the MainActivity
object where the VideoView
is located):
private GestureDetectorCompat gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetectorCompat(this, new CameraGestureListener());
cameraView = findViewById(R.id.cameraView);
cameraView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (gestureDetector.onTouchEvent(motionEvent))
return true;
return MainActivity.super.onTouchEvent(motionEvent);
}
});
And finally I defined this class (inside MainActivity
):
class CameraGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent event) {
Log.d(getClass().getName(), "onDown: " + event.toString());
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
if(Math.abs(velocityY)>Math.abs(velocityX))
{
Log.d(getClass().getName(), "change side");
}
else if(velocityX>0)
{
Log.d(getClass().getName(), "change camera type");
}
else if(velocityX<0)
{
Log.d(getClass().getName(), "change camera type");
}
return true;
}
@Override
public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY) {
if(Math.abs(distanceY)>Math.abs(distanceX))
{
Log.d(getClass().getName(), "change side");
}
else if(distanceX>0)
{
Log.d(getClass().getName(), "change camera type");
}
else if(distanceX<0)
{
Log.d(getClass().getName(), "change camera type");
}
return true;
}
}
In debug I can clearly see that Scroll
events are triggered twice and often distanceX is larger than distanceY even if the gesture is mainly on the physical Y screen axis. Same problems arise with similar behaviours on Fling
events.
How can I fix this?
java android gesture-recognition
add a comment |
In my application I have a VideoView
that display a video feed coming from a camera pool. I wish to use gesture recognition to change which camera is being shown.
Scrolling on X axis should change camera type, scrolling on Y axis should switch the side of the camera group.
Source (of the MainActivity
object where the VideoView
is located):
private GestureDetectorCompat gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetectorCompat(this, new CameraGestureListener());
cameraView = findViewById(R.id.cameraView);
cameraView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (gestureDetector.onTouchEvent(motionEvent))
return true;
return MainActivity.super.onTouchEvent(motionEvent);
}
});
And finally I defined this class (inside MainActivity
):
class CameraGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent event) {
Log.d(getClass().getName(), "onDown: " + event.toString());
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
if(Math.abs(velocityY)>Math.abs(velocityX))
{
Log.d(getClass().getName(), "change side");
}
else if(velocityX>0)
{
Log.d(getClass().getName(), "change camera type");
}
else if(velocityX<0)
{
Log.d(getClass().getName(), "change camera type");
}
return true;
}
@Override
public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY) {
if(Math.abs(distanceY)>Math.abs(distanceX))
{
Log.d(getClass().getName(), "change side");
}
else if(distanceX>0)
{
Log.d(getClass().getName(), "change camera type");
}
else if(distanceX<0)
{
Log.d(getClass().getName(), "change camera type");
}
return true;
}
}
In debug I can clearly see that Scroll
events are triggered twice and often distanceX is larger than distanceY even if the gesture is mainly on the physical Y screen axis. Same problems arise with similar behaviours on Fling
events.
How can I fix this?
java android gesture-recognition
1
what doesreturn MainActivity.super.onTouchEvent(motionEvent);
do insideView.OnTouchListener#onTouch
method? just returntrue
from it
– pskink
Nov 19 '18 at 17:52
@pskink it's works! ...onSling
event now correctly reports the action I'm looking for. Thanks.
– weirdgyn
Nov 20 '18 at 8:12
good, nice to hear that
– pskink
Nov 20 '18 at 8:14
add a comment |
In my application I have a VideoView
that display a video feed coming from a camera pool. I wish to use gesture recognition to change which camera is being shown.
Scrolling on X axis should change camera type, scrolling on Y axis should switch the side of the camera group.
Source (of the MainActivity
object where the VideoView
is located):
private GestureDetectorCompat gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetectorCompat(this, new CameraGestureListener());
cameraView = findViewById(R.id.cameraView);
cameraView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (gestureDetector.onTouchEvent(motionEvent))
return true;
return MainActivity.super.onTouchEvent(motionEvent);
}
});
And finally I defined this class (inside MainActivity
):
class CameraGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent event) {
Log.d(getClass().getName(), "onDown: " + event.toString());
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
if(Math.abs(velocityY)>Math.abs(velocityX))
{
Log.d(getClass().getName(), "change side");
}
else if(velocityX>0)
{
Log.d(getClass().getName(), "change camera type");
}
else if(velocityX<0)
{
Log.d(getClass().getName(), "change camera type");
}
return true;
}
@Override
public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY) {
if(Math.abs(distanceY)>Math.abs(distanceX))
{
Log.d(getClass().getName(), "change side");
}
else if(distanceX>0)
{
Log.d(getClass().getName(), "change camera type");
}
else if(distanceX<0)
{
Log.d(getClass().getName(), "change camera type");
}
return true;
}
}
In debug I can clearly see that Scroll
events are triggered twice and often distanceX is larger than distanceY even if the gesture is mainly on the physical Y screen axis. Same problems arise with similar behaviours on Fling
events.
How can I fix this?
java android gesture-recognition
In my application I have a VideoView
that display a video feed coming from a camera pool. I wish to use gesture recognition to change which camera is being shown.
Scrolling on X axis should change camera type, scrolling on Y axis should switch the side of the camera group.
Source (of the MainActivity
object where the VideoView
is located):
private GestureDetectorCompat gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetectorCompat(this, new CameraGestureListener());
cameraView = findViewById(R.id.cameraView);
cameraView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (gestureDetector.onTouchEvent(motionEvent))
return true;
return MainActivity.super.onTouchEvent(motionEvent);
}
});
And finally I defined this class (inside MainActivity
):
class CameraGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent event) {
Log.d(getClass().getName(), "onDown: " + event.toString());
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
if(Math.abs(velocityY)>Math.abs(velocityX))
{
Log.d(getClass().getName(), "change side");
}
else if(velocityX>0)
{
Log.d(getClass().getName(), "change camera type");
}
else if(velocityX<0)
{
Log.d(getClass().getName(), "change camera type");
}
return true;
}
@Override
public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY) {
if(Math.abs(distanceY)>Math.abs(distanceX))
{
Log.d(getClass().getName(), "change side");
}
else if(distanceX>0)
{
Log.d(getClass().getName(), "change camera type");
}
else if(distanceX<0)
{
Log.d(getClass().getName(), "change camera type");
}
return true;
}
}
In debug I can clearly see that Scroll
events are triggered twice and often distanceX is larger than distanceY even if the gesture is mainly on the physical Y screen axis. Same problems arise with similar behaviours on Fling
events.
How can I fix this?
java android gesture-recognition
java android gesture-recognition
asked Nov 19 '18 at 17:17
weirdgynweirdgyn
279427
279427
1
what doesreturn MainActivity.super.onTouchEvent(motionEvent);
do insideView.OnTouchListener#onTouch
method? just returntrue
from it
– pskink
Nov 19 '18 at 17:52
@pskink it's works! ...onSling
event now correctly reports the action I'm looking for. Thanks.
– weirdgyn
Nov 20 '18 at 8:12
good, nice to hear that
– pskink
Nov 20 '18 at 8:14
add a comment |
1
what doesreturn MainActivity.super.onTouchEvent(motionEvent);
do insideView.OnTouchListener#onTouch
method? just returntrue
from it
– pskink
Nov 19 '18 at 17:52
@pskink it's works! ...onSling
event now correctly reports the action I'm looking for. Thanks.
– weirdgyn
Nov 20 '18 at 8:12
good, nice to hear that
– pskink
Nov 20 '18 at 8:14
1
1
what does
return MainActivity.super.onTouchEvent(motionEvent);
do inside View.OnTouchListener#onTouch
method? just return true
from it– pskink
Nov 19 '18 at 17:52
what does
return MainActivity.super.onTouchEvent(motionEvent);
do inside View.OnTouchListener#onTouch
method? just return true
from it– pskink
Nov 19 '18 at 17:52
@pskink it's works! ...
onSling
event now correctly reports the action I'm looking for. Thanks.– weirdgyn
Nov 20 '18 at 8:12
@pskink it's works! ...
onSling
event now correctly reports the action I'm looking for. Thanks.– weirdgyn
Nov 20 '18 at 8:12
good, nice to hear that
– pskink
Nov 20 '18 at 8:14
good, nice to hear that
– pskink
Nov 20 '18 at 8:14
add a comment |
1 Answer
1
active
oldest
votes
Following pskink suggestion the only needed fix was to remove the unneeded retrigger of the superclass event handler (a cut&paste misprint of my own):
MainActivity.super.onTouchEvent(motionEvent);
The correct code become:
cameraView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
gestureDetector.onTouchEvent(motionEvent);
return true;
}
});
The gesture is properly recognized by onFling
event handler. Trough onScroll
event handlers you still observe two events for each gesture but it's probably a correct behaviour.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379666%2fandroid-gesture-recognition-multiple-events%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Following pskink suggestion the only needed fix was to remove the unneeded retrigger of the superclass event handler (a cut&paste misprint of my own):
MainActivity.super.onTouchEvent(motionEvent);
The correct code become:
cameraView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
gestureDetector.onTouchEvent(motionEvent);
return true;
}
});
The gesture is properly recognized by onFling
event handler. Trough onScroll
event handlers you still observe two events for each gesture but it's probably a correct behaviour.
add a comment |
Following pskink suggestion the only needed fix was to remove the unneeded retrigger of the superclass event handler (a cut&paste misprint of my own):
MainActivity.super.onTouchEvent(motionEvent);
The correct code become:
cameraView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
gestureDetector.onTouchEvent(motionEvent);
return true;
}
});
The gesture is properly recognized by onFling
event handler. Trough onScroll
event handlers you still observe two events for each gesture but it's probably a correct behaviour.
add a comment |
Following pskink suggestion the only needed fix was to remove the unneeded retrigger of the superclass event handler (a cut&paste misprint of my own):
MainActivity.super.onTouchEvent(motionEvent);
The correct code become:
cameraView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
gestureDetector.onTouchEvent(motionEvent);
return true;
}
});
The gesture is properly recognized by onFling
event handler. Trough onScroll
event handlers you still observe two events for each gesture but it's probably a correct behaviour.
Following pskink suggestion the only needed fix was to remove the unneeded retrigger of the superclass event handler (a cut&paste misprint of my own):
MainActivity.super.onTouchEvent(motionEvent);
The correct code become:
cameraView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
gestureDetector.onTouchEvent(motionEvent);
return true;
}
});
The gesture is properly recognized by onFling
event handler. Trough onScroll
event handlers you still observe two events for each gesture but it's probably a correct behaviour.
edited Nov 20 '18 at 11:09
answered Nov 20 '18 at 8:19
weirdgynweirdgyn
279427
279427
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379666%2fandroid-gesture-recognition-multiple-events%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
what does
return MainActivity.super.onTouchEvent(motionEvent);
do insideView.OnTouchListener#onTouch
method? just returntrue
from it– pskink
Nov 19 '18 at 17:52
@pskink it's works! ...
onSling
event now correctly reports the action I'm looking for. Thanks.– weirdgyn
Nov 20 '18 at 8:12
good, nice to hear that
– pskink
Nov 20 '18 at 8:14