gpt4 book ai didi

android - 如何覆盖 View 中的 Canvas ?

转载 作者:行者123 更新时间:2023-11-29 22:34:30 25 4
gpt4 key购买 nike

我正在使用 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 中 Canvas 的大小是我的模拟器屏幕的大小,即 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);
}

我希望 Canvas 大小为 250 x 250,以便我可以将此 Canvas 传递给绘图例程。现在,我的绘图例程正在我的 LinearLayout 定义的 250 x 250 位图之外绘制。

我意识到我可以通过翻译 Canvas 使我的 Canvas 绘图例程正常工作,但如果我可以用新 Canvas 暂时覆盖原始 Canvas ,它会更干净。

我知道我可以通过以下方式定义自己的 Canvas :

Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);

如何用我的新 Canvas 替换 View 类创建的 Canvas ?任何帮助将不胜感激。

查尔斯

最佳答案

您应该使用 View 的宽度和高度,而不是 Canvas ,两者都可以通过 getWidth() 和 getHeight() 获得。然后将这些尺寸传递给您的绘图例程。

关于android - 如何覆盖 View 中的 Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2420330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com