進階篇-安卓系統:2.多點觸控的交互處理

1.android 觸摸事件偵聽android

安卓的用戶交互方式包括兩種,一種是點擊交互,一種是觸摸交互。點擊交互就是手指按下擡起一個動做組。而觸摸交互分爲按下(down),移動(move),擡起(up)。api

觸摸事件偵聽代碼:輸出觸摸事件的三個動做:app

咱們用一個framlayout佈局進行操做ide

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。佈局

運行結果:this

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.獲取觸摸的當前座標google

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
            }
        });

    }
}

輸出結果:spa

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.實現拖動控件.net

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
            }
        });

    }
}

運行結果:code

該圖片沒法顯示!

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
            }
        });

      
    }

   
}
相關文章
相關標籤/搜索