<?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" >
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"/>
</scrollview>
</RelativeLayout>
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 is 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;//magnification factor
Private static final Double Zoom_out_scale = 0.8;//reduction factor
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);
//Amplification button
zoomin = (Button) Findviewbyid ( R.ID.ZOOM_IN);
//Shrink button
zoomout = (Button) Findviewbyid ( R.id.zoom_out);
Zoomin.setonclicklistener (this);
Zoomout.setonclicklistener (this);
view = (ImageView) Findviewbyid (R.id.imageview);
View.setontouchlistener (this);
//Get pictures in drawable, zoom in, zoom out, touch objects
bmp = Bitmapfactory.decoderesource (MutilTouchDemoActivity.this.getResources (), R.drawable.splash);
}
public boolean Ontouch (View V, motionevent event) {
Handle Touch the events here ...
ImageView view = (ImageView) v;
Handle Touch the events here ...
Switch (event.getaction () & Motionevent.action_mask) {
Set 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 multi-point 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 you are drag mode, click Move Picture
Case Motionevent.action_move:
if (mode = = DRAG) {
Matrix.set (Savedmatrix);
Set displacement
Matrix.posttranslate (Event.getx ()-Start.x,
Event.getx ()-start.x);
}
For zoom mode, multi-point touch scaling
else if (mode = = ZOOM) {
float newdist = spacing (event);
LOG.D (TAG, "newdist=" + newdist);
if (Newdist > 10f) {
Matrix.set (Savedmatrix);
float scale = newdist/olddist;
Set zoom ratio and picture midpoint position
Matrix.postscale (scale, scale, mid.x, MID.Y);
}
}
Break
}
Perform the transformation
View.setimagematrix (matrix);
return true;//indicate event is handled
}
//Calculate move distance
& nbsp 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);
Point.set (X/2, Y/2);
}
Zoom in, Zoom Out button click event
@Override
public void OnClick (View v) {
if (v = = zoomin) {
Enlarge ();
}else if (v = = zoomout) {
Small ();
}
}
button click to Shrink function
private void Small () {
int bmpwidth = 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, bmpwidth, bmpheight, Matrix,
true);
View.setimagebitmap (zoomedbmp);
}
button click to enlarge function
private void enlarge () {
try {
int bmpwidth = 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, bmpwidth, bmpheight, Matrix,
true);
View.setimagebitmap (zoomedbmp);
catch (Exception e) {
}
}
}