How to manage multi-touch capability in Android and be able to draw roles?
6 minute(s) read
|
Published on: Aug 13, 2021
Updated on: Dec 14, 2021
|
In previous tutorials, we talked to you about the one-touch and multitouch capabilities of the phone screen with Android operating systems and the excellent support and management of the Android system. This article also wants to know how to manage and control this feature. (One-touch and multitouch phone screen capability with Android operating system) Let's talk. It is possible that in Android operating systems, after the development of Android (in German: Android-Entwicklung ), some people can do some activities by touching the screen with one finger, and on the contrary, it is possible to drag and drop the touch screen using several fingers. How to manage this feature is discussed in this article.
What you will learn in this article are as follows:
- Basics of touch in Android operating systems
- What is a single touch event?
- What is a multitouch event?
- How to learn how to use the GestureDetector class
- How to draw using the touch screen feature in Android systems?
- What is Tracking?
Touch basics in Android operating systems
Note that the View class supports touch-related events, and with this account, we can respond to touches in Views and Activity. Android can easily support multiple pointers simultaneously. For example, we can say that when a few fingers work on the screen and touch it the fingers touching the screen interact with the system.
Base class is a MotionEvent that also supports touch. Note that this class can be sent to Views using the onTouchEvent () function. Of course, to respond to or react to touch events, you must first rewrite the touch method.
The MotionEvent class contains information related to touch events. If the touch event is managed using views, true will be the method's output.
What is a single touch event?
The getY () and getX () functions can be used when only one input is used. In this case, the getY () and getX () functions can be used to get the positions of the fingers.
Touch event
- MotionEvent.ACTION_DOWN event: This event can create and start a new Touch.
- MotionEvent.ACTION_MOVE event:
MotionEvent.ACTION_DOWN event is moving the finger.
- MotionEvent.ACTION_UP event:
This event can be used for a finger to move upwards.
- MotionEvent.ACTION_CANCEL event:
This event indicates that the current event has been canceled and stopped and that something else has taken control of the event.
- MotionEvent.ACTION_POINTER_DOWN event:
This event is low for the pointer and can be used for it.
- MotionEvent.ACTION_POINTER_UP event:
This event is for the pointer above and can be used for it.
What is a multitouch event?
This feature is possible in versions 2.0 and above and can be used for them. This feature makes it possible to manage a few fingers and perform operations.
It should be noted that in this type of event, the Android systems MotionEvent.ACTION_POINTER_DOWN and MotionEvent.ACTION_POINTER_UP is activated when the second finger touches the touch screen. Inside the MotionEvent class, the getPointerCount () function is used, and this function can also help the programmer be aware when the second finger or other fingers touch the screen. The MotionEvent class includes all the events of the Android system and the location of the pointers, all the information of which is transmitted to the programmer using the onTouch () function, and the programmer can access all that information through this method.
The MotionEvent.getActionIndex () and MotionEvent.getActionMasked () functions can be used to track touch events.
A pointer index is an index whose values may change over time because it may touch a finger on the screen, in which case its value will change. Like the following code:
Note:
Of course, you should note that if you use simulation to run a project, you cannot run the Multitouch feature. To run the Multitouch feature, you must use a real device that has the Android operating system.
The ScaleGestureDetector class can be used to specify movements such as zooming in or out using two fingers.
How to draw using the touch screen feature in Android systems?
In this part of the tutorial, we want to display the Singletouch using a View.
1- We create the project com.vogella. Android.touch.single.
2- Set the activity that will be used in this project to SingleTouchActivity.
3- Create the SingleTouchEventView class as follows and make the necessary changes.
4- Now it's time to add the View you created to the View, as follows:
After you have made all the above changes and inserted the above code in the files, you will notice at runtime that you can draw on the phone screen with one finger. If you use a simulator to run this project, you can do so using the mouse cursor.
What is Tracking?
In this part of the article, we also want to add code to our previous drawings so that we can change their function. In this way, by adding code, we can change and adjust our drawings in such a way that it can identify and show us the places where the finger touches. To be able to draw a circle that shows where the finger touches, we need to call the addCircle (x, y, 50, Path.Direction.CW) on a Path. The circle we want to draw to show where the finger touches should be displayed when it touches the screen and disappears when it touches the screen.
1- We created an Android project called com.vogella. Android. multitouch.
2- MainActivity is the name of the activity that should be used in this project.
3- The MultitouchView class should look like this:
4- Then, we must add the View to our activity. Like the following:
5- Activity will be as follows:
After adding all the code listed above to the project, we will see that we can draw by drawing the page.
How to create activity (ScaleGestureDetector)
1- Create a new project and select the name de.vogella.android.touch.scaledetector on it.
2- Add an activity to the project and name it ScaleDetectorTestActivity. 3- Enter the codes listed below in the created class.
4- Then, we must add the View to the activity.
As a result of these changes, we will also be able to zoom in and out on images using two fingers.
About our Android and iOS app development services