用多点触摸缩放图像

  • 使用Eclipse创建工程:
    Project name: Touch
    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);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值