用简单的方法,在android上实现手势放大缩小功能

用简单的方法,在网易新闻android上实现手势放大缩小功能

package com.example.eventtest;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.graphics.Point;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {

	
	double nLenStart = 0;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {	
				
		int nCnt = event.getPointerCount();
		
		int n = event.getAction();
		if( (event.getAction()&MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN && 2 == nCnt)//2表示两个手指
		{
					
			for(int i=0; i< nCnt; i++)
			{
				float x = event.getX(i);
				float y = event.getY(i);
				
				Point pt = new Point((int)x, (int)y);
	
			}
			
			int xlen = Math.abs((int)event.getX(0) - (int)event.getX(1));
			int ylen = Math.abs((int)event.getY(0) - (int)event.getY(1));
			
			nLenStart = Math.sqrt((double)xlen*xlen + (double)ylen * ylen);
			
			
		}else if( (event.getAction()&MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_UP  && 2 == nCnt)
		{
			
			for(int i=0; i< nCnt; i++)
			{
				float x = event.getX(i);
				float y = event.getY(i);
				
				Point pt = new Point((int)x, (int)y);

			}
			
			int xlen = Math.abs((int)event.getX(0) - (int)event.getX(1));
			int ylen = Math.abs((int)event.getY(0) - (int)event.getY(1));
			
			double nLenEnd = Math.sqrt((double)xlen*xlen + (double)ylen * ylen);
			
			if(nLenEnd > nLenStart)//通过两个手指开始距离和结束距离,来判断放大缩小
			{
				Toast.makeText(getApplicationContext(), "放大", 3000).show();
			}
			else
			{
				Toast.makeText(getApplicationContext(), "缩小", 3000).show();
			}
		}
		
		
	
		return super.onTouchEvent(event);
	}


}


基础知识:

getAction()方法返回的是int类型,用到的只有低16位,其中:低八位是动作的类型,高8位是触摸点索引值的表示(单点为0,双点为1)

获得动作类型: int action = event.getAction() & ACTION_MASK 或者使用 getActionMaskeAAAAAAAAAA>获得触摸点索引类型: int pointerIndex = (event.getAction() & ACTION_POINTER_INDEX_MASK ) >> ACTION_POINTER_INDEX_SHIFT

或者使用 getActionIndex()

http://www.bkjia.com/Androidjc/867752.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/867752.htmlTechArticle用简单的方法,在android上实现手势放大缩小功能 package com.example.eventtest;import java.util.ArrayList;import android.os.Bundle;import android.app.Activity;impor...

本文源自: AG平台官网

上一篇:NSArray中存的是实体时的排序,NSArray实体排序

下一篇:没有了