zoukankan      html  css  js  c++  java
  • Android 横向和竖向scroll的两种解决方法

    一种是使用ScrollView和HorizontalScrollView结合。
         这种方式的话,斜向滑动会有先后次序,一般将ScrollView放在外层
    第二种,使用onTouchEvent方法,监听移动事件,进行处理。
         如果只是监听移动事件,图片可以移出指定区域之外,可以通过控制边界来进行限制在一定范围内移动。
         简单解决方案代码如下:

         container.setOnTouchListener(new OnTouchListener() {
     
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN: {
                            currentX = (int) event.getRawX();
                            currentY = (int) event.getRawY();
                            break;
                        }
    
                        case MotionEvent.ACTION_MOVE: {
                            int x2 = (int) event.getRawX();
                            int y2 = (int) event.getRawY();
                            // 限制移动的边界========================
                            int[] imageLocation = new int[2];
                            int[] containerLocation = new int[2];
                            // 取得 图片和Layout的坐标
                            image.getLocationOnScreen(imageLocation);
                            container.getLocationOnScreen(containerLocation);
                            // 向右 currentX - x2 < 0
                            // 向左 currentX - x2 > 0
                            // 向上 currentY - y2 > 0
                            // 向下 currentY - y2 < 0
                            if (currentX - x2 > 0) {
                                //当向左移动时
                                if (imageLocation[0] +image.getRight() > containerLocation[0] + container.getRight()) {
                                    container.scrollBy(currentX - x2 , 0);
                                } else {
                                    container.scrollTo(image.getWidth() - container.getWidth(), containerLocation[1] - imageLocation[1]); // y2不正确
                                }
                            } else {
                                Log.e("scroll path", "向右移动");
                                if (imageLocation[0] < containerLocation[0]) {
                                    container.scrollBy(currentX - x2 , 0);
                                } else {
                                    container.scrollTo(0, containerLocation[1] - imageLocation[1]);   
                                }
                            }
                            if (currentY - y2 > 0) {
                                Log.e("scroll path", "向上移动");
                                if (image.getBottom() + imageLocation[1] > containerLocation[1] +container.getBottom()) {
                                    container.scrollBy(0 , currentY - y2);
                                } else {
                                    container.scrollTo(containerLocation[0] - imageLocation[0], image.getHeight() - container.getWidth());
                                }
                            } else {
                                Log.e("scroll path", "向下移动");
                                if (imageLocation[1] < containerLocation[1]) {
                                    container.scrollBy(0 , currentY - y2);
                                } else {
                                    container.scrollTo(containerLocation[0] - imageLocation[0], 0);
                                }
                            }
                            // 限制移动的边界========================
                            // 无限制移动的边界========================                        
                            //container.scrollBy(currentX - x2 , currentY - y2);
                            // 无限制移动的边界========================                        
                            currentX = x2;
                            currentY = y2;
                            break;
                        }    
                        case MotionEvent.ACTION_UP: {
                            break;
                        }
                    }
                      return true;  
                }
            });



  • 相关阅读:
    java后台生成图片二维码
    layui框架下的摸索与学习
    eclipse/myeclipse中js/java的自动提示只有4个字符怎么解决
    Git日常操作指令
    node指南开发练习笔记(1)-- express
    echart全国主要城市某数据的显示
    微信公众号开发获取当前位置
    显示上传图片
    移动端Safari onclick事件兼容
    Plupload上传插件自定义图片的修改
  • 原文地址:https://www.cnblogs.com/sipher/p/2583089.html
Copyright © 2011-2022 走看看