Layout:
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/relativeLayout1"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button
Android: id = "@ + id/zoom_in"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentTop = "true"
Android: text = "zoom_in"/>
<Button
Android: id = "@ + id/zoom_out"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: layout_alignParentTop = "true"
Android: text = "zoom_out"/>
<ScrollView
Android: id = "@ + id/imageContainer"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_below = "@ + id/zoom_in"
Android: fadingEdge = "none"
Android: scrollbars = "none">
<HorizontalScrollView
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_centerHorizontal = "true"
Android: fadingEdge = "none"
Android: scrollbars = "none">
<ImageView
Android: id = "@ + id/imageView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentTop = "true"
Android: layout_centerHorizontal = "true"
Android: scaleType = "matrix"/>
</HorizontalScrollView>
</ScrollView>
</RelativeLayout>
Program code:
Copy codeThe Code is as follows: package taokun. demo. MutilTouchDemo;
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Matrix;
Import android. graphics. PointF;
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 MutilTouchDemoActivity 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 (MutilTouchDemoActivity. this. getResources (), R. drawable. splash );
}
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 ){
}
}
}