Package org. example. Touch;
Import Android. App. activity;
Import Android. Graphics. Bitmap;
Import Android. Graphics. bitmapfactory;
Import Android. Graphics. matrix;
Import Android. Graphics. pointf;
Import Android. Hardware. sensormanager;
Import Android. OS. Bundle;
Import Android. util. floatmath;
Import Android. util. log;
Import Android. View. motionevent;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. View. View. ontouchlistener;
Import Android. widget. Button;
Import Android. widget. imageview;
Public class touch extends activity implements ontouchlistener, onclicklistener {
Private Static final string tag = "Touch ";
// These matrices will be used to move and zoom Image
Matrix matrix = new matrix ();
Matrix savedmatrix = new matrix ();
Pointf start = new pointf ();
Pointf mid = new pointf ();
Float olddist;
Private imageview view;
Private button zoomin, zoomout;
// Button zoom
Private float scalewidth = 1;
Private float scaleheight = 1;
Private bitmap BMP, zoomedbmp;
Private int zoom_level = 0;
Private Static final double zoom_in_scale = 1.25; // amplification factor
Private Static final double zoom_out_scale = 0.8; // reduce the Coefficient
// We can be in one of these 3 states
Static final int none = 0;
Static final int drag = 1;
Static final int zoom = 2;
Int mode = none;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Zoom in button
Zoomin = (button) findviewbyid (R. Id. zoom_in );
// Zoom out button
Zoomout = (button) findviewbyid (R. Id. zoom_out );
Zoomin. setonclicklistener (this );
Zoomout. setonclicklistener (this );
View = (imageview) findviewbyid (R. Id. imageview );
View. setontouchlistener (this );
// Obtain the image in drawable, zoom in, zoom out, and multi-point touch
BMP = bitmapfactory. decoderesource (touch. This. getresources (), R. drawable. Butterfly );
}
Public Boolean ontouch (view V, motionevent event ){
// Handle touch events here...
Imageview view = (imageview) V;
// Handle touch events here...
Switch (event. getaction () & motionevent. action_mask ){
// Set the drag Mode
Case motionevent. action_down:
Savedmatrix. Set (matrix );
Start. Set (event. getx (), event. Gety ());
Log. D (TAG, "mode = drag ");
Mode = drag;
Break;
Case motionevent. action_up:
Case motionevent. action_pointer_up:
Mode = none;
Log. D (TAG, "mode = none ");
Break;
// Set the multi-touch mode
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;
// If it is in drag mode, click to move the image
Case motionevent. action_move:
If (mode = drag ){
Matrix. Set (savedmatrix );
// Set the displacement
Matrix. posttranslate (event. getx ()-start. X,
Event. getx ()-start. X );
}
// If the zoom mode is used, the multi-touch zoom mode is used.
Else if (mode = zoom ){
Float newdist = spacing (event );
Log. D (TAG, "newdist =" + newdist );
If (newdist> 10f ){
Matrix. Set (savedmatrix );
Float scale = newdist/olddist;
// Set the scaling ratio and the point position of the image
Matrix. postscale (scale, scale, mid. X, mid. y );
}
}
Break;
}
// Perform the transformation
View. setimagematrix (matrix );
Return true; // indicate event was handled
}
// Calculate the 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 the midpoint
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 );
}
// Zoom in and zoom out button click events
@ Override
Public void onclick (view v ){
If (V = zoomin ){
Enlarge ();
} Else if (V = zoomout ){
Small ();
}
}
// Click the button to narrow down the Function
Private void small (){
Int BMP width = BMP. getwidth ();
Int bmpheight = BMP. getheight ();
Scalewidth = (float) (scalewidth * zoom_out_scale );
Scaleheight = (float) (scaleheight * zoom_out_scale );
Matrix matrix = new matrix ();
Matrix. postscale (scalewidth, scaleheight );
Zoomedbmp = bitmap. createbitmap (BMP, 0, 0, BMP width, bmpheight, matrix,
True );
View. setimagebitmap (zoomedbmp );
}
// Click the button to enlarge the Function
Private void enlarge (){
Try {
Int BMP width = BMP. getwidth ();
Int bmpheight = BMP. getheight ();
Scalewidth = (float) (scalewidth * zoom_in_scale );
Scaleheight = (float) (scaleheight * zoom_in_scale );
Matrix matrix = new matrix ();
Matrix. postscale (scalewidth, scaleheight );
Zoomedbmp = bitmap. createbitmap (BMP, 0, 0, BMP width, bmpheight, matrix,
True );
View. setimagebitmap (zoomedbmp );
} Catch (exception e ){
// Can't zoom because of memory issue, just ignore, no big deal
}
}
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/fhy_2008/archive/2011/02/23/6202476.aspx