Android 中高效的 2D 绘图

发布于 2024-09-13 12:35:06 字数 661 浏览 15 评论 0原文

我已经搜索了好几个小时,但未能找到针对我的问题的简洁明确的答案。我有一个应用程序,需要在屏幕上绘制一个运动场(包括所有球场线)。到目前为止,我已经扩展了 SurfaceView,并且几乎复制了 LunarLander 演示的其余部分。 应用程序将节距绘制到正确尺寸所需的所有数据都是从插座接收的,该插座也工作得很好。然而,目前在 onDraw() 函数中,我每帧绘制所有线条,这导致模拟器中的帧速率相当慢(例如〜10fps)。这是我的 onDraw() 函数:

@Override
public void onDraw(Canvas canvas) {
canvas.drawARGB(255,0,144,0);
canvas.drawLine(canvas, getFirstLine(), mPaint);
canvas.drawRect(canvas, getFirstRect(), mPaint);
canvas.drawRect(canvas, getSecondRect(), mPaint);
...
canvas.drawRect(canvas, getSecondRect(), mPaint);
drawAnimatedObjects();
}

然后我在这个背景上绘制圆圈和不同的位置。我的问题是如何提高效率?有没有一种方法可以在应用程序初始化时绘制线条,而不必每帧都重新绘制它们?

感谢您的任何帮助。

I have searched for quite a few hours and have not been able to find a concise definite andswer to my question. I have an application where I need to draw a sports field (including all pitch lines) to the screen. So far, I have extended the SurfaceView and pretty much copied the rest of the LunarLander demo as well.
All the data the application requires to draw the pitch to the correct dimensions is being received from a socket which works fine too. However, at the minute in the onDraw() function, I am drawing all lines each frame which is causing a fairly slow framerate in the emulator (e.g. ~10fps). Here is my onDraw() function:

@Override
public void onDraw(Canvas canvas) {
canvas.drawARGB(255,0,144,0);
canvas.drawLine(canvas, getFirstLine(), mPaint);
canvas.drawRect(canvas, getFirstRect(), mPaint);
canvas.drawRect(canvas, getSecondRect(), mPaint);
...
canvas.drawRect(canvas, getSecondRect(), mPaint);
drawAnimatedObjects();
}

I then draw circles and different positions over this background. My question is how do I make this more efficient? Is there a way that I can draw the lines at the application initialisation and not have to redraw them every frame?

Thanks for any help.

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

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

发布评论

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

评论(2

暮凉 2024-09-20 12:35:06

您绝对应该缓存任何在初始化时不会更改为位图的画布绘图,然后在 onDraw() 中绘制该位图。这将有助于渲染时间很多。像这样的东西:

Bitmap mField = null;

void init()
{
  mField = new Bitmap(...dimensions...);
  Canvas c = new Canvas(mField);
  c.drawRect(...);
  ...
}

void onDraw(Canvas c)
{
  c.drawBitmap(mField);
}

You should definitely be caching any canvas drawing which will not change to a bitmap at initialization time and then draw that bitmap in onDraw(). That will help render times a lot. Something like:

Bitmap mField = null;

void init()
{
  mField = new Bitmap(...dimensions...);
  Canvas c = new Canvas(mField);
  c.drawRect(...);
  ...
}

void onDraw(Canvas c)
{
  c.drawBitmap(mField);
}
〆一缕阳光ご 2024-09-20 12:35:06

您的运动场是静态的还是在屏幕上滚动?
如果它是静态的,您应该考虑构建一次并将其保存为图像,然后每次都会重新绘制。
另一件事:与现代设备相比,模拟器非常慢。您应该至少在 G1 或更高版本上检查您的性能,以验证真实性能。

Is your sports field static or does is scroll over the screen?
If it is static, you should consider to build it once and save it as an image which than will be redrawn each time.
Another thing: The emulator is very slow in comparison with modern devices. You should check your performance at least on a G1 or later to verify the real performance.

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