Drag an image only horizontally or vertically in android?

こ雲淡風輕ζ 提交于 2019-11-28 13:37:42

问题


I want to control the dragging way of the image in android. It should be moved only in one direction. How can i control it?


回答1:


First, put a ScrollView inside a HorizontalScrollView, and the ImageView inside the innermost ScrollView.

Hierarchy will look like this:

  LinearLayout (ROOT)
  |
  +-- ScrollView (V)
       |
       +-- LinearLayout (1)
            |
            +-- HorizontalScrollView (H)
                |
                +-- LinearLayout (2)
                    |
                    +-- ImageView

And let me explain:

(V) scrolls in vertical direction only.

(H) scrolls in horizontal direction only.

(1) and (2) are linear layouts used immediately following any ScrollViews. Because, a ScrollView can host only one direct child.

Note on naming: There's only "ScrollView", and not "VerticalScrollView"

What happens when you drag the ImageView ?

Based on the very first direction of the gesture you make, if it's horizontal, the HorizontalScrollView will process the action, and the ScrollView will sit idle. And if the action was vertical, the ScrollView will process the action, and HorizontalScrollView will sit idle.



来源:https://stackoverflow.com/questions/16266785/drag-an-image-only-horizontally-or-vertically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!