Android多点触摸缩放图片-android学习之旅(四)

获取多触摸点

核心代码:

获取触摸点的个数和位置

public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    System.out.println("down");
                    break;
                case MotionEvent.ACTION_UP:
                    System.out.println("触摸点的个数:"+event.getPointerCount());
                    System.out.println(String.format("x1:%f y1:%f x2:%f y2:%f",event.getX(0),event.getY(0),event.getX(1),event.getY(1)));
//                  System.out.println("up");
//                  FrameLayout.LayoutParams lp = (LayoutParams) image.getLayoutParams();
//                  lp.leftMargin = (int)event.getX();
//                  lp.rightMargin = (int)event.getY();
//                  image.setLayoutParams(lp);
//                  System.out.println(String.format("x:%f,y:%f", event.getX(),event.getY()));
                    break;
                case MotionEvent.ACTION_MOVE:
                    System.out.println("move");
                    break;

两个手指缩放android机器人图片

frame.setOnTouchListener(new OnTouchListener() {

            @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        System.out.println("down");
                        break;
                    case MotionEvent.ACTION_UP:
                        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 (lastDisatance < 0) {
                                lastDisatance = currentDistance;
                            }else {
                                if (currentDistance-lastDisatance > 5) {
                                    FrameLayout.LayoutParams lp = (LayoutParams) image.getLayoutParams();
                                    lp.width = (int) (1.1f*image.getWidth());
                                    lp.height = (int) (1.1f*image.getHeight());
                                    image.setLayoutParams(lp);
                                    lastDisatance = currentDistance;
                                }else if (lastDisatance-currentDistance > 5) {
                                    FrameLayout.LayoutParams lp = (LayoutParams) image.getLayoutParams();
                                    lp.width = (int) (0.9f*image.getWidth());
                                    lp.height = (int) (0.9f*image.getHeight());
                                    image.setLayoutParams(lp);
                                    lastDisatance = currentDistance;
                                }
                            }
                        }
//                      System.out.println("触摸点的个数:"+event.getPointerCount());
//                      System.out.println(String.format("x1:%f y1:%f x2:%f y2:%f",event.getX(0),event.getY(0),event.getX(1),event.getY(1)));
    //                  System.out.println("up");
    //                  FrameLayout.LayoutParams lp = (LayoutParams) image.getLayoutParams();
    //                  lp.leftMargin = (int)event.getX();
    //                  lp.rightMargin = (int)event.getY();
    //                  image.setLayoutParams(lp);
    //                  System.out.println(String.format("x:%f,y:%f", event.getX(),event.getY()));
                        break;
                    case MotionEvent.ACTION_MOVE:
                        System.out.println("move");
                        break;
                default:
                    break;
                }
                return false;
            }
        });
上一篇:DIV+CSS IE6/IE7/IE8/FF兼容问题大全


下一篇:Android(蓝牙)