Reputation: 17779
In Android, I am creating a custom layout. It contains a view that I need to position, rotate, and then measure precisely.
If I have a 10x10px view that I move 10px right, 10px down, and then rotate 45 degrees, how wide will the view measure itself and where precisely will it be positioned?
view.setX(10)
view.setY(10)
view.setRotation(45)
view.measure(widthSpec, heightSpec)
Will it still report 10px as its width, or will it take its rotation into account? Are the x and y coordinates aligned with the rotated corner, or do the remain fixed?
Upvotes: 3
Views: 722
Reputation: 20944
rotation
, translationX
, translationY
, translationZ
, scaleX
, scaleY
- those are Canvas attributes. It means that you tell Android where to draw view after it has been measured and laid out relative to view's x
and y
position. Those attributes do not affect default measuring routine (unless you override onMeasure
in your custom view and take those into account, but I highly don't recommend doing that). So changing any of those attributes will not affect view measurments
x
, y
- are layout attributes. It means that parent of this view decides where to position it relative to itself, so if you set those values before onLayout
is called - these values will be overridden by parent. If you override x
and y
after onLayout
- you can change position of this view within its parent. But if you go after this effect - I would rather use translationX
and translationY
attributes since the very next onLayout
call will reset x
and y
again.
But to answer your original question - yes, it will report 10px
as its width. None of the mentioned above attributes will actually affect reported width or height of the view
EDIT As Dave pointed out, my original statement regarding x
and y
are being overridden during layout is not really correct. Setting x
and y
attributes will internally set translationX
and translationY
, so updating these attributes is effectively the same as updating translation
Upvotes: 3