Android 什么时候可以调用getMeasuredWidth()

Android 什么时候可以调用getMeasuredWidth(),android,Android,我有一个自定义类,它继承了视图并重写了onDraw方法。在这个方法中,我使用getMeasuredWidth()作为参数创建了一个Bitmap实例 编译器不喜欢说在onDraw方法中创建对象不酷 所以我需要将位图实例创建移动到另一个方法。但我需要确保getMeasuredWidth()会返回最新信息 我的问题是:我应该使用什么方法来捕获更改的getMeasuredWidth()并重新创建我的Bitmap对象?getMeasuredWidth()返回测量值,如果您需要布局中视图的真实宽度,请使用g

我有一个自定义类,它继承了
视图
并重写了
onDraw
方法。在这个方法中,我使用
getMeasuredWidth()
作为参数创建了一个
Bitmap
实例

编译器不喜欢说在
onDraw
方法中创建对象不酷

所以我需要将
位图
实例创建移动到另一个方法。但我需要确保
getMeasuredWidth()
会返回最新信息

我的问题是:我应该使用什么方法来捕获更改的
getMeasuredWidth()
并重新创建我的
Bitmap
对象?

getMeasuredWidth()
返回测量值,如果您需要布局中视图的真实宽度,请使用
getWidth()

如果位图应具有视图的宽度或高度,请在
onSizeChanged()

/**
 * This is called during layout when the size of this view has changed. If
 * you were just added to the view hierarchy, you're called with the old
 * values of 0.
 *
 * @param w Current width of this view.
 * @param h Current height of this view.
 * @param oldw Old width of this view.
 * @param oldh Old height of this view.
 */
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    if (w != 0 && h != 0) {
        //create Bitmap here
    }
}