安卓触摸事件集锦

  1. 设置触摸面板的监听;

root.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");
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.
                        LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
                lp.leftMargin = (int) event.getX();
                lp.topMargin = (int) event.getY();
                root.setLayoutParams(lp);
                //获取触摸点的坐标
                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;  //触发成功才会进行后续事件的触发
    }
});

2.如果想获取多个触摸点可采用方法:

event.getPointerCount();

3.获取某个触摸点的坐标:

event.getX(*);
event.getY(*);

4.安卓图片的放大缩小缩放

root = (FrameLayout)findViewById(R.id.container);
imageView = (ImageView) findViewById(R.id.img);
root.setOnTouchListener(new View.OnTouchListener() {
    float currentDistance;  //当前的位置
    float lastDistance = -1; //上次的位置
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                System.out.println("action move");
                System.out.println(event.getPointerCount());
                //只有当触摸点超过两个求距离
                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 {
                        //在安卓环境中,当手指放上去的时候就会有显示,会有误差,如果按照理论
                        //设置为零,当你的手指刚放上去就会忽大忽小,所以这块的5是一个容错范围,
                        //可根据自己的软件的具体环境设置相应的值
                        if(currentDistance -lastDistance > 5 ) {
                            System.out.println("放大");
                            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) imageView.getLayoutParams();
                            lp.width = (int) (1.1f*imageView.getWidth());
                            lp.height = (int) (1.1f*imageView.getHeight());
                            imageView.setLayoutParams(lp);
                            lastDistance = currentDistance;
                        } else if(lastDistance -currentDistance > 5) {
                            System.out.println("缩小");
                            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) imageView.getLayoutParams();
                            lp.width = (int) (0.9f*imageView.getWidth());
                            lp.height = (int) (0.9f*imageView.getHeight());
                            imageView.setLayoutParams(lp);
                            lastDistance = currentDistance;
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;  //触发成功才会进行后续事件的触发
    }
});

如果一直缩小到0,那么这张图片就会消失,所以,应该增加一个为零判断,这部分可自行设置。

你可能感兴趣的:(android)