如何覆盖视图中的画布?

发布于 2024-08-24 06:10:35 字数 1170 浏览 4 评论 0原文

我正在使用 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 类创建的画布? 任何帮助将不胜感激。

查尔斯

I am using Android 2.1 and Eclipse.

I have defined a LinearLayout with two components that have dimensions: 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);

So far, so good. I see two vertical compoments on my emulator screen.

However, in my Meter View class, the size of the canvas in onDraw is the size of my emulator screen, which is 480 by 800.

@Override
protected void onDraw(Canvas canvas) {
    int w = canvas.getWidth();
    int h = canvas.getHeight();
    Log.i(TAG, "  onDraw, w: " + w + ", h: " + h);
}

I would like a canvas size to be 250 x 250, so that I can pass this canvas to drawing routines. Right now, my drawing routines are drawing outside the 250 x 250 bitmap that was defined by my LinearLayout.

I realize that I can make my canvas drawing routine to work by translating the canvas, but it would be cleaner if I could temporarily override the original canvas with a new one.

I understand that I can define my own canvas by:

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

How can I replace the canvas that is created by the View class with my new canvas?
Any help will be greatly appreciated.

Charles

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沐歌 2024-08-31 06:10:35

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

you should use the width and height of the view, not the canvas, both available via getWidth() and getHeight(). Then pass these dimensions to your drawing routine.

吃不饱 2024-08-31 06:10:35

不要替换 Canvas,只需使用剪辑矩形即可。

Don't replace the Canvas, just use the clip rect instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文