码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-05-13 14:52:46      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:多触摸   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;
            }
        });

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

标签:多触摸   android   

原文地址:http://blog.csdn.net/lpjishu/article/details/45691523

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!