Android: Drag ImageView Off Screen

后端 未结 1 973
陌清茗
陌清茗 2021-01-21 19:37

I am creating an Android application that programmatically creates an ImageView, to be dragged and placed anywhere on screen. I can drag the view along the screen, but when it i

相关标签:
1条回答
  • 2021-01-21 20:05

    The view shrinks because at some point you change the view's width/height, i.e. say getRight() - getLeft() is forced to decrease because there is not enough space on the screen.

    Anyway, setting view's coordinates directly is not something you should do. Instead implement your custom ViewGroup/layout:

    • Override onLayout()
    • Iterate through children: use your custom logic to determine top and left coordinate for each child. In your case it would be using touch events to calculate the coordinates during dragging. For the right and bottom coordinates you can add the measured width/height of the child.
    • On each child call child.layout(left, top, right, bottom)

    For an example see the code of FrameLayout at line 513 and the docs for onLayout().

    0 讨论(0)
提交回复
热议问题