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

来源:互联网 发布:阿里云快照收费吗 编辑:程序博客网 时间:2024/06/05 23:52

获取多触摸点

核心代码:

获取触摸点的个数和位置

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;            }        });
0 0
原创粉丝点击