• 布局的监听事件setOnTouchListener


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

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

     获得布局的监听事件,注意返回值改为true,否则在执行了DOWN以后不再执行UP和MOVE操作。

                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;
                                }
                          }
                     }
    
  • 相关阅读:
    php第九节课
    php第八节课
    php第七节课
    php第六讲
    php第五节课
    php第四节课
    php第三节课
    Golang Slice切片
    Golang 反射
    Golang 流程控制
  • 原文地址:https://www.cnblogs.com/lyxin/p/5792614.html
Copyright © 2020-2023  润新知