Android Touch Screen test Instance code _android

Source: Internet
Author: User
Tags touch

The example of this article describes the Android touch screen test code, can realize the touch screen of the Click, move, leave and other events, for Android beginners have a good reference value.

The specific function code is as follows:

Package com.test;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.DisplayMetrics;
Import Android.util.Log;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.widget.AbsoluteLayout;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import Android.widget.Toast;
  public class Touchactivity extends activity {/* DECLARE ImageView variable/private ImageView mImageView01;
  /* Declare related variables as a store picture wide, position using * * Private int intwidth, intheight, Intdefaultx, intdefaulty; 
  Private float MX, my;
  /* Declare the resolution variable of the storage screen * * Private int intscreenx, Intscreeny; 
   public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
   
   Setcontentview (R.layout.main); 
   /* Get Screen Object * * displaymetrics DM = new Displaymetrics ();
   
   Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
   /* Get screen Resolution pixel * * Intscreenx = dm.widthpixels;
   
   Intscreeny = Dm.heightpixels;
   /* Set the picture's width/intwidth = 100;
   intheight = 100; /* ThroughFindviewbyid constructor creates ImageView object/mImageView01 = (ImageView) Findviewbyid (R.ID.MYIMAGEVIEW1);
   
   /* Assign the picture from drawable to ImageView to present/Mimageview01.setimageresource (R.drawable.baby);
   
   /* Initialize button position Center/Restorebutton (); /* When clicked ImageView, restore initial position/Mimageview01.setonclicklistener (new Button.onclicklistener () {@Override public vo
    ID OnClick (View v) {Restorebutton ();
  }
   });
   /* Overwrite Touch Event/public boolean ontouchevent (Motionevent event) {/* Get the position of the finger touch screen/float x = Event.getx ();
   
   Float y = event.gety (); try {/* Touch event handling/switch (Event.getaction ()) {/* Click on screen/Case MotionEvent.ACTION_DOWN:picM
       Ove (x, y);
     Break
       /* Mobile Position * * Case MotionEvent.ACTION_MOVE:picMove (x, y);
     Break 
       * * Leave screen/Case MotionEvent.ACTION_UP:picMove (x, y);
    Break
    }}catch (Exception e) {e.printstacktrace ();
  return true; /* The method of moving the picture * * private void PICMOve (float x, float y) {/* default trimmer picture with pointer relative position * * * mx=x-(INTWIDTH/2);
   
   my=y-(INTHEIGHT/2);
   /* Anti-screen RELATED processing * * * prevent screen to right over screen/if ((mx+intwidth) >intscreenx) {MX = Intscreenx-intwidth;
   /* Prevent screen to left over screen */else if (mx<0) {MX = 0;
   /* * Prevents the screen from being down over the screen/else if ((my+intheight) >intscreeny) {my=intscreeny-intheight;
   /* * Prevent the screen from up to over screen/else if (my<0) {my = 0;
   */* through Log to view picture position/LOG.I ("Jay", Float.tostring (MX) + "," +float.tostring (my)); /* with Setlayoutparams method, rearrange the position on the layout/mimageview01.setlayoutparams (New Absolutelayout.layoutparams (INTW
  Idth,intheight, (int) MX, (int) my);
   /* Restore event Handling at ImageView location/public void Restorebutton () {Intdefaultx = ((intscreenx-intwidth)/2);
   Intdefaulty = ((intscreeny-intheight)/2); /*toast Restore Position Coordinates * * Mmaketexttoast ("+ integer.tostring (INTDEFAULTX) +", "+ integer.tostring (intdefaul
   
   TY) + ")", true); /* to SetlayoutparaMs Method, rearranging the position on the layout/mimageview01.setlayoutparams (New Absolutelayout.layoutparams (intwidth,intheight,in
  tdefaultx,intdefaulty)); }/* Customize a method of sending a message * * public void Mmaketexttoast (String str, Boolean Islong) {if (islong==true) {Toast.mak
   EText (Touchactivity.this, str, toast.length_long). Show ();
   else {toast.maketext (touchactivity.this, str, toast.length_short). Show (); }
  }
}

Readers can also improve the response processing functions of various events on the basis of this instance, making them richer in functionality.

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.