Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onMeasure will not return heights #5

Open
lowenbjer opened this issue Mar 13, 2013 · 1 comment
Open

onMeasure will not return heights #5

lowenbjer opened this issue Mar 13, 2013 · 1 comment

Comments

@lowenbjer
Copy link

getHeight() and getWidth does not return height and width of view if used in onMeasure; they return 0.

Height and width are assigned in the drawing phase which, in essence occurs way after onMeasure is called.

There is a simple fix for this:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = View.MeasureSpec.getSize(widthMeasureSpec);
height = View.MeasureSpec.getSize(heightMeasureSpec);

    int size = (width > height) ? height : width; // Choose the smaller
    // between width and
    // height to make a
    // square

    cx = width / 2; // Center X for circle
    cy = height / 2; // Center Y for circle
    outerRadius = size / 2; // Radius of the outer circle

    innerRadius = outerRadius - barWidth; // Radius of the inner circle

    left = cx - outerRadius; // Calculate left bound of our rect
    right = cx + outerRadius;// Calculate right bound of our rect
    top = cy - outerRadius;// Calculate top bound of our rect
    bottom = cy + outerRadius;// Calculate bottom bound of our rect

    startPointX = cx; // 12 O'clock X coordinate
    startPointY = cy - outerRadius;// 12 O'clock Y coordinate
    markPointX = startPointX;// Initial locatino of the marker X coordinate
    markPointY = startPointY;// Initial locatino of the marker Y coordinate

    rect.set(left, top, right, bottom); // assign size to rect
}

This will work in any layout, activity or fragment.

Best wishes, and great code. +1

@melanke
Copy link

melanke commented Oct 4, 2013

I am using onLayout instead of onMeasure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants