• 布局的监听事件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;
                                }
                          }
                     }
    
  • 相关阅读:
    css盒子模型之内边距padding及简写
    css盒子模型之宽度和高度
    windows 组策略
    windows 快捷键
    cmd 命令快捷键
    django 远程访问
    django 部署在 apache2 上面
    国内常用开源镜像站
    ubuntu1804自带官方源
    ubuntu1604 apt华为国内源
  • 原文地址:https://www.cnblogs.com/lyxin/p/5792614.html
Copyright © 2020-2023  润新知