开发者

How to fill parent when drawing on canvas

开发者 https://www.devze.com 2023-04-06 18:48 出处:网络
I have a custom view (android) which draws a custom progress bar. I want the progress bar to fill its parent view (width). The problem that I have is that I don\'t know how the custom view can get its

I have a custom view (android) which draws a custom progress bar. I want the progress bar to fill its parent view (width). The problem that I have is that I don't know how the custom view can get its real limits. it needs to know the width limits in order to display different colors in the right ratio. I've implemented onMeasure() in the following way:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    setMeasuredDimension(widthMeasureSpec, 15);
}

Then, in my onDraw(), when I call getWidth() I get: 1073742246. How can I kn开发者_JS百科ow the real parent bounds? obviously I did something wrong here... the view layout params are set to MATCH_PARENT/FILL_PARENT and I can draw on the entire area but I don’t know the real width in order to draw the bars in the right proportion.

Thanks,

Noam


widthMeasureSpec includes other information than just the width in pixels. It looks like onMeasure is trying to pass you 422px as width with the mode MeasureSpec.EXACTLY.

Use MeasureSpec.getSize(widthMeasureSpec); to get the actual width in pixels. For more details, please see http://developer.android.com/reference/android/view/View.MeasureSpec.html


This value contains the needed width, but it also contains MeasureSpec flags.

Use MeasureSpec.getSize() to strip the flags.

0

精彩评论

暂无评论...
验证码 换一张
取 消