Android multi-touch demo

Source: Internet
Author: User

Mainactivity:

Package Potter. back; </P> <p> Import android. app. activity; </P> <p> public class main extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> // hide the title bar <br/> requestwindowfeature (window. feature_no_title); <br/> // set to full screen <br/> getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); <br/> // set it to the mtview above <br/> setcontentview (New mysurfaceview (this); <br/>}< br/>}

Surfaceview:

 

Package Potter. back; </P> <p> Import android. content. context; <br/> Import android. graphics. canvas; <br/> Import android. graphics. color; <br/> Import android. graphics. paint; <br/> Import android. view. motionevent; <br/> Import android. view. surfaceholder; <br/> Import android. view. surfaceholder. callback; <br/> Import android. view. surfaceview; </P> <p> public class mysurfaceview extends surfaceview implements call Back {<br/> Private Static final int max_touchpoints = 10; <br/> Private Static final string start_text = "touch the screen to test "; <br/> private paint textpaint = new paint (); <br/> private paint touchpaints [] = new paint [max_touchpoints]; <br/> private int colors [] = new int [max_touchpoints]; <br/> private int width and height; <br/> private float scale = 1.0f; </P> <p> Public mysurfaceview (context) {<br/> supe R (context); <br/> surfaceholder holder = getholder (); <br/> holder. addcallback (this); <br/> setfocusable (true); // ensure that our view can obtain the input focus <br/> setfocusableintouchmode (true ); // ensure that the touch screen event is received <br/> Init (); <br/>}< br/> Public void Init () {<br/> // initialize 10 paint brushes of different colors <br/> textpaint. setcolor (color. white); <br/> colors [0] = color. blue; <br/> colors [1] = color. red; <br/> colors [2] = color. green; <br/> colors [3] = color. yellow; <Br/> colors [4] = color. cyan; <br/> colors [5] = color. magenta; <br/> colors [6] = color. dkgray; <br/> colors [7] = color. white; <br/> colors [8] = color. ltgray; <br/> colors [9] = color. gray; <br/> for (INT I = 0; I <max_touchpoints; I ++) {<br/> touchpaints [I] = new paint (); <br/> touchpaints [I]. setcolor (colors [I]); <br/>}</P> <p> @ override <br/> Public Boolean ontouchevent (motionevent event) {<br/> int pointerc Ount = event. getpointercount (); <br/> If (pointercount> max_touchpoints) {<br/> pointercount = max_touchpoints; <br/>}< br/> canvas c = getholder (). lockcanvas (); <br/> If (C! = NULL) {<br/> C. drawcolor (color. black); <br/> If (event. getaction () = motionevent. action_up) {<br/> // clear the screen when your hand leaves the screen <br/> C. drawcolor (color. black); <br/>}else {<br/> // first draw a cross on the screen and then draw a circle <br/> for (INT I = 0; I <pointercount; I ++) {<br/> // obtain the coordinates of a contact and start to draw <br/> int id = event. getpointerid (I); <br/> int x = (INT) event. getx (I); <br/> int y = (INT) event. gety (I); <br/> drawcrosshairsandtext (X, Y, touchpaints [ID], I, ID, c); <br />}< Br/> for (INT I = 0; I <pointercount; I ++) {<br/> int id = event. getpointerid (I); <br/> int x = (INT) event. getx (I); <br/> int y = (INT) event. gety (I); <br/> drawcircle (X, Y, touchpaints [ID], C ); <br/>}< br/> getholder (). unlockcanvasandpost (c); <br/>}< br/> return true; <br/>}</P> <p> private void drawcrosshairsandtext (int x, int y, paint paint, int PTR, int ID, canvas c) {<br/> C. drawline (0, Y, width, Y, Pai NT); <br/> C. drawline (x, 0, x, height, paint); <br/> int texty = (INT) (15 + 20 * PTR) * scale ); <br/> C. drawtext ("X" + PTR + "=" + X, 10 * scale, texty, textpaint); <br/> C. drawtext ("Y" + PTR + "=" + Y, 70 * scale, texty, textpaint); <br/> C. drawtext ("ID" + PTR + "=" + id, width-55 * scale, texty, textpaint ); <br/>}</P> <p> private void drawcircle (int x, int y, paint, canvas c) {<br/> C. drawci Rcle (X, Y, 40 * scale, paint); <br/>}</P> <p> @ override <br/> Public void surfacechanged (surfaceholder holder, int format, int width, <br/> int height) {<br/> This. width = width; <br/> This. height = height; <br/> If (width> height) {<br/> This. scale = width/480f; <br/>} else {<br/> This. scale = height/480f; <br/>}< br/> textpaint. settextsize (14 * scale); <br/> canvas c = getholder (). lockcanva S (); <br/> If (C! = NULL) {// black background <br/> C. drawcolor (color. black); <br/> float twidth = textpaint. measuretext (start_text); <br/> C. drawtext (start_text, width/2-twidth/2, height/2, textpaint); <br/> getholder (). unlockcanvasandpost (c); <br/>}< br/> @ override <br/> Public void surfacecreated (surfaceholder holder) {<br/> // todo auto-generated method stub </P> <p >}< br/> @ override <br/> Public void surfacedestroyed (surfaceholder holder) {<br/> // todo auto-generated method stub </P> <p >}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.