zoukankan      html  css  js  c++  java
  • 进阶篇-安卓系统:2.多点触控的交互处理

    1.android 触摸事件侦听

    安卓的用户交互方式包括两种,一种是点击交互,一种是触摸交互。点击交互就是手指按下抬起一个动作组。而触摸交互分为按下(down),移动(move),抬起(up)。

    触摸事件侦听代码:输出触摸事件的三个动作:

    我们用一个framlayout布局进行操作

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.FrameLayout;
    
    public class MainActivity extends AppCompatActivity {
        private FrameLayout container;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            container = (FrameLayout) findViewById(R.id.container);
            container.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            System.out.println("action:down");
                            break;
                        case MotionEvent.ACTION_MOVE:
                            System.out.println("action:move");
                            break;
                        case MotionEvent.ACTION_UP:
                            System.out.println("action:up");
                            break;
                    }
                    return true;   //remember to change this return to ture
                }
            });
    
        }
    }

    注意:记得把onTouch方法的返回值改成true。

    运行结果:

    07-13 14:35:19.883 11720-11720/bhu.com.myapplication I/System.out: action:down
    07-13 14:35:19.924 11720-11720/bhu.com.myapplication I/System.out: action:move
    07-13 14:35:19.954 11720-11720/bhu.com.myapplication I/System.out: action:move
    07-13 14:35:19.964 11720-11720/bhu.com.myapplication I/System.out: action:move
    07-13 14:35:19.984 11720-11720/bhu.com.myapplication I/System.out: action:move
    07-13 14:35:19.984 11720-11720/bhu.com.myapplication I/System.out: action:up

    2.获取触摸的当前坐标

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.FrameLayout;
    
    public class MainActivity extends AppCompatActivity {
        private FrameLayout container;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            container = (FrameLayout) findViewById(R.id.container);
            container.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            System.out.println("action:down");
                            break;
                        case MotionEvent.ACTION_MOVE:
    
                            System.out.println(String.format("(%f,%f)",motionEvent.getX(),motionEvent.getY()));
    
                            break;
                        case MotionEvent.ACTION_UP:
                            System.out.println("action:up");
                            break;
                    }
                    return true;   //remember to change this return to ture
                }
            });
    
        }
    }

    输出结果:

    07-13 14:39:50.442 11720-11720/bhu.com.myapplication I/System.out: (366.251312,682.000000)
    07-13 14:39:50.452 11720-11720/bhu.com.myapplication I/System.out: (367.076874,682.000000)
    07-13 14:39:50.472 11720-11720/bhu.com.myapplication I/System.out: (367.000000,680.500000)
    07-13 14:39:50.482 11720-11720/bhu.com.myapplication I/System.out: (368.067780,681.000000)
    07-13 14:39:50.512 11720-11720/bhu.com.myapplication I/System.out: (369.000000,681.000000)
    07-13 14:39:50.532 11720-11720/bhu.com.myapplication I/System.out: (370.142609,681.000000)
    07-13 14:39:50.542 11720-11720/bhu.com.myapplication I/System.out: (371.500000,681.000000)
    07-13 14:39:50.582 11720-11720/bhu.com.myapplication I/System.out: (373.497681,681.000000)
    07-13 14:39:50.592 11720-11720/bhu.com.myapplication I/System.out: (374.960114,681.000000)
    07-13 14:39:50.612 11720-11720/bhu.com.myapplication I/System.out: (376.413116,681.000000)

    3.实现拖动控件

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.FrameLayout;
    import android.widget.ImageView;
    
    public class MainActivity extends AppCompatActivity {
        private FrameLayout container;
        private ImageView imv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            container = (FrameLayout) findViewById(R.id.container);
            imv = (ImageView) findViewById(R.id.imv);
            container.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            System.out.println("action:down");
                            break;
                        case MotionEvent.ACTION_MOVE:
    
                            //System.out.println(String.format("(%f,%f)",motionEvent.getX(),motionEvent.getY()));
                            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) imv.getLayoutParams();
                            lp.leftMargin = (int) motionEvent.getX();
                            lp.topMargin = (int) motionEvent.getY();
    
                            imv.setLayoutParams(lp);
    
    
                            break;
                        case MotionEvent.ACTION_UP:
                            System.out.println("action:up");
                            break;
                    }
                    return true;   //remember to change this return to ture
                }
            });
    
        }
    }

    运行结果:

    该图片无法显示!

    4.获取多个触控点的坐标

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.FrameLayout;
    import android.widget.ImageView;
    
    public class MainActivity extends AppCompatActivity {
        private FrameLayout container;
        private ImageView imv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            container = (FrameLayout) findViewById(R.id.container);
            imv = (ImageView) findViewById(R.id.imv);
            container.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            System.out.println("action:down");
                            break;
                        case MotionEvent.ACTION_MOVE:
    
                            //System.out.println(String.format("(%f,%f)",motionEvent.getX(),motionEvent.getY()));
                            System.out.println("pointers count:"+motionEvent.getPointerCount());  //get the number of pointers
    
                            System.out.println(String.format("point1:(%f,%f)  point2:(%f,%f)",motionEvent.getX(0),motionEvent.getY(0),motionEvent.getX(1),motionEvent.getY(1)));
                            //when you put just one finger on the screen,there will be a exception,because there is not a getX(1).
    
                            break;
                        case MotionEvent.ACTION_UP:
                            System.out.println("action:up");
                            break;
                    }
                    return true;   //remember to change this return to ture
                }
            });
    
        }

    输出结果:

    07-13 14:54:52.855 5804-5804/bhu.com.myapplication I/System.out: point1:(245.296295,718.407410)  point2:(456.000000,389.000000)
    07-13 14:54:52.865 5804-5804/bhu.com.myapplication I/System.out: pointers count:2
    07-13 14:54:52.865 5804-5804/bhu.com.myapplication I/System.out: point1:(244.000000,721.000000)  point2:(455.000000,392.000000)
    07-13 14:54:52.885 5804-5804/bhu.com.myapplication I/System.out: pointers count:2
    07-13 14:54:52.885 5804-5804/bhu.com.myapplication I/System.out: point1:(242.387100,722.612915)  point2:(454.000000,394.000000)
    07-13 14:54:52.895 5804-5804/bhu.com.myapplication I/System.out: pointers count:2
    07-13 14:54:52.905 5804-5804/bhu.com.myapplication I/System.out: point1:(242.000000,724.000000)  point2:(453.000000,396.000000)
    07-13 14:54:52.915 5804-5804/bhu.com.myapplication I/System.out: pointers count:2
    07-13 14:54:52.915 5804-5804/bhu.com.myapplication I/System.out: point1:(241.000000,724.000000)  point2:(452.000000,397.000000)
    07-13 14:54:52.935 5804-5804/bhu.com.myapplication I/System.out: pointers count:2
    07-13 14:54:52.935 5804-5804/bhu.com.myapplication I/System.out: point1:(241.000000,725.000000)  point2:(452.000000,397.000000)

    5.根据手势动作实现图片的缩放(两点触控)<并且可以拖动>

    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.FrameLayout;
    import android.widget.ImageView;
    
    import com.google.android.gms.appindexing.Action;
    import com.google.android.gms.appindexing.AppIndex;
    import com.google.android.gms.common.api.GoogleApiClient;
    
    public class MainActivity extends AppCompatActivity {
        private FrameLayout container;
        private ImageView imv;
       
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            container = (FrameLayout) findViewById(R.id.container);
            imv = (ImageView) findViewById(R.id.imv);
            container.setOnTouchListener(new View.OnTouchListener() {
    
                float cureentDistance;
                float lastDistance = -1; //the distance of two point can not be a minus,when the distance is -1 ,it means it's a initial value.
    
    
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            System.out.println("action:down");
                            break;
                        case MotionEvent.ACTION_MOVE:
    
                            if (motionEvent.getPointerCount() >= 2) {//there must be two finger on the screen
    
    
                                float offsetX = motionEvent.getX(0) - motionEvent.getX(1);
                                float offsetY = motionEvent.getY(1) - motionEvent.getY(1);
    
                                cureentDistance = (float) Math.sqrt(offsetX * offsetX + offsetY * offsetY);
                                if (lastDistance < 0) {
                                    lastDistance = cureentDistance;
                                } else {
                                    if (cureentDistance - lastDistance > 5) {
                                        System.out.println("Zoom in");
                                        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) imv.getLayoutParams();
                                        lp.width = (int) (1.1f*imv.getWidth());
                                        lp.height = (int) (1.1f*imv.getHeight());
                                        imv.setLayoutParams(lp);
    
                                        lastDistance = cureentDistance;
                                    } else if (lastDistance - cureentDistance > 5) {
                                        System.out.println("Zoom out");
                                        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) imv.getLayoutParams();
                                        lp.width = (int) (0.9f*imv.getWidth());
                                        lp.height = (int) (0.9f*imv.getHeight());
                                        imv.setLayoutParams(lp);
                                        lastDistance = cureentDistance;
                                    }
                                }
                            }else{
                                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) imv.getLayoutParams();
                                lp.leftMargin = (int) (motionEvent.getX()-(imv.getWidth()/2));
                                lp.topMargin = (int) (motionEvent.getY()-(imv.getHeight()/2));
    
                                imv.setLayoutParams(lp);
    
                            }
    
                            break;
                        case MotionEvent.ACTION_UP:
                            System.out.println("action:up");
                            break;
                    }
                    return true;   //remember to change this return to ture
                }
            });
    
          
        }
    
       
    }
  • 相关阅读:
    2021年2月4号
    2021年2月3号
    2021年2月2号
    2021年2月1日
    2021年1月31日
    2021年1月30日
    20171205xlVBA往返航班组合
    选择文件
    从VBA过渡到Python
    20171114xlVba选定单行记录并打印
  • 原文地址:https://www.cnblogs.com/androidNot/p/5667051.html
Copyright © 2011-2022 走看看