Android multi-touch scaling and drag-and-drop instances
When you view images or browse webpages on Android, you may need to zoom in or out images or webpages to obtain more details.
Or get more full-picture information. The multi-touch and bloom functions are exactly the technologies that meet this application scenario.
The following uses an example to learn how to drag an image:
Program running: 1. initialize the interface 2. Zoom in the interface 3. Zoom in the interface
Create an Android project named DragAndDrop. The directory structure is as follows:
The layout code of activity_layout.xml on the main interface is as follows:
You need to note that our ImageView control method is set to matrix, so that you can easily control the image in the code.
We put an image (larger than the mobile phone screen) in the drawable-mdpi file.
Image resources are as follows:
The MainActivity. java code is as follows:
Package com. shen. draganddrop; import android. app. activity; import android. graphics. matrix; import android. graphics. pointF; import android. OS. bundle; import android. util. log; import android. view. menu; import android. view. menuItem; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; import android. widget. imageView; public class MainActivity extends Activity {private ImageView imageView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageView = (ImageView) findViewById (R. id. show_img); imageView. setOnTouchListener (new ImageTouchListener () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml.int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item);} private class ImageTouchListener implements OnTouchListener {// declare a coordinate point private PointF startPoint; // declare and instantiate a Matrix to control the image's private Matrix matrix = new Matrix (); // declare and instantiate the current image's Matrixprivate Matrix mCurrentMatrix = new Matrix (); // The initial distance from private float startDistance During Scaling; // The DRAG mark private static final int DRAG = 1; // The scale mark private static final int ZOOM = 2; // ID record private int mode; // the center point of scaling private PointF midPoint; @ Overridepublic boolean onTouch (View v, MotionEvent event) {switch (event. getAction () & MotionEvent. ACTION_MASK) {case MotionEvent. ACTION_DOWN: System. out. println (ACTION_DOWN); Log. w (Drag, ACTION_DOWN); // In the DRAG mode, mode = Drag; // obtain the coordinate startPoint = new PointF (event. getX (), event. getY (); // set the Matrix of the current image to the MatrixmCurrentMatrix of the slice. set (imageView. getImageMatrix (); break; case MotionEvent. ACTION_MOVE: Log. w (Drag, ACTION_MOVE); // perform the corresponding scaling or dragging operation switch (mode) {case DRAG: // float dx = event. getX ()-startPoint. x; // float dy = event. getY ()-startPoint. y; // set the current matrixmatrix of the Matrix. set (mCurrentMatrix); // indicates the distance between the X and Y axes of the matrix to be moved. postTranslate (dx, dy); break; case ZOOM: // calculates the scale distance from float endDistance = distance (event); // calculates the scale ratio float scale = endDistance/startDistance; // set the current Matrixmatrix. set (mCurrentMatrix); // sets the scaled parameter matrix. postScale (scale, scale, midPoint. x, midPoint. y); break; default: break;} break; // a finger has been holding down the screen, and another finger pressing the screen will trigger this event case MotionEvent. ACTION_POINTER_DOWN: Log. w (Drag, ACTION_POINTER_DOWN); // In this case, the scaling mode is mode = ZOOM; // startDistance = distance (event ); // perform the zoom operation if (startDistance> 10) {// calculate the intermediate point midPoint = mid (event) when the distance between the two points is greater than 10 ); // obtain the image bloom multiple mCurrentMatrix before scaling. set (imageView. getImageMatrix ();} break; // The case MotionEvent is triggered when one finger has left the screen and the other finger is on the screen. ACTION_POINTER_UP: Log. w (Drag, ACTION_POINTER_UP); mode = 0; break; case MotionEvent. ACTION_UP: Log. w (Drag, ACTION_UP); mode = 0; break; default: break;} // follow the requirements of Matrix to move the image to a certain position in imageView. setImageMatrix (matrix); // return true indicates that we will consume this action, and no further processing of the parent control is required. return true;} public static float distance (MotionEvent event) {float dx = event. getX (1)-event. getX (0); float dy = event. getY (1)-event. getY (0); return (float) Math. sqrt (dx * dx + dy * dy);} public static PointF mid (MotionEvent event) {float x = (event. getX (1)-event. getX (0)/2; float y = (event. getY (1)-event. getY (0)/2; return new PointF (x, y );}}Comments in the Code are not described here!