Android Multi-Touch zoom drag and drop instance

Source: Internet
Author: User

When viewing images on Android or browsing the web, we often have the need to enlarge or reduce the number of images or pages, so that we can get more details.
Or get more information, multi-touch and bloom is the technology that satisfies this scenario.

Here is an example of how to implement the drag function of the picture:

Program run: 1. Initialize interface 2. For shrinking interface 3. For enlarged interface


Create a new Android project called Draganddrop, with the following directory structure:

The layout code for the activity_layout.xml of the main interface is as follows:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    >    <imageview         android:id= "@+id/show_img"        android:layout_width= "Wrap_content"        Android : layout_height= "wrap_content"        android:scaletype= "Matrix"        android:src= "@drawable/s001"        android: Background= "#ff0000"        android:contentdescription= "@string/app_name"/></relativelayout>

Readers need to note that our ImageView control is set to the matrix so that you can easily control the image in the code.

We put a picture (larger than the phone screen) in the drawable-mdpi file.

Picture resources are as follows:



The code for Mainactivity.java 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 PR Esent.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{//declares a coordinate point private PointF startpoint;// Declare and instantiate a matrix to control the image private Matrix matrix = new Matrix ();//Declare and instantiate the current picture of the matrixprivate matrix Mcurrentmatrix = new Matrix () ;//Scaling initial distance private float startdistance;//Drag tag private static final int DRAG = 1;//scaled markup private static final int zoom = 2;/ /identity Record private int mode;//scaling intermediate point 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");//At this time in the drag mode mode = drag;//Get the coordinates of the current press point startpoint = new PointF (Event.getx (), event.gety ()); /Set the matrix of the current picture to the Matrixmcurrentmatrix.set (Imageview.getimagematrix ()) of the slice, break;case Motionevent.action_movE:LOG.W ("Drag", "action_move");//Depending on the mode to perform the corresponding scaling or drag operation switch (mode) {case drag://moving x-coordinate distance of float dx = event.getx ()- startpoint.x;//move the y-coordinate distance of float dy = event.gety ()-startpoint.y;//Set the current Matrixmatrix.set (Mcurrentmatrix) of the Matrix;// Tell the matrix the distance matrix.posttranslate (dx, dy) of the x and Y axes to move, break;case zoom://calculate the distance of the zoom float enddistance = distance (event);// Calculates the zoom ratio float scale = enddistance/startdistance;//Sets the current Matrixmatrix.set (Mcurrentmatrix);//sets the parameter for scaling Matrix.postscale ( Scale, scale, midpoint.x, midpoint.y); break;default:break;} break;//already has a finger holding down the screen, and then a finger pressing the screen will trigger the event case MOTIONEVENT.ACTION_POINTER_DOWN:LOG.W ("Drag", "Action_pointer_down"); /At this point, mode = zoom;//The distance between two points at the start of the calculation startdistance = Distance (event),//when the distance of two points is greater than 10 o'clock, the zoom operation if (Startdistance > 10) {// Calculates the middle point midpoint = Mid (event);//The Bloom multiplier of the photo Mcurrentmatrix.set (Imageview.getimagematrix ()) Before the zoom operation is obtained;} break;//already has a finger left on the screen, and there is a finger on the screen that will trigger the event case MOTIONEVENT.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;} Move the picture to a certain location according to the requirements of the Matrix Imageview.setimagematrix (matrix);//return True indicates that we will consume the action, do not need the parent control for further processing 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);}}
There are comments in the code that are not detailed here!


Program source code Download

http://download.csdn.net/detail/cqtddt/8308631

Android Multi-Touch zoom drag and drop instance

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.