触摸事件,可以包含多点触摸,也可以使用捏合手势缩放,并且放大图片;
多点触摸的实现:
num =motionEvent.getPointerCount()//使用MotionEvent的此方法来获取当前手指的数量.
然后使用一个循环,进行手指数据的遍历:
id = 0; while(id < num){ int ptridx = motionEvent.getPointerId(id); float x1 = motionEvent.getX(ptridx); float y1 = motionEvent.getY(ptridx); Log.d(TAG,"x1="+x1+",y1="+y1);//手指的屏幕坐标 float pressure = motionEvent.getPressure(ptridx);//压力 float size = motionEvent.getSize(ptridx);//手指尺寸 id++;//下一根手指 }
其余的全部代码:
package com.lanxin.testtouch; import android.graphics.Matrix; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnTouchListener { private static final String TAG = "TouchLog"; float y = 0; float x = 0; ImageView img; ScaleGestureDetector mScaleGesture; float mScaleFloat = 1f; Matrix matrix = new Matrix(); TextView text2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);//使用relativeLayout来绑定触摸事件 //relativeLayout.setOnTouchListener(this);//添加事件监听 //捏合手势的演示 text2 = (TextView)findViewById(R.id.text2); img = (ImageView)findViewById(R.id.images1); mScaleGesture = new ScaleGestureDetector(this, new ScaleGestureDetector.OnScaleGestureListener() { @Override public boolean onScale(ScaleGestureDetector detector) { mScaleFloat *= detector.getScaleFactor(); String str = ""; str = "比例:"+detector.getScaleFactor()+" mScaleFloat:"+mScaleFloat+"\n"; mScaleFloat = Math.max(0.1f,Math.min(mScaleFloat,5.0f));//图像比例,最小0.1,最大5 str += " mScaleFloat:"+mScaleFloat; text2.setText(str); matrix.setScale(mScaleFloat, mScaleFloat); img.setImageMatrix(matrix); img.invalidate(); return true;//这里一定要返回真,否则不会继续监听事件 } @Override public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) { return true;//这里一定要返回真,否则不会继续监听事件 } @Override public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) { } }); //捏合手势演示结束 } public boolean onTouchEvent(MotionEvent event){ mScaleGesture.onTouchEvent(event);//为捏合手势绑定触摸事件 Log.d(TAG,"onTouchEvent"); return true; } @Override public boolean onTouch(View view, MotionEvent motionEvent) { //多点触摸的代码演示: int num = motionEvent.getPointerCount(); int id = 0; while (id < num){ int ptridx = motionEvent.getPointerId(id); float x1 = motionEvent.getX(ptridx); float y1 = motionEvent.getY(ptridx); Log.d(TAG,"x1="+x1+",y1="+y1);//手指的屏幕坐标 float pressure = motionEvent.getPressure(ptridx);//压力 float size = motionEvent.getSize(ptridx);//手指尺寸 id++;//下一根手指 } String action = ""; switch (motionEvent.getAction()){ case 0: x =motionEvent.getX(); y = motionEvent.getY(); action = "按下"; break; case 1: if (motionEvent.getX() - x < -50){ action = "放开:往左边移动["+(motionEvent.getX() - x) +"]";//小于-50肯定是往左滑动 }else if (motionEvent.getX() - x > 50){ action = "放开:往右边移动["+(motionEvent.getX() - x) +"]";//大于50肯定是往右边滑动 }else { action = "没有移动"; } break; case 2: action = "移动"; break; default: action = "其他:"+motionEvent.getAction(); break; } String buff = "Aciton:"+action+"\n"; buff += "Location:x=>"+x+" y=>"+y+"\n"; buff += "EdgeFlags:"+motionEvent.getEdgeFlags()+"\n"; buff += "pressure:"+motionEvent.getPressure()+"\n"; buff += "size:"+motionEvent.getSize()+"\n"; buff += "downtime:"+motionEvent.getDownTime()+"\n"; buff += "eventtime:"+motionEvent.getEventTime()+"\n"; buff += "Elapsed:"+ (motionEvent.getEventTime() - motionEvent.getDownTime()); Log.d(TAG,buff.toString()); return true; } }
xml布局文件的代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:id="@+id/relativeLayout" tools:context=".MainActivity"> <TextView android:text="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text2"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/images1" android:src="@drawable/wewer" android:layout_marginTop="50dp" android:scaleType="matrix"/> </RelativeLayout>