Android Development Example Multi-point touch program _android

Source: Internet
Author: User
Tags drawtext gety stub

Multi-touch operation of intelligent terminal Equipment brings us all kinds of cool experience, which also makes many Android developers interested in the development of multi-touch programs. In fact, the implementation of multi-touch programs is not so remote, but relatively easy. This article mainly through an example to explain the realization of multi-touch program.

Let's start by understanding the fundamentals of multi-touch in Android.

Android multi-touch in essence requires LCD driver and program itself design support, currently on the market HTC, Motorola and Samsung and other well-known manufacturers, as long as the use of capacitive screen touch principle of mobile phones can support multi-touch multitouch technology, for Web page scaling, There is a better user experience in gesture operation. Events on the Android platform are handled using Motionevent objects, such as triggering action_down when the touch is started, and action_move when the move is finally released, triggering the Action_up event. Of course, there are user-action_cancel operations that can trigger this action.

For conventional touch operations, we are dealing with the Ontouchevent () method implemented by the Setontouchlistener () interface for view. So for the parameters of the Ontouchevent method motionevent we need to have some understanding, such as:

Java code

Event.getaction ()//Get touch action such as Action_down  
event.getpointercount ()//Get the number of touch points, for example 2 may be two fingers and press the screen  
simultaneously Event.getpointerid (NID); For the details of each touch point, we can get the index Event.getx (NID) via a loop execution Getpointerid method  
;//Get the X position  
event.gety (NID) of the NID touch point; Gets the Y position event.getpressure (NID) of the NID point touch  
,//LCD can sense the user's finger pressure, of course the specific level is determined by the driver and physical hardware  
event.getdowntime ()//Press Start time  
event.geteventtime ()///Event End time  
event.geteventtime ()-event.getdowntime ());///Total time spent 

Below, we'll explain how to implement multi-touch with a project.

Let's take a look at the running effect of this instance before the code:

The following steps are implemented:

1, create an Android project, named Multitouchvisible.

2. In this project, create a new view, inherit Surfaceview, and implement the callback interface, where we name the class as Myview.java, the following is the specific implementation code of this class:

Java Code

public class MyView extends Surfaceview implements surfaceholder.callback{multitouchvisible;   
  private static final int max_touchpoints = 10;   
  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 MyView (multitouchvisible father) {super (father);  
    TODO auto-generated constructor stub this.father=father;   
    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 touchscreen Events/@Override public boolean ontouchevent (Motionevent event) {//Get screen  
     Contact quantity 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 {//First in Draw a cross on the screen and draw a circle for (int i = 0; i < Pointercount i++) {//Get aThe coordinates of the contact point, 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 */private void Drawcrosshairsandtext (int x, int y, Paint Paint, int ptr,int ID, Ca   
     Nvas c) {c.drawline (0, y, width, y, paint);   
     C.drawline (x, 0, x, height, paint);   
     int texty = (int) (5 + * ptr);   
     C.drawtext ("x" + ptr + "=" + x, texty, textpaint);   
     C.drawtext ("y" + ptr + "=" + Y, N, texty, textpaint); C.drawtext ("ID "+ ptr +" = "+ ID, width-55, texty, Textpaint); /** * Draw round/private void drawcircle (int x, int y, Paint Paint, Canvas c) {c.drawcircle (x, y   
   , paint, etc.);  @Override public void surfacechanged (surfaceholder holder, int format, int width, int height) {//   
    TODO auto-generated Method Stub this.width = width;   
    This.height = height;   
    Textpaint.settextsize (14);   
    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);  
      
  @Override public void surfacecreated (Surfaceholder holder) {//TODO auto-generated a stub @Override public void surfacedestroyed (Surfaceholder holder) {//TODO auto-generated stub 
      
  }  
} 
 

3, modify the main activity class, put the view into the Setcontentview () to display, the specific implementation of the following:

Java code

The public class Multitouchvisible extends activity {/** called the ' when ' is the ' The activity ' is the ' the '  
  ---' 
  @Override 
  public void OnCreate (Bundle savedinstancestate) {  
    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 MyView (this));   
  }  
 

4, to this, the project has been completed, visible multi-touch implementation is not difficult! Run this instance program to get the interface as shown in the above effect graph.

The above is the Android Multi-Touch information collation, follow-up to continue to collate relevant information, thank you for your support to this site.

Related Article

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.