Android touch screen test sample code

  • 2020-06-03 08:14:10
  • OfStack

The example of this paper describes the Android touch screen test code in detail, which can handle the click, move, leave and other events of the touch screen, which has a good reference value for Android beginners.

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 {
  /* The statement ImageView variable */
  private ImageView mImageView01;
  /* Declare related variables to store image width and height , use */
  private int intWidth, intHeight, intDefaultX, intDefaultY;
  private float mX, mY; 
  /* Declares the resolution variable for the storage screen  */
  private int intScreenX, intScreenY;
  public void onCreate(Bundle savedInstanceState)
  {
   super.onCreate(savedInstanceState); 
   setContentView(R.layout.main);
   
   /*  Get the screen object  */
   DisplayMetrics dm = new DisplayMetrics(); 
   getWindowManager().getDefaultDisplay().getMetrics(dm);
   
   /*  Gets the screen resolution pixel  */
   intScreenX = dm.widthPixels;
   intScreenY = dm.heightPixels;
   
   /*  Set the width and height of the image  */
   intWidth = 100;
   intHeight = 100;
   /* through findViewById Constructor creation ImageView object */ 
   mImageView01 =(ImageView) findViewById(R.id.myImageView1);
   /* From the picture Drawable Assigned to ImageView To render */
   mImageView01.setImageResource(R.drawable.baby);
   
   /*  The initialization button is centered  */
   RestoreButton();
   
   /*  When you click ImageView , restore the initial position  */
   mImageView01.setOnClickListener(new Button.OnClickListener()
   {
    @Override
    public void onClick(View v)
    {
     RestoreButton();
    }
   });
  }
  
  /* Override touch events */
  public boolean onTouchEvent(MotionEvent event) 
  {
   /* Get the position of the finger on the screen */
   float x = event.getX();
   float y = event.getY();
   
   try
   {
    /* Handling of touch events */
    switch (event.getAction()) 
    {
     /* Click on the screen */
     case MotionEvent.ACTION_DOWN:
      picMove(x, y);
       break;
     /* Mobile location */
     case MotionEvent.ACTION_MOVE:
      picMove(x, y);
       break;
     /* Leave the screen */
     case MotionEvent.ACTION_UP:
      picMove(x, y); 
       break;
    }
   }catch(Exception e)
    {
     e.printStackTrace();
    }
   return true;
  }
  /* How to move an image */
  private void picMove(float x, float y)
  {
   /* The default position of the image relative to the pointer is tweaked */
   mX=x-(intWidth/2);
   mY=y-(intHeight/2);
   
   /* Prevent images from exceeding the screen related processing */
   /* Prevent the screen from overrunning the screen to the right */
   if((mX+intWidth)>intScreenX)
   {
    mX = intScreenX-intWidth;
   }
   /* Prevent the screen from going left over the screen */
   else if(mX<0)
   {
    mX = 0;
   }
   /* Prevent the screen from falling over the screen */
   else if ((mY+intHeight)>intScreenY)
   {
    mY=intScreenY-intHeight;
   }
   /* Prevent the screen from going up over the screen */
   else if (mY<0)
   {
    mY = 0;
   }
   /* through log  To see where the picture is */
   Log.i("jay", Float.toString(mX)+","+Float.toString(mY));
   /*  In order to setLayoutParams Method, rearrange Layout On the location of the  */
   mImageView01.setLayoutParams
   (
    new AbsoluteLayout.LayoutParams
    (intWidth,intHeight,(int) mX,(int)mY)
   );
  }
  
  /*  reduction ImageView Event handling for location  */
  public void RestoreButton()
  {
   intDefaultX = ((intScreenX-intWidth)/2);
   intDefaultY = ((intScreenY-intHeight)/2);
   /*Toast Reduction position coordinates */
   mMakeTextToast
   (
    "("+
    Integer.toString(intDefaultX)+
    ","+
    Integer.toString(intDefaultY)+")",true
   );
   
   /*  In order to setLayoutParams Method, rearrange Layout On the location of the  */
   mImageView01.setLayoutParams
   (
    new AbsoluteLayout.LayoutParams
    (intWidth,intHeight,intDefaultX,intDefaultY)
   );
  }
  
  /* The custom 1 A way to send a message */
  public void mMakeTextToast(String str, boolean isLong)
  {
   if(isLong==true)
   {
    Toast.makeText(TouchActivity.this, str, Toast.LENGTH_LONG).show();
   }
   else
   {
    Toast.makeText(TouchActivity.this, str, Toast.LENGTH_SHORT).show();
   }
  }
}

The reader can also improve the response handling functions of various events on the basis of this example to make its functions more rich.


Related articles: