布局的监听事件setOnTouchListener

布局的监听事件重写方法:java

        layout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

 得到布局的监听事件,注意返回值改成true,不然在执行了DOWN之后再也不执行UP和MOVE操做。ide

            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_MOVE
                        break;
                }
                return true;
            }

 

得到在屏幕上点击数量(即手指数量)的函数,每个手机能够得到的最大数量不等函数

 event.getPointerCount()

 

能够经过move对图片的大小进行操做布局

    case MotionEvent.ACTION_MOVE:
             if (event.getPointerCount() >= 2) {
                     float offsetX = event.getX(0) - event.getX(1);//监听手机的PointerCount数量大于2时,得到两个点的坐标
                     float offsetY = event.getY(0) - event.getY(1);
                     currentdistance = (float) Math.sqrt(offsetX * offsetX + offsetY * offsetY);
                     if (lastdistance < 0) {
                           lastdistance = currentdistance;
                     } else {
                       if (currentdistance - lastdistance > 5) {//像素改变大于5是为了容错的存在
                           RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) img.getLayoutParams();
                            layoutParams.width = (int) (img.getWidth() * 1.1);
                            layoutParams.height = (int) (img.getHeight() * 1.1);
                            img.setLayoutParams(layoutParams);
                            lastdistance = currentdistance;
                         } else if (lastdistance - currentdistance > 5) {
                               RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) img.getLayoutParams();
                               layoutParams.width = (int) (img.getWidth() * 0.9);
                                 layoutParams.height = (int) (img.getHeight() * 0.9);
                                 img.setLayoutParams(layoutParams);
                                 lastdistance = currentdistance;
                            }
                      }
                 }
相关文章
相关标签/搜索