Analysis of Android Multi-Touch control multitouch
Source: Internet
Author: User
<span id="Label3"></p><span style="font-family:Courier New; font-size:18px"><span style="font-family:Courier New; font-size:18px">disclaimer:</span></span><p><p><span style="font-family:Courier New; font-size:18px">Reference: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0226/914.html</span></p></p><p><p><span style="font-family:Courier New; font-size:18px">The following implements how the Ontouchevent () method is handled for general control touch operations through the application layer that supports multi-touch operation. At the same time, some understanding of the parameter motionevent of Ontouchevent method is Made.<br></span></p></p><p><p><span style="font-family:Courier New; font-size:18px"><br></span></p></p><span style="font-family:Courier New; font-size:18px"><span style="font-family:Courier New; font-size:18px">text:</span></span><p><p><span style="font-family:Courier New; font-size:18px">Here's a sample of two apps to learn about Multitouch in Android.</span></p></p><p><p><span style="font-family:Courier New; font-size:18px"><br></span></p></p><span style="font-family:Courier New; font-size:18px"><span style="font-family:Courier New; font-size:18px">Example One (demomultitouch-canvas):</span></span><span style="font-family:Courier New; font-size:18px"><span style="font-family:Courier New; font-size:18px">Core Technology Introduction</span></span><p><p><span style="font-family:Courier New; font-size:18px">This example uses canvas in Surfaceview to show the User's multitouch, but the core technology is still a common method: ontouchevent (motionevent event)</span></p></p><p><p><span style="font-family:Courier New; font-size:18px">Motionevent There are several ways to pay attention, but there are some common methods, if you ontouchevent use More.<br></span></p></p><p><p><span style="font-family:Courier New; font-size:18px">Event.getaction ()//get Touch Action like Action_down<br>Event.getpointercount (); Get the number of touch points, such as 2 may be two fingers simultaneously press the screen<br>Event.getpointerid (nID); For the details of each touch point, we can get the index through a loop execution Getpointerid method<br>Event.getx (nID); Gets the x position of the first, nid touch Point<br>Event.gety (nID); Gets the y position of the first-click touch<br>Event.getpressure (nID); The LCD can sense the User's finger pressure, of course, the specific level is determined by the drive and physical hardware<br>Event.getdowntime ()//press Start time<br>Event.geteventtime ()//event End Time<br>Event.geteventtime ()-event.getdowntime ()); Time spent in total press<br></span></p></p><p><p><span style="font-family:Courier New; font-size:18px"><br></span></p></p><span style="font-family:Courier New; font-size:18px"><span style="font-family:Courier New; font-size:18px">Code Show:</span></span><p><p></p></p><pre name="code" class="java">public class Mtview extends Surfaceview implements Surfaceholder.callback {private static final int max_touchpoints = 10;p Rivate static final String Start_text = "please Feel free to touch the screen to test";p rivate paint textpaint = new paint ();p rivate paint touchpaints[] = New paint[max_touchpoints];p rivate int colors[] = new int[max_touchpoints];p rivate int width, height;private Float scale = 1.0f;public mtview (context Context) {super (context); Surfaceholder holder = Getholder (); holder.addcallback (this); setfocusable (true); Make sure that our view gets the input focus Setfocusableintouchmode (true); Make sure to receive the Touch event init ();} private void init () {//initialize 10 different color brushes 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_TOUCHPOINT S I++) {touchpaints[i] = new Paint (); touchpaints[i].setcolor (colors[i]);}} /** * Handle TouchScreen Event */@Overridepublic Boolean ontouchevent (motionevent Event) {//get The number of screen contacts int pointercount = Event.getpointercount (); If (pointercount > Max_touchpoints) {pointercount = max_touchpoints;} Lock the canvas and start the appropriate 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 each contact and coordinate information for (int i = 0; i < pointercount; i++) {int ID = Event.getpointerid (i); int x = (int) event.getx (i); int y = (int) event.gety (i);d rawcrosshairsandtext (x, y, touchpaints[i d], i, id, c);} Draw a circle for each contact (int i = 0; i < pointercount; i++) {int id = event.getpointerid (i); int x = (int) event.getx (i); int y = (int) event.gety (i);d rawcircle (x, y, touchpaints[id], c);}} After the painting, unlockgetholder (). unlockcanvasandpost (c);} Return true;} /** * Draw Cross and coordinate information * * @param x * @param y * @param paint * @param ptr * @param ID * @param c */private void Drawcrosshairsan Dtext (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, ten * scale, texty, textpaint), c.drawtext ("y" + ptr + "=" + y, [x] scale, texty, textpaint); c.drawtext ("i D "+ ptr +" = "+ id, width-55 * scale, texty, textpaint);} /** * Draw Circle * * @param x * @param y * @param paint * @param c */private void drawcircle (int x, int y, paint paint, Canvas C) {c.drawcircle (x, y, * scale, paint);} /** * Enter the program when the background is painted black, 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/4 80f;} Textpaint.settextsize (* scale); Canvas C = Getholder (). Lockcanvas (); If (c! = Null) {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) {}} </pre><p><p></p></p><p><p><span style="font-family:Courier New; font-size:18px"><br></span></p></p><p><p><span style="font-family:Courier New; font-size:18px">The use is also so easy:</span></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">Setcontentview (new Mtview (this));</pre></pre><p><p></p></p><p><p><span style="font-family:Courier New; font-size:18px"><br></span></p></p><span style="font-family:Courier New; font-size:18px"><span style="font-family:Courier New; font-size:18px">run:</span></span><p><p><br></p></p><span style="font-family:Courier New; font-size:18px"><span style="font-family:Courier New; font-size:18px"><br></span></span><span style="font-family:Courier New; font-size:18px"><span style="font-family:Courier New; font-size:18px">Example Two (demomultitouch-imageview):</span></span><p><p><span style="font-family:Courier New; font-size:18px">Zoom in or out of an image with a multi-touch screen beforelenght to save the distance between two o'clock of the previous time afterlenght to save the distance between two o'clock of the current time</span><br></p></p><p><p></p></p><pre code_snippet_id="671091" snippet_file_name="blog_20150520_3_1421336" name="code" class="java"><pre code_snippet_id="671091" snippet_file_name="blog_20150520_3_1421336" name="code" class="java">public void Scalewithfinger (motionevent event) {float moveX = event.getx (1)-event.getx (0); float Movey = event.gety (1)- Event.gety (0); Switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:beforeLenght = (float) math.sqrt ((moveX * MoveX ) + (movey * movey)); break;case motionevent.action_move://get The length between two points afterlenght = (float) math.sqrt ((moveX * moveX) + (mov EY * movey)); Float gaplenght = afterlenght-beforelenght;if (gaplenght = = 0) {break;} If the current time two o'clock distance is greater than the previous two point distance, then pass 0, otherwise 1if (gaplenght > 0) {this.setscale (scale, 0);} else {this.setscale (scale, 1);} Beforelenght = afterlenght;break;}}}</pre></pre><p><p></p></p><p><p><span style="font-family:Courier New; font-size:18px"><br></span></p></p><p><p><span style="font-family:Courier New; font-size:18px">Let the picture follow the position of the finger touch screen move beforex, y is used to save the coordinates of the previous position afterx, y is used to save the current position of the Coordinates. Their difference is the increment or decrease of the ImageView Coordinates.</span><br></p></p><pre code_snippet_id="671091" snippet_file_name="blog_20150520_4_6900122" name="code" class="java"><pre code_snippet_id="671091" snippet_file_name="blog_20150520_4_6900122" name="code" class="java">public void Movewithfinger (motionevent event) {switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:beforeX = Event.getx (); Beforey = event.gety (); break;case MotionEvent.ACTION_MOVE:afterX = event.getx (); aftery = event.gety (); This.setlocation ((int) (afterx-beforex), (int) (aftery-beforey)); Beforex = Afterx;beforey = Aftery;break;case MotionEv Ent. action_up:break;}}</pre></pre><p><p><span style="font-family:Courier New; font-size:18px"><br></span></p></p><span style="font-family:Courier New; font-size:18px"><span style="font-family:Courier New; font-size:18px">used to zoom in and out ImageView because the picture is filled imageview, so there is the effect of zooming in and out of the image flag 0 is a magnified image, for 1 is to reduce the picture</span></span><p><p></p></p><pre code_snippet_id="671091" snippet_file_name="blog_20150520_5_4231696" name="code" class="java"><pre code_snippet_id="671091" snippet_file_name="blog_20150520_5_4231696" name="code" class="java">private void Setscale (float temp, int flag) {if (flag = = 0) {this.setframe (this.getleft ()-(int) (temp * this.getwidth ()) , This.gettop ()-(int) (temp * this.getheight ()), this.getright () + (int) (temp * this.getwidth ()), this.getbottom () + (int) (temp * This.getheight ()));} else {this.setframe (this.getleft () + (int) (temp * this.getwidth ()), this.gettop () + (int) (temp * this.getheight ()), This. GetRight ()-(int) (temp * this.getwidth ()), this.getbottom ()-(int) (temp * this.getheight ()));}}</pre></pre><p><p></p></p><p><p><span style="font-family:Courier New; font-size:18px"><br></span></p></p><span style="font-family:Courier New; font-size:18px"><span style="font-family:Courier New; font-size:18px">the Ontouchevent event is as Follows:</span></span><p><p></p></p><pre code_snippet_id="671091" snippet_file_name="blog_20150520_6_2709105" name="code" class="java"><pre code_snippet_id="671091" snippet_file_name="blog_20150520_6_2709105" name="code" class="java">public boolean ontouchevent (motionevent event) {if (inview (imageView, event)) {if (event.getpointercount () = = 2) {imagevi Ew.scalewithfinger (event);} else if (event.getpointercount () = = 1) {imageview.movewithfinger (event);}} Return true;}</pre></pre><span style="font-family:Courier New; font-size:18px"><span style="font-family:Courier New; font-size:18px"><br></span></span><p><p></p></p><span style="font-family:Courier New; font-size:18px"><span style="font-family:Courier New; font-size:18px">Source Download:</span></span><p><p><span style="font-family:Courier New; font-size:18px">http://download.csdn.net/detail/u013761665/8719349</span><br></p></p><p><p>Analysis of Android Multi-Touch control multitouch</p></p></span>
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