calculateDistanceX(MotionEvent) method in intercept touch events handling - android

I'm new to android and trying to learn intercept touch events handling using this documentation, but i do not understand that calculateDistanceX(MotionEvent) method at all, and the more I searched about it the less i found. I'm completely confused.
Could anyone please explain it to me?
Thanks in advance.

It says that the method calculateDistanceX() is left as an exercise for a user. One solution would be to cache the first MotionEvent and then the distance would be mFirstMotionEvent.getX() - currentMotionEvent.getX()

Related

Is there a way to simulate a touch on a specific position of a widget or screen?

I want to make a touch or tap somewhere on a widget without making the user explicitly touch the screen at that point. Is there any way to do so?
I've checked SO answers, and some recommend using "integration tests" but which is not available to do on devices which aren't physically or in some way connected to the laptop (couldn't find a better wording).
I also attempted to do a hitTest wondering if it actually touches or taps the screen at that point, while interacting with the UI, but seems like it doesn't.
onPressed: () {
RenderObject? rb = context.findRenderObject();
if (rb is RenderBox){
final hit = BoxHitTestResult();
if (rb.hitTest(hit, position: Offset(300,500))){
print(hit.path);
}
}
},
Also, please understand the question right, as in many of the answers I've been reading regarding my query has been answered with "you don't need it, just call the function/method". I want to "simulate" a tap on a certain specified position of the screen, maybe according the X,Y axis as I attempted on the code.
An honest thank you for any directions.
If anything is lacking in my question, please let me know.
Edit: (The following is an update to what I'm finding)
I went diving into the Flutter codes. I found a few files:
Under flutter/src/gestures/hit_test.dart I found void handleEvent(PointerEvent event, HitTestEntry entry);. I assume it consumes a PointerEvent. Checking further,
Under flutter/src/gestures/events.dart I find class PointerDownEvent extends PointerEvent and under flutter_test/src/test_pointer.dart
I find class TestPointer
Would any of these files give any light to what I want to do? I'm checking through.
Thanks to #pskink , the following code works the best for me/ the situation. I've been able to make the taps as wanted. Thank you very much.
WidgetsBinding.instance!.handlePointerEvent(PointerDownEvent(pointer: 0, position: Offset(100, 100),)); WidgetsBinding.instance!.handlePointerEvent(PointerUpEvent(pointer: 0,position: Offset(100, 100),));

What's the BEST WAY to check (and do something) for a touch event in libgdx?

I have a newbie question. I just started learning about libgdx and I'm a bit confused. I read the documentation/wiki, I followed some tutorials like the one from gamefromscratch and others and I still have a question.
What's the best way to check and do something for a touch/tap event?
I'm using Scenes and Actors and I found at least 4 ways (till now) of interacting with an Actor, let's say:
1) myActor.addListener(new ClickListener(){...});
2) myActor.setTouchable(Touchable.enabled); and putting the code in the act() method
3) verifying Gdx.input.isTouched() in the render() method
4) overriding touchDown, touchUp methods
Any help with some details and suggestions when to use one over the other, or what's the difference between them would be very appreciated.
Thanks.
I've always been using the first method and I think from an OOP viewpoint, it's the "best" way to do it.
The second approach will not work. Whether you set an Actor to be touchable or not, Actor.act(float) will still be called whenever you do stage.act(float). That means you would execute your code in every frame.
Gdx.input.isTouched() will only tell you that a touch event has happened anywhere on the screen. It would not be a good idea to try to find out which actor has been hit by that touch, as they are already able to determine that themselves (Actor.hit()).
I'm not sure where you'd override touchDown and touchUp. Actors don't have these methods, so I'm assuming you are talking about a standard InputProcessor. In this case you will have the same problem like with your 3rd approach.
So adding a ClickListener to the actors you want to monitor for these kind of events is probably the best way to go.

Overriding onTouchEvent with Flixel (Android port)

So I followed Mathew Casperson's Making Games on Android Tutorial and got a small game running a few days ago, now I am trying to switch the controls to touchscreen instead of the D-pad.
I am running into some problems and was wondering if anyone here could help me. Flixel doesn't have any built in touchscreen functions so I am overriding onTouchEvent(MotionEvent event) in my Activity (FlixelDemo.java in the tutorial) and hopefully getting the coordinates of the touch.
I then have a function in my Player.java that given the touch coordinates could tell me whether my player has been touched.
The problem I am having is trying to figure out how to get to/call that function (isCollision) from in the activity.
It seems that I can only override the onTouchEvent in FlixelDemo.java and that I can only use the isCollision function in GameState.java where I add the player.
How do I get the info from the overridden touch event to any of my other classes? Can anyone tell me what I am doing wrong or help me figure out a different way of implementing touch events?
Looking at the code, FlixelDemo is really just a container for org.flixel.FlxGameView (via the res/layout/main.xml file).
The onTouchEvent method can be applied to any View, so you can apply it to just the flixel viewport.
And in fact, that's probably what you want to do here: Add your handler directly to FlxGameView.java, and then let it call a method on the internal GameThread class.
It's already handling the other events this way. See FlxGameView.onKeyDown (and the associated FlxGameView.GameThread.doKeyDown) for a good example.

getHistorySize() returns 0 even on MotionEvent.ACTION_MOVE events

I kept getting 0 from event.getHistorySize() even when the event is MotionEvent.ACTION_MOVE.
However, it's not always zero. It adds one point count in like 10 ACTION_MOVE events. Can anyone please help?
I am not sure about this, but I have been troubled by the same problem. What I believe is that the MotionEvent history contains the events which have not been reported individually to the onTouchEvent. This is also supported by the Android Documentation (http://developer.android.com/reference/android/view/MotionEvent.html) under "Batching".
The HistorySize is the list of MotionEvents it's missed. So when it drops frames. It'll give you all the ones it missed, if it's important. It doesn't store all the events that it gave you previously. It only stores those events it doesn't.

button events in android

I want to develop in android a simple application: when the user push a button, an index will be incremented until the button is released. Which button events i have to use?
Thanks
Use an OnTouchListener. And as the others said, you should accept answers to some of your previous questions.
This code implements an open-source prototype, the Android HCI Extractor, which tracks and monitor the user and system interaction events in multimodal applications (e.g. touch, keypresses, scroll, number of elements provided by the system, etc.)
Android HCI Extractor code: http://code.google.com/p/android-hci-extractor/
In the source code you can find the class /android.hci.extractor/src/org/mmi/android/instrumentation/filters/NavigationInputFilter.java in one method is aimed to control the touch-press, the touch-move and the touch-release events. The method is called "public boolean onTouch (View v, MotionEvent event)", and the MotionEvent is the extra information that describes you the action made with the finger.
In this clas you can also find other methods aimed to control clicks and itemClicks.
Further information about the MIM project (including tutorials about the tool integration and usage): http://www.catedrasaes.org/trac/wiki/MIM
I hope it helps you!!

Categories

Resources