Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Multi-Touch Application with android

Shulou Source: shulou.com Published: 2022-05-31 16:51:28 09月25日 Update

This article introduces the relevant knowledge of "how to achieve multi-touch applications in android". Many people will encounter such a dilemma in the operation of actual cases, 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!

JhkMultiTouchActivity.java

Package com.android.forlinx; import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.view.WindowManager; public class JhkMultiTouchActivity extends Activity {/ * * Called when the activity is first created. * / @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); / / setContentView (R.layout.main); / / hide title bar requestWindowFeature (Window.FEATURE_NO_TITLE) / / set to full screen getWindow () .setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); / / set to the above MTView setContentView (new MTView (this));}}

MTView.java

Package com.android.forlinx; import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Typeface;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView; public class MTView extends SurfaceView implements SurfaceHolder.Callback {private static final int MAX_TOUCHPOINTS = 10; private static final String START_TEXT = "feel free to touch the screen to test"; private Paint textPaint = new Paint () Private Paint touchPaints [] = new Paint [Max _ TOUCHPOINTS]; private int colors [] = new int [Max _ TOUCHPOINTS]; private int width, height; private float scale = 1.0f; public MTView (Context context) {super (context); SurfaceHolder holder = getHolder (); holder.addCallback (this); setFocusable (true); / / make sure our View gets input focus setFocusableInTouchMode (true) / / ensure that touch screen events init ();} private void init () {/ / initialize 10 different color brushes textPaint.setColor (Color.GREEN); textPaint.setTypeface (null); textPaint.setAlpha (200); colors [0] = Color.BLUE; colors [1] = Color.RED; colors [2] = Color.GREEN Colors [3] = Color.YELLOW; colors [4] = Color.CYAN; colors [5] = Color.MAGENTA; colors [6] = Color.DKGRAY; colors [7] = Color.WHITE; colors [8] = Color.LTGRAY; colors [9] = Color.GRAY; for (int I = 0; I

< MAX_TOUCHPOINTS; i++) { touchPaints[i] = new Paint(); touchPaints[i].setColor(colors[i]); touchPaints[i].setAlpha(50); } } /* * 处理触屏事件 */ @Override public boolean onTouchEvent(MotionEvent event) { // 获得屏幕触点数量 int pointerCount = event.getPointerCount(); if (pointerCount >

MAX_TOUCHPOINTS) {pointerCount = MAX_TOUCHPOINTS;} / / lock Canvas, start the corresponding interface processing Canvas c = getHolder () .lockCanvas (); if (c! = null) {c.drawColor (Color.BLACK) If (event.getAction () = = MotionEvent.ACTION_UP) {/ / when the hand leaves the screen, clear the screen} else {/ / draw a cross on the screen first, and then draw a circle for (int I = 0; I

< pointerCount; i++) { // 获取一个触点的坐标,然后开始绘制 int id = event.getPointerId(i); int x = (int) event.getX(i); int y = (int) event.getY(i); drawCrosshairsAndText(x, y, touchPaints[id], i, id, c); } for (int i = 0; i < pointerCount; i++) { int id = event.getPointerId(i); int x = (int) event.getX(i); int y = (int) event.getY(i); drawCircle(x, y, touchPaints[id], c); } } // 画完后,unlock getHolder().unlockCanvasAndPost(c); } return true; } /** * 画十字及坐标信息 * * @param x * @param y * @param paint * @param ptr * @param id * @param c */ private void drawCrosshairsAndText(int x, int y, Paint paint, int ptr, int id, Canvas c) { c.drawLine(0, y, width, y, paint); c.drawLine(x, 0, x, height, paint); int textY = (int) ((15 + 20 * ptr) * scale); c.drawText("x" + ptr + "=" + x, 10 * scale, textY, textPaint); c.drawText("y" + ptr + "=" + y, 70 * scale, textY, textPaint); c.drawText("id" + ptr + "=" + id, width - 55 * scale, textY, textPaint); } /** * 画圆 * * @param x * @param y * @param paint * @param c */ private void drawCircle(int x, int y, Paint paint, Canvas c) { c.drawCircle(x, y, 40 * scale, paint); } /* * 进入程序时背景画成黑色,然后把"START_TEXT"写到屏幕 */ public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { this.width = width; this.height = height; if (width >

Height) {this.scale = width / 480f;} else {this.scale = height / 480f;} textPaint.setTextSize (14 * scale); Canvas c = getHolder (). LockCanvas (); if (c! = null) {/ / background black c.drawColor (Color.BLACK); float tWidth = textPaint.measureText (START_TEXT) C.drawText (START_TEXT, width / 2-tWidth / 2, height / 2, textPaint); getHolder () .unlockCanvasAndPost (c);} public void surfaceCreated (SurfaceHolder holder) {} public void surfaceDestroyed (SurfaceHolder holder) {}}

Effect picture

This is the end of the content of "how to implement multi-touch applications with 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!

Tags: Screen multipoint application content more knowledge different practical successful next event cross dilemma coordinates reality situation effect effect article title Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS NVidia MariaDB Huawei Microsoft