Android setX() and setY() behaving weird

后端 未结 6 1331
情歌与酒
情歌与酒 2021-01-17 23:10

I am trying to dynamically create and then move an image in an Android activity. However, the setX() and setY() methods seem to not work correctly. It correctly sets the pos

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-17 23:34

    From the docs, setTranslationX is:

    Sets the horizontal location of this view relative to its left position. This effectively positions the object post-layout, in addition to wherever the object's layout placed it.

    And setX is:

    Sets the visual x position of this view, in pixels. This is equivalent to setting the translationX property to be the difference between the x value passed in and the current left property.

    Thus you can think of setTranlsationX as a relative offset: move 3 pixels left of where you normally would be. And setX is a fixed position: move whatever you have to so that you end up drawing at coordinate X.

提交回复
热议问题