onMeasure():wrap_content,我怎么知道要包装的大小? [英] onMeasure(): wrap_content, how do I know the size to wrap?

查看:93
本文介绍了onMeasure():wrap_content,我怎么知道要包装的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用覆盖了 onDraw()的自定义视图在画布上绘制了位图。当我指定我希望它在布局文件中wrap_content时,它仍然会填满整个屏幕。 onMeasure()表示:

I have made a custom View with onDraw() overridden that draws a bitmap on the canvas. When I specify that I want it wrap_content in the layout file it still fills up the entire screen. onMeasure() says this:


measure的基类实现默认为背景大小,除非 MeasureSpec 允许更大的大小。子类应该重写onMeasure(int,int)以提供对其内容更好的度量。

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

好的,所以我知道我需要重写 onMeasure()并与 MeasureSpec 一起使用。根据此答案

Ok cool so I know I need to override onMeasure() and work with MeasureSpec. According to this answer


UNSPECIFIED表示将layout_width或layout_height值设置为wrap_content。您可以随心所欲。

UNSPECIFIED means the layout_width or layout_height value was set to wrap_content. You can be whatever size you would like.

现在我要解决我的问题,如何在 onMeasure ()测量尚未创建的位图,然后测量/包装?我知道其他Android视图必须做些什么,因为如果设置为wrap_content,它们不会阻塞整个屏幕。

Now I get to my problem, how do I at onMeasure() measure my bitmap that is not created yet and measure/wrap it? I know the other Android views MUST do something because they do not block out the entire screen if set to wrap_content. Thanks in advance!

推荐答案

如果在onMeasure调用之前无法测量位图,则可以返回大小为直到位图被加载为零。加载后,使父级ViewGroup无效以强制执行另一种措施(不记得View上的invalidate()是否会强制进行onMeasure。)

If you can't measure the bitmap prior to the onMeasure call, then you could return a size of zero until the Bitmap is loaded. Once it is loaded, invalidate the parent ViewGroup to force another measure (can't remember if invalidate() on the View itself will force an onMeasure).

这篇关于onMeasure():wrap_content,我怎么知道要包装的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆