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;  
            }
        });



原文地址:https://www.cnblogs.com/sipher/p/2583089.html