AndroidCircularSeekBar icon indicating copy to clipboard operation
AndroidCircularSeekBar copied to clipboard

onMeasure will not return heights

Open lowenbjer opened this issue 12 years ago • 1 comments

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

lowenbjer avatar Mar 13 '13 14:03 lowenbjer

I am using onLayout instead of onMeasure

melanke avatar Oct 04 '13 14:10 melanke