在硬件加速设置为 false 的自定义视图中获取 canvas 大小

Get canvas size in custom view with hardwareAcceleration set to false

当我在 AndroidManifest.xmlhardwareAccelerated="false" 中将硬件加速设置为 false 时,方法 canvas.getWidth()canvas.getHeight() 回落到一个奇怪的行为:

@Override
protected void onDraw(Canvas canvas) {
    int h = canvas.getHeight();
    int w = canvas.getWidth();
}

hardwareAccelerationfalse 时,如何获取视图的尺寸?

How can I get the dimensions of my view when hardwareAcceleration is false?

Canvas尺寸与View尺寸无关。如果您想获得 onDraw() 内视图的大小,您可以使用 getMeasuredWidth()/getMeasuredHeight().