Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of Multi-touch in Android

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to achieve multi-touch function in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Using the Android system API to achieve multi-touch function, multi-touch has certain requirements for the hardware of the device. At present, almost all mobile phones on the market can achieve multi-touch.

The most important thing to achieve multi-touch is API:

Event.getPointerCount ()

It means that the number of pointers to the data contained in this event is always greater than or equal to 1. Can be simply understood as the number of fingers on the phone screen, why is it always greater than or equal to 1? Because the trigger event needs to touch the screen with your finger, a pointer must be generated when you touch the screen.

Override these two functions:

One:

Public boolean onTouchEvent (MotionEvent event)

Call a touch screen event to complete the handling of touch screen related events by operating event.

The above code:

Public boolean onTouchEvent (MotionEvent event) {/ / if (event.getX (1) = = event.getX (0) & & event.getY (1) = = event.getY (0) if (event.getPointerCount () = = 2) {x2 = event.getX (1); y2 = event.getY (1);} x1 = event.getX (0); y1 = event.getY (0); myView.invalidate () Log.d ("multiTouch", event.getPointerCount () + "xy1:" + x1 + "," + y1 + "xy2:" + x2 + "," + y2); return super.onTouchEvent (event);}

Two:

Protected void onDraw (Canvas canvas)

By rewriting onDraw to draw, Canvas means "holds the" draw "calls to draw something" (an introduction to the official document, which is already easy to understand)

The above code:

Protected void onDraw (Canvas canvas) {super.onDraw (canvas); canvas.drawColor (Color.WHITE); Paint paint = new Paint (); paint.setAntiAlias (true); paint.setStyle (Paint.Style.FILL); paint.setColor (Color.BLUE); canvas.drawCircle (x1, y1, 30, paint); canvas.drawRect (x2-30, y2-30, x2 + 30, y2 + 30, paint) Paint.setColor (Color.RED); canvas.drawLine (x1, y1, x2, y2, paint); Shader mShader = new LinearGradient (0,0,100,100, new int [] {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW}, null, Shader.TileMode.REPEAT); paint.setShader (mShader); paint.setTextSize (14); canvas.drawText ("xy1:" + x1 + ") "+ y1,80,50, paint); canvas.drawText (" xy2: "+ x2 +"; "+ y2,80,120, paint);}

Implementing onDraw needs to be done in a class that inherits from View

Private class TestView extends View {public TestView (Context context) {super (context);}.

After completing the above operations, the main program is simple, initialize and then call the previously written code

Public class multiTouch extends Activity {public float x1, x2, y1, y2; private TestView testView; @ Override public void onCreate (Bundle savedInstanceState) {requestWindowFeature (Window.FEATURE_NO_TITLE); x1 = 30; y1 = 30; x2 = 200; y2 = 30; super.onCreate (savedInstanceState); testView = new TestView (this); setContentView (testView);}

Realization effect diagram

The circle represents the finger when touching the screen for the first time, and the square represents the finger when touching the screen for the second time.

This is the end of the content of "how to achieve multi-touch function in Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report