Android gesture Control Zoom, move picture _android

Source: Internet
Author: User
Tags gety

The example of this article for everyone to share the Android gesture Control Zoom, move the image of the method for your reference, the specific contents are as follows

Create a new touch listener class

Package com.liyong.btprinter; 
Import Android.graphics.Matrix; 
Import Android.graphics.PointF; 
Import Android.util.FloatMath; 
Import android.view.MotionEvent; 
Import Android.view.View; 
Import Android.view.View.OnTouchListener; 

Import Android.widget.ImageView; 
  public class Mulitpointtouchlistener implements Ontouchlistener {private static final String TAG = ' touch '; 
  These matrices is used to move and zoom image matrix = new Matrix (); 

  Matrix Savedmatrix = new Matrix (); 
  We can be as one of these 3 states static final int NONE = 0; 
  static final int DRAG = 1; 
  static final int ZOOM = 2; 

  int mode = NONE; 
  Remember some things for zooming PointF start = new PointF (); 
  PointF mid = new PointF (); 

  float olddist = 1f; 
      @Override public boolean Ontouch (view V, motionevent event) {ImageView view = (ImageView) v; LOG.E ("View_width",//View.getimagematrix (). 
      ToString () + "*" +v.getwidth ()); DuMP Touch event to log dumpevent (event); Handle Touch the events here ... switch (event.getaction () & Motionevent.action_mask) {case MOTIONEVENT.A 
          CTION_DOWN:matrix.set (View.getimagematrix ()); 
          Savedmatrix.set (matrix); 
          Start.set (Event.getx (), event.gety ()); 
          LOG.D (TAG, "Mode=drag"); 

          
          mode = DRAG; 
          LOG.D (TAG, "Mode=none"); 
      Break 
          Case MotionEvent.ACTION_POINTER_DOWN:oldDist = spacing (event); 
          LOG.D (TAG, "olddist=" + olddist); 
              if (Olddist > 10f) {savedmatrix.set (matrix); 
              Midpoint (Mid, event); 
              mode = ZOOM; 
          LOG.D (TAG, "mode=zoom"); 
      } break; 
          Case MotionEvent.ACTION_UP:case MotionEvent.ACTION_POINTER_UP:mode = NONE; 
          LOG.E ("View.getwidth", view.getwidth () + ""); LOG.E ("View.getheight", View.getHeight () + ""); 
      Break  
              Case MotionEvent.ACTION_MOVE:if (mode = = DRAG) {//... matrix.set (Savedmatrix); 
          Matrix.posttranslate (Event.getx ()-Start.x, Event.gety ()-START.Y); 
              else if (mode = = ZOOM) {Float newdist = spacing (event); 
              LOG.D (TAG, "newdist=" + newdist); 
                  if (Newdist > 10f) {matrix.set (Savedmatrix); 
                  float scale = newdist/olddist; 
              Matrix.postscale (scale, scale, mid.x, MID.Y); 
      }} break; 
      } view.setimagematrix (matrix); return true;  Indicate event was handled} private void Dumpevent (Motionevent event) {String names[] = {' Down ', ' up ', 
      "Move", "Cancel", "OUTSIDE", "Pointer_down", "pointer_up", "7", "8", "9?"}; 
      StringBuilder sb = new StringBuilder (); int ACtion = event.getaction (); 
      int actioncode = action & motionevent.action_mask; 
      Sb.append ("Event Action_"). Append (Names[actioncode)); 
          if (Actioncode = = Motionevent.action_pointer_down | | actioncode = = MOTIONEVENT.ACTION_POINTER_UP) { 
          Sb.append ("(pid"). Append (action >> motionevent.action_pointer_id_shift); 
      Sb.append (")"); 
      } sb.append ("["); 
          for (int i = 0; i < Event.getpointercount (); i++) {Sb.append ("#"). append (i); 
          Sb.append ("(pid"). Append (Event.getpointerid (i)); 
          Sb.append (") ="). Append ((int) event.getx (i)); 
          Sb.append (","). Append ((int) event.gety (i)); 
      if (i + 1 < Event.getpointercount ()) sb.append (";"); 
      } sb.append ("]"); 
  LOG.D (TAG, sb.tostring ()); 
      Private float spacing (motionevent event) {float x = event.getx (0)-event.getx (1); Float y = event.gety (0)-EvEnt.gety (1); 
  return FLOATMATH.SQRT (x * x + y * y); 
      } private void Midpoint (PointF point, motionevent event) {float x = event.getx (0) + event.getx (1); 
      Float y = event.gety (0) + event.gety (1); 
  Point.set (X/2, Y/2);
 } 


}

Write an XML file

<?xml version= "1.0" encoding= "UTF-8"?> <linearlayout xmlns:android= 
  "http://schemas.android.com/" Apk/res/android " 
  android:orientation=" vertical " 
  android:layout_height=" fill_parent " 
  android:layout" _width= "Fill_parent" 
  android:id= "@+id/linearlayout1" 
  android:weightsum= "1" > 
    <imageview Android:layout_width= "Fill_parent" 
    android:id= "@+id/priviewimage" 
    android:layout_height= "Fill_parent" " 
    android:layout_gravity=" center " 
    android:scaletype=" Matrix " 
    > 
   </ImageView> 
  </LinearLayout> 

Note the following: android:scaletype= "Matrix"

Priviewimage.setimagebitmap (ACTIVITYPREVIEWIMAGE.CLICK_BITMAP); 
Priviewimage= (ImageView) Findviewbyid (r.id.priviewimage); 

The above is the Android gesture control Zoom to move the picture of all the code, I hope to help you learn.

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.