This article describes the implementation of Multi-Touch (Multitouch) for Android programming. Share to everyone for your reference, specific as follows:
If you are interested in developing a multi-touch program, then this article will be a good start, Android application development, the multi-touch is not so remote, the implementation is very simple, this example requires only two classes to achieve multi-touch.
First take a look at our view class Mtview.java:
Package Com.ideasandroid.demo;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
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 = 1
0;
private static final String Start_text = "Please feel free to touch the screen for testing";
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 {Super (context);
Surfaceholder holder = Getholder ();
Holder.addcallback (this); Setfocusable (TRUE); Make sure that our view gets the input focus Setfocusableintouchmode (TRUE);
Ensure that the touch-screen event init () is received;
private void Init () {//Initialize 10 different colors of brush textpaint.setcolor (Color.White);
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]); }/* * Handle touch Event/@Override public boolean ontouchevent (Motionevent event) {//Get screen contact number int Pointe
Rcount = 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 {//first draw a cross on the screen, then draw a circle for (int i = 0; i < Pointercount; i++) {//get the coordinates of a contact, and then start drawing 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);
After the drawing is finished, unlock getholder (). Unlockcanvasandpost (c);
return true; /** * Draw Cross and coordinate information * * @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) ((+ * ptr) * scale);
C.drawtext ("x" + ptr + "=" + x, * scale, texty, textpaint);
C.drawtext ("y" + ptr + "=" + y, * scale, texty, textpaint); C.drawtext ("id" + ptr + "=" + ID, width-55 * scale, texty, textpaint); /** * Draw Round * * @param x * @param y * @param paint * @param c */private void drawcircle (int x, in
T y, Paint Paint, Canvas c) {c.drawcircle (x, Y, scale, Paint); * * * Enter the program when the background is painted black, and then write "Start_text" to the screen/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 (* 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) {}}
The corresponding comments are made in the code, and there is not much to say here.
Next, look at our Activity,multitouchvisible.java.
Package Com.ideasandroid.demo;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Window;
Import Android.view.WindowManager;
public class Multitouchvisible extends activity {
@Override public
void OnCreate (Bundle savedinstancestate) { C8/>super.oncreate (savedinstancestate);
Hide title bar
requestwindowfeature (window.feature_no_title);
Set up the perfect screen
GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Set to the Mtview
Setcontentview above (new Mtview (this));
}
More interested readers of Android-related content can view the site: Android Development Primer and Advanced tutorials, the summary of operational tips for Android programming, and the Android View View tips Summary, Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Resource Operation tips Summary and the "Android Controls usage Summary"
I hope this article will help you with your Android programming.