Android Multi-Touch

Source: Internet
Author: User

public class mainactivity extends activity implements ontouchlistener {//   Zoom Out Matrix matrix = new matrix (); Matrix savedmatrix = new matrix (); Pointf start = new pointf (); Pointf mid = new pointf (); float olddist;private imageview myimageview;//   Mode static final int none = 0;static final int drag = 1; static final int zoom = 2;int mode = none; @Overridepublic  void  oncreate (bundle savedinstancestate)  {super.oncreate (savedinstancestate); SetContentView ( R.layout.main);myimageview =  (ImageView)  findviewbyid (r.id.iv_image); Myimageview.setontouchlistener (this);} @Overridepublic  boolean ontouch (view v, motionevent event)  {ImageView  myimageview =  (ImageView)  v;switch  (event.getaction () &nbsP;& motionevent.action_mask)  {//  Set drag mode case motionevent.action_down:log.i ("point",   "Action_pointer_down"); Matrix.set (Myimageview.getimagematrix ()); Savedmatrix.set (matrix); Start.set ( Event.getx (),  event.gety ());mode = drag;break;case motionevent.action_up:case  motionevent.action_pointer_up:mode = none;break;//  setting Multi-touch mode Case motionevent.action_pointer _down:olddist = spacing (Event);if  (olddist > 10f)  {savedmatrix.set (matrix); Midpoint (mid, event); mode = zoom;} break;//  if it is DRAG mode, click Move Picture case motionevent.action_move:if  (Mode == drag)  { Matrix.set (Savedmatrix); Matrix.posttranslate (Event.getx ()  - start.x, event.gety ()  -  START.Y);}   If zoom mode, tap Touch zoom else if  (mode == zoom)  {float newDist =  Spacing (event);if  (newdist > 10f)  {matrix.set (Savedmatrix); float scale = newdist / olddist;//  set the zoom ratio and the midpoint position of the picture Matrix.postscale (scale, scale,  MID.X, MID.Y);}} break;} Myimageview.setimagematrix (matrix); return true;}   Calculate moving distance 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);}   Calculate midpoint position Private void midpoint (pointf point, motionevent event)  {float  x = event.getx (0)  + event.getx (1); float y = event.gety (0)  +  event.gety (1);p oint.set (X / 2, Y / 2);}}
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/     Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " Android:background= "@android: Color/black" > <imageview android:id= "@+id/iv_image" android:layout _width= "Wrap_content" android:layout_height= "wrap_content" android:scaletype= "Matrix" android:src= "@d Rawable/koala "/></linearlayout>


Android Multi-Touch

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.