我有一个扩展我绘制的 View 的类。
尽管视图被设置为具有特定尺寸的 RelativeLayout 的子视图,但视图的画布报告它是 1280 宽(与设备一样宽)。
我试图了解我需要做什么才能使画布受到其视图尺寸的约束(正如常识所决定的那样)。
好奇我忽略了什么......这里是相关的代码片段......
dv = new DrawView(this, getWindowManager());
dv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//this wrapper is 500x500
((RelativeLayout)findViewById(R.id.drawWrapper)).addView(dv);
L.log("DRAWVIEW WIDTH = " + dv.getWidth()); //this reports 0... why?
在 DrawView 内...
@Override
public void onDraw(Canvas canvas) {
L.log("CANVAS WIDTH ON DRAW = " + canvas.getWidth()); //this always reports 1280 on xoom
this.canvas = canvas;
drawElementsOntoCanvas(canvas, true, false);
...
}
有没有我忽略的东西,或者视图中的画布总是整个屏幕的宽度和高度?
相对布局的 XML:
<RelativeLayout
android:id="@+id/drawWrapper"
android:layout_width="500px"
android:layout_height="500px"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dip"
android:clipChildren="true"
android:background="@drawable/cut_out_frame"
/>