Android multi-point touch for Image Scaling

Source: Internet
Author: User
Tags gety

 

Package com. Touch;
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 {
Matrix matrix = new matrix ();
Matrix savedmatrix = new matrix ();

Static final int none = 0;
Static final int drag = 1;
Static final int zoom = 2;
Int mode = none;

Pointf start = new pointf ();
Pointf mid = new pointf ();
Float olddist = 1f;

@ Override
Public Boolean ontouch (view V, motionevent event ){

Imageview view = (imageview) V;

Switch (event. getaction () & motionevent. action_mask ){
Case motionevent. action_down:

Matrix. Set (view. getimagematrix ());
Savedmatrix. Set (matrix );
Start. Set (event. getx (), event. Gety ());
Mode = drag;


Break;
Case motionevent. action_pointer_down:
Olddist = spacing (event );
If (olddist> 10f ){
Savedmatrix. Set (matrix );
Midpoint (MID, event );
Mode = zoom;
}
Break;
Case motionevent. action_up:
Case motionevent. action_pointer_up:
Mode = none;

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 );
If (newdist> 10f ){
Matrix. Set (savedmatrix );
Float scale = newdist/olddist;
Matrix. postscale (scale, scale, mid. X, mid. y );
}
}
Break;
}

View. setimagematrix (matrix );
Return true;
}



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 );
}
}

Imageview. setontouchlistener (New mulitpointtouchlistener (); in XML, change the image scaling format to matrix.

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.