Multi-finger contact refers to monitoring the contact occasions of a number of fingers. We are able to override the
onTouchEvent technique in
View, or use the
setOnTouchListener technique to deal with contact occasions.
First, let’s check out easy methods to decide the occasion sort of multi-finger contact.
Typically talking, we choose the kind of enter occasion by judging the motion of
MotionEvent, in order to make corresponding processing.
With out contemplating a number of fingers, we typically solely concentrate on the next occasion sorts:
Faucet the display screen with the primary finger
The final finger leaves the display screen
A finger is swiping on the display screen
So for multi-finger touch, along with the above-mentioned widespread occasion sorts, we additionally want to concentrate to 2 different occasion sorts:
A finger already exists on the display screen earlier than the faucet
When one finger on the display screen is lifted, there are nonetheless different fingers on the display screen
It must be famous that the above two sorts can’t be obtained through the use of the
MotionEvent#getAction technique as earlier than, and want to make use of
So when coping with multi-finger contact, our
onTouch technique can typically be written like this:
When a number of fingers contact the display screen on the similar time, we have to monitor completely different fingers. There are a number of different ideas concerned right here.
There are a number of strategies for monitoring completely different fingers in
ActionIndex could be obtained immediately by the
getActionIndex technique, which could be roughly understood as describing the variety of fingers that the present occasion happens on. For instance, once we monitor the finger raise, we might wish to know which finger is lifted, then it may be judged by
As well as, for a similar finger, the worth of
ActionIndex might change because the finger is pressed and lifted, so we can’t use it to establish a finger.
It appears that evidently the one goal of
ActionIndex is to get the
Specifically, it must be famous that this technique is just legitimate for the
ACTION_POINTER_UP occasions. The
ACTION_MOVE occasion can’t precisely acquire the worth. We have to make a complete judgment together with different occasions.
PointerId is obtained by the
getPointerId(int) technique, and the parameter is
We are able to establish a finger by
PointerId. For a similar finger, the
PointerId is fastened all through the method from urgent to lifting.
Additionally notice that this worth could also be reused, for instance, a finger with an
id of 0 can also have an
id of 0 when a finger is re-pressed after it has been lifted.
PointerIndex is obtained by
findPointerIndex(int), the parameter is
This worth is used to get extra content material of the occasion.
If we wish to get the press level place of the occasion, once we get the coordinates by the
getY() technique, we will solely get the place of the primary finger, however these two strategies present an overload:
float getX(int pointerIndex);
float getY(int pointerIndex);
Via the above introduction, now we have roughly understood some key factors of multi-touch, and now let’s apply them in observe.
Right here I’ll make a
DrawView for drawing finger motion trajectory, and it may well monitor the trajectory of a number of fingers on the similar time. The impact is as follows:
The image above is the impact when swiping 4 fingers on the similar time.
To attain this impact, there are two fundamental points to think about:
The primary is easy methods to precisely monitor the sliding trajectory of a finger, as a result of as talked about above,
ACTION_MOVE can’t acquire
ActionIndex. However when God closes the door, he will certainly open a window. We are able to monitor it by
PointerId. First, take heed to the 2 occasions of
ACTION_POINTER_DOWN, get the
PointerId of the brand new finger right here, traverse all of the fingers within the
ACTION_MOVE occasion, after which evaluate
PointerId could be both.
Second, as a result of
MotionEvent will bundle a number of consecutive sliding trajectories into one
MotionEvent, we have to use
getHistoricalX to get the historic trajectory of this sliding. The strategy signature is as follows:
float getHistoricalX(int pointerIndex, int pos);
The primary parameter,
pointerIndex, is straightforward to unravel. The primary query has already been talked about, primarily the second parameter.
As a result of
HistoricalX is an inventory, we have to learn one after the other by the index, and the second pos parameter is the index, however provided that we all know the size of the listing. This may be solved with just one
MotionEvent offers a way to get the size of this listing:
However this offers this technique, there aren’t any different overloads, so you may’t get the size of the historical past monitor listing of a sure finger’s slide this time by
Nevertheless, after my check, irrespective of which finger slides, you may get the size of the historic monitor by the
getHistorySize technique, after which name the
getHistoricalX technique to acquire the coordinates of the historic monitor.
Though I don’t know why it’s designed this fashion, it does remedy this downside.
We first outline an internal class to behave as drawing metadata:
DrawPath corresponds to the life cycle of a finger slide, that’s, the trajectory skilled within the center from DOWN to UP.
Then outline an inventory of
DrawPath and variables akin to brushes, monitor colour arrays, and so on.:
Now let’s override the
There are feedback above, so I received’t go into element.
Then rewrite the
onDraw technique. Though this can be a management for drawing tracks, there’s not a lot code within the
On this means, a easy management that helps multi-finger drawing is realized, and we will additionally add some strategies akin to undoing the earlier step to it. I received’t discuss it right here.
The whole code for
DrawView has been placed on GitHub.
You’re welcome to check it out.