package com.xzs.multouch_test1;import android.support.constraint.ConstraintLayout;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.widget.FrameLayout;import android.widget.ImageView;import android.widget.RelativeLayout;public class MainActivity extends AppCompatActivity {    private FrameLayout root;//activity_main中的主布局    private static final String TAG = "MainActivity";    private ImageView iv;//用来展示多点触控时 图片的放大缩小操作    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        root=(FrameLayout)findViewById(R.id.root);        iv=(ImageView) findViewById(R.id.iv);        root.setOnTouchListener(new View.OnTouchListener() {            float currentDistance;//用来保存当前两根手指的距离            float lastDistance=-1;//用来保存最后一次保存的两根手指的距离            @Override            public boolean onTouch(View view, MotionEvent motionEvent) {                switch(motionEvent.getAction()){                    case MotionEvent.ACTION_DOWN://当手指放下的时候触发                        //Log.d(TAG, "onTouch: +ACTION_DOWN");                        break;                    case MotionEvent.ACTION_MOVE://当手指滑动的时候触发                        //Log.d(TAG, "onTouch: +ACTION_MOVE");                        if(motionEvent.getPointerCount()>=2){//当为多点触控的时候,计算两根手指的距离并进行图片的放大缩小操作                            float offsetX=motionEvent.getX(0)-motionEvent.getX(1);                            float offsetY=motionEvent.getY(0)-motionEvent.getY(1);                            currentDistance=(float) Math.sqrt(offsetX*offsetX+offsetY*offsetY);                            if(lastDistance<0){                                lastDistance=currentDistance;                            }else{                                if(currentDistance-lastDistance>5){//为什么为5?因为要允许两根手指放上去时候的误差,不然两根手指一放上去照片就直接放大缩小放大缩小了。                                    Log.d(TAG, "放大 ");                                    FrameLayout.LayoutParams lp=(FrameLayout.LayoutParams)iv.getLayoutParams();                                    lp.width=(int)(1.1f*iv.getWidth());                                    lp.height=(int)(1.1f*iv.getHeight());                                    iv.setLayoutParams(lp);                                    lastDistance=currentDistance;                                }else if(lastDistance-currentDistance>5){                                    Log.d(TAG, "缩小");                                    FrameLayout.LayoutParams lp=(FrameLayout.LayoutParams)iv.getLayoutParams();                                    lp.width=(int)(0.9f*iv.getWidth());                                    lp.height=(int)(0.9f*iv.getHeight());                                    iv.setLayoutParams(lp);                                    lastDistance=currentDistance;                                }                            }                        }                        //Log.d(TAG, "point count: "+motionEvent.getPointerCount());                        //如果没有两个手指来触摸,就会报错。数组越界                        //Log.d(TAG, String.format("x1:%f y1:%f x2:%f y2:%f",motionEvent.getX(0),motionEvent.getY(0),motionEvent.getX(1),motionEvent.getY(1)));//                        FrameLayout.LayoutParams lp=(FrameLayout.LayoutParams) iv.getLayoutParams();//拖动图片//                        lp.leftMargin=(int)motionEvent.getX();//                        lp.topMargin=(int)motionEvent.getY();//                        iv.setLayoutParams(lp);                        //Log.d(TAG, (String.format("x:%f,y:%f" , motionEvent.getX(),motionEvent.getY())));                        break;                    case MotionEvent.ACTION_UP://当手指抬起来的时候触发                        //Log.d(TAG, "onTouch: +ACTION_UP");                        break;                }                return true;//返回True可以一直监控,返回False只能监控到第一个动作(即按下动作)            }        });    }}

更多相关文章

  1. C#如何计算传入的时间距离今天的时间差的实例分享
  2. C#通过KD树进行距离最近点的查找的实例分析
  3. React 教程:React 快速上手指南 [每日前端夜话0x2B]
  4. 盘一盘机器学习中的那些距离
  5. PHP实例计算经纬坐标间的距离
  6. jquery判断滚动条距离顶部的距离
  7. jquery手指触摸滑动放大图片的方法(比较靠谱的方法)
  8. Apache 2.2.19 + PHP5.2.17配置问题,请高手指点
  9. [LeetCode] 244. Shortest Word Distance II 最短单词距离 II

随机推荐

  1. Android:相对位置排版
  2. android系统自带样式Android:theme
  3. Android(安卓)SDK Android(安卓)NDK Andr
  4. Android系统自带样式Android:theme
  5. Android系统自带样式(android:theme)
  6. android 系统自带的图标
  7. Android(安卓)Theme
  8. android中xml文件注意事项
  9. android:theme & android:style
  10. Android绘图之Path(3)