- 使用Eclipse创建工程:
Build Target: Android 2.1
Application name: Touch
Package name: org.example.touch
Create Activity: Touch
- 修改main布局——res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/butterfly"
android:scaleType="matrix" > <!--使用matrix控制View的缩放-->
</ImageView>
</FrameLayout>
- AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.touch"
android:versionCode="1"
android:versionName="1.0" >
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
...
- 最重要的代码文件——Touch.java
package org.example.touch;
import android.app.Activity;
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.OnTouchListener;
import android.widget.ImageView;
// 实现OnTouchListener接口是为了使用onTouch方法处理View
public class Touch extends Activity implements OnTouchListener
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView view = (ImageView) findViewById(R.id.imageView);
view.setOnTouchListener(this);
}
// These matrices will be used to move and zoom image
Matrix mMatrix = new Matrix();
Matrix mSavedMatrix = new Matrix();
float mOldDist = 0f;
// 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 mMode = NONE;
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
mSavedMatrix.set(mMatrix);
start.set(event.getX(), event.getY());
mMode = DRAG;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mMode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mMode == DRAG)
{
mMatrix.set(mSavedMatrix);
mMatrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
}
else if (mMode == ZOOM)
{
float newDist = spacing(event);
if (newDist > 10f) {
mMatrix.set(mSavedMatrix);
float scale = newDist / mOldDist;
mMatrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
mOldDist = spacing(event);
if (mOldDist > 10f) {
mSavedMatrix.set(mMatrix);
midPoint(mid, event);
mMode = ZOOM;
}
break;
}
// Perform the transformation
view.setImageMatrix(mMatrix);
return true; // indicate event was handled
}
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);
}
}