Android多点触摸交互处理

在FrameLayout中学习Android的单点和多点触摸交互,并实现图像拖拽移动,放大和缩小。

单点触摸
即为view设置监听事件setOnTouchListener,通过MotionEvent event获得触摸的相关操作,如event.getAction()获取触摸的动作,event.getX(),event.getY()分别获取触摸点的坐标。

        View view = inflater.inflate(R.layout.fragment_touch, container, false);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        System.out.println("ACTION_DOWN");
                        break;
                    case MotionEvent.ACTION_MOVE:
                        System.out.println("ACTION_MOVE"); 
                        System.out.println(String.format("X:%f,Y:%f", event.getX(), event.getY()));
                        break;
                    case MotionEvent.ACTION_UP:
                        System.out.println("ACTION_UP");
                        break;
                }
                return true;

}

多点触摸
只要使用event.getPointerCount()就能得到触摸点的个数。多个触摸点的坐标也可以通过event.getX(int index),event.getX(int index)获取,index从0开始。

图像拖拽移动,放大和缩小。
我们利用event的相关操作获取触摸点的信息并将它通过layoutParams传递给图片参数设置,从而控制图像的位置大小,达到移动,放大和缩小。

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_touch, container, false);
        iv_move = (ImageView) view.findViewById(R.id.iv_move);
        tv_xy= (TextView) view.findViewById(R.id.tv_xy);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        System.out.println("ACTION_DOWN");
                        break;
                    case MotionEvent.ACTION_MOVE:
                        System.out.println("ACTION_MOVE");
//                        System.out.println(String.format("X:%f,Y:%f", event.getX(), event.getY()));
//                        System.out.println(event.getPointerCount());
                        tv_xy.setText(String.format("X:%f,Y:%f", event.getX(), event.getY()));
                        layoutParams = (FrameLayout.LayoutParams) iv_move.getLayoutParams();
//                        layoutParams.setMargins((int) event.getX(),(int) event.getY(),0,0);
                        if (event.getPointerCount()>=2) {
                            float offsetX = event.getX(0) - event.getX(1);
                            float offsetY = event.getY(0) - event.getY(1);
                            currentdistance = (float) Math.sqrt(offsetX * offsetX + offsetY * offsetY);
                            if (lastdistance < 0) {
                                lastdistance = currentdistance;
                            } else if (lastdistance - currentdistance < 5) {
                                System.out.println("放大");
                                layoutParams.width = (int) (iv_move.getWidth() *1.05f);
                                layoutParams.height = (int) (iv_move.getHeight() * 1.05f);
                                lastdistance = currentdistance;
                            } else if (lastdistance - currentdistance > 5) {
                                System.out.println("缩小");
                                layoutParams.width = (int) (iv_move.getWidth() *0.95f);
                                layoutParams.height = (int) (iv_move.getHeight() * 0.95f);
                                lastdistance = currentdistance;
                            }
                        }
                        iv_move.setLayoutParams(layoutParams);
                        break;
                    case MotionEvent.ACTION_UP:
                        System.out.println("ACTION_UP");
                        break;
                }
                return true;
            }
        });
        return view;
    }

你可能感兴趣的:(android)