The examples in this article describe in detail the Android touch screen test code, which can handle touch screen clicks, moves, and leaves, which is of great reference value for Android beginners.
The specific function codes are as follows:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class TouchActivity extends Activity { /*Declare ImageView variable*/ private ImageView mImageView01; /*Declare the relevant variables as the width and height of the stored image, use */ 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) { (savedInstanceState); setContentView(); /* Get screen object */ DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); /* Get screen resolution pixels */ intScreenX = ; intScreenY = ; /* Set the width and height of the picture */ intWidth = 100; intHeight = 100; /*Create ImageView object through findViewById constructor*/ mImageView01 =(ImageView) findViewById(.myImageView1); /* Assign the image from Drawable to ImageView to render*/ (); /* Initialization button position is centered */ RestoreButton(); /* When clicking ImageView, restore the initial position */ (new () { @Override public void onClick(View v) { RestoreButton(); } }); } /*Overwrite touch events*/ public boolean onTouchEvent(MotionEvent event) { /*Get the position of the finger touch screen*/ float x = (); float y = (); try { /*Touch event handling*/ switch (()) { /*Click on the screen*/ case MotionEvent.ACTION_DOWN: picMove(x, y); break; /*Move position*/ case MotionEvent.ACTION_MOVE: picMove(x, y); break; /*Leave the screen*/ case MotionEvent.ACTION_UP: picMove(x, y); break; } }catch(Exception e) { (); } return true; } /*How to move pictures*/ private void picMove(float x, float y) { /*Default fine-tuning of the relative position of the picture and the pointer*/ mX=x-(intWidth/2); mY=y-(intHeight/2); /*Related processing to prevent pictures from exceeding the screen*/ /*Prevent the screen from going to the right and exceeding the screen*/ if((mX+intWidth)>intScreenX) { mX = intScreenX-intWidth; } /*Prevent the screen from going left and exceeding the screen*/ else if(mX<0) { mX = 0; } /*Prevent the screen from going downwards*/ else if ((mY+intHeight)>intScreenY) { mY=intScreenY-intHeight; } /*Prevent the screen from going upwards*/ else if (mY<0) { mY = 0; } /*View the image location through log*/ ("jay", (mX)+","+(mY)); /* Rearrange the position on Layout with the setLayoutParams method */ ( new (intWidth,intHeight,(int) mX,(int)mY) ); } /* Event handling of restore ImageView location */ public void RestoreButton() { intDefaultX = ((intScreenX-intWidth)/2); intDefaultY = ((intScreenY-intHeight)/2); /*Toast restore position coordinate*/ mMakeTextToast ( "("+ (intDefaultX)+ ","+ (intDefaultY)+")",true ); /* Rearrange the position on Layout with the setLayoutParams method */ ( new (intWidth,intHeight,intDefaultX,intDefaultY) ); } /*Customize the method of sending messages*/ public void mMakeTextToast(String str, boolean isLong) { if(isLong==true) { (, str, Toast.LENGTH_LONG).show(); } else { (, str, Toast.LENGTH_SHORT).show(); } } }
Readers can also improve the response processing functions of various events based on this example to make their functions richer.