Lining up Views with coordinates for the origin, not the top-left corner
Is there an easy way to change the "anchor" point of a view from the
top-left to the origin? Right now, I am trying something that requires a
line to intersect the center of a ViewGroup that extends RelativeLayout.
So the line has coordinates [x0, y0] and so does the ViewGroup, but the
ViewGroup uses the coordinates as the top-left and leaves me with
something like this:
This is the line being added to the parent view:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
mapView.addView(vEdgeViews[i], 10000, 10000);
This is the rectangle view group being added to the parent view:
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
float centerX = vNodeGroup[j].getX();
float centerY = vNodeGroup[j].getY();
float w = (vNodeGroup[j].getLeft() - vNodeGroup[j].getRight())/2;
float h = (vNodeGroup[j].getTop() - vNodeGroup[j].getBottom())/2;
params.leftMargin = (int)(vNodeGroup[j].getX() - vNodeGroup[j].getWidth()/2);
params.topMargin = (int)(vNodeGroup[j].getY() - vNodeGroup[j].getHeight()/2);
mapView.addView(vNodeGroup[j], params);
The problem is vNodeGroup[j].getWidth()/2); and
vNodeGroup[j].getHeight()/2); are both returning zero. I think this may
have something to do with either onLayout() or onMeasure(), but I'm new to
this and don't really understand those two methods.
My ViewGroup extends RelativeLayout, so shouldn't that mean I don't have
to override onMeasure()? I just want to get the width of the layout (which
uses setBackgroundDrawable() to produce the rectangle.
One last thought: perhaps this has something to do with the width/height
are set to wrap_content and there don't have a width/height when I am
calling those methods?
What's the best way to solve this problem?
No comments:
Post a Comment