Android 橫向和豎向scroll的兩種解決方案

來源:互聯網
上載者:User

一種是使用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;              }        });

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.