多指触摸事件

package test.bwie.com.circleview;

import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class Images_TouchActivity extends AppCompatActivity implements View.OnTouchListener{


     private ImageView Img;
    private Matrix matrix  = new Matrix();
    private Matrix savematrix = new Matrix();

    private static final int NONE = 0;
    private static final int DRAG = 1;
    private static final int ZOOM = 2;
    private int mode = NONE;

    // 第一个按下的手指的点
    private PointF startPoint = new PointF();
    // 两个按下的手指的触摸点的中点
    private PointF midPoint = new PointF();
    // 初始的两个手指按下的触摸点的距离
    private float oriDis = 1f;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_images__touch);
        initView();
    }
    private void initView() {
        Img = (ImageView) findViewById(R.id.Img);
        Img.setOnTouchListener(this);
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView img = (ImageView) v;
        switch (event.getAction() & MotionEvent.ACTION_MASK){
            case MotionEvent.ACTION_DOWN:
                //第一个手指按下事件
                matrix.set(img.getImageMatrix());
                savematrix.set(matrix);
                startPoint.set(event.getX(),event.getY());
                //拖动
                mode=DRAG;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                // 第二个手指按下事件
                oriDis = distance(event);
                if (oriDis > 10f) {
                    savematrix.set(matrix);
                    midPoint = middle(event);
                    //标记成缩放状态
                    mode = ZOOM;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode==DRAG){
                    matrix.set(savematrix);
                    matrix.postTranslate(event.getX()-startPoint.x,event.getY()-startPoint.y);
                    Log.e("TAG---","移动。。。。。。");
                }else if (mode == ZOOM) {
                    // 两个手指滑动
                    float newDist = distance(event);
                    if (newDist > 10f) {
                        matrix.set(savematrix);
                        float scale = newDist / oriDis;
                        //图片的缩放
                        matrix.postScale(scale, scale, midPoint.x, midPoint.y);
                    }
                }
                    break;
        }
        img.setImageMatrix(matrix);
        invalidateOptionsMenu();
        return true;
    }
    // 计算两个触摸点之间的距离
    private float distance(MotionEvent event) {
        //获取第0跟手指和第一根手指的坐标
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

    // 计算两个触摸点的中点
    private PointF middle(MotionEvent event) {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        return new PointF(x / 2, y / 2);
    }
}

你可能感兴趣的:(多指触摸事件)