布局的监听事件重写方法:
layout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; } });
获得布局的监听事件,注意返回值改为true,否则在执行了DOWN以后不再执行UP和MOVE操作。
public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_MOVE break; } return true; }
获得在屏幕上点击数量(即手指数量)的函数,每一个手机可以获得的最大数量不等
event.getPointerCount()
可以通过move对图片的大小进行操作
case MotionEvent.ACTION_MOVE: if (event.getPointerCount() >= 2) { float offsetX = event.getX(0) - event.getX(1);//监听手机的PointerCount数量大于2时,获得两个点的坐标 float offsetY = event.getY(0) - event.getY(1); currentdistance = (float) Math.sqrt(offsetX * offsetX + offsetY * offsetY); if (lastdistance < 0) { lastdistance = currentdistance; } else { if (currentdistance - lastdistance > 5) {//像素改变大于5是为了容错的存在 RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) img.getLayoutParams(); layoutParams.width = (int) (img.getWidth() * 1.1); layoutParams.height = (int) (img.getHeight() * 1.1); img.setLayoutParams(layoutParams); lastdistance = currentdistance; } else if (lastdistance - currentdistance > 5) { RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) img.getLayoutParams(); layoutParams.width = (int) (img.getWidth() * 0.9); layoutParams.height = (int) (img.getHeight() * 0.9); img.setLayoutParams(layoutParams); lastdistance = currentdistance; } } }