我正在使用 Android 2.1 和 Eclipse。
我定义了一个具有两个尺寸的组件的 LinearLayout:250 x 250。
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams llp =
new llp.LayoutParams(250, 250);
meter1View = new MeterView(this, "Meter 1");
ll.addView(meter1View, llp);
meter2View = new MeterView(this, "Meter 2");
ll.addView(meter2View, llp);
ScrollView scrollView = new ScrollView(this);
scrollView.addView(ll);
setContentView(scrollView);
到目前为止,一切都很好。我在模拟器屏幕上看到两个垂直组件。
但是,在我的 Meter View 类中,onDraw 中画布的大小是我的模拟器屏幕的大小,即 480 x 800。
@Override
protected void onDraw(Canvas canvas) {
int w = canvas.getWidth();
int h = canvas.getHeight();
Log.i(TAG, " onDraw, w: " + w + ", h: " + h);
}
我希望画布大小为 250 x 250,以便我可以将此画布传递给绘图例程。现在,我的绘图例程在我的 LinearLayout 定义的 250 x 250 位图之外绘图。
我意识到我可以通过翻译画布来使我的画布绘图例程工作,但如果我可以暂时用新画布覆盖原始画布会更干净。
我知道我可以通过以下方式定义自己的画布:
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
如何用我的新画布替换 View 类创建的画布?任何帮助将不胜感激。
查尔斯