如何使用Android SurfaceView实现每秒30帧?

发布于 2024-10-15 06:56:25 字数 266 浏览 0 评论 0原文

我正在寻找示例代码示例,展示如何使用以下命令实现每秒 30 帧 Android SurfaceView?假设每一帧从位置 x 和 y 开始绘制一个圆,并且每一帧将 x 和 y 递增 1。

奖金问题: 在上面我想覆盖另一个视图来显示文本内容,以便正在绘制的任何图形都显示在该文本内容下方。如何做到这一点?

I am looking for example code sample that shows how to achieve 30 frames per seconds using
Android SurfaceView? Assume each frame draws a circle starting at position x and y and each frame increments x and y by one.

Bonus question:
On top of above I want to overlay another View to display text content so that whatever graphics is being drawn shows underneath this text content. How this can be done?

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

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

发布评论

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

评论(3

余厌 2024-10-22 06:56:25

如果您想要高帧速率,您确实需要阅读 GLSurfaceView 。在正常的表面视图下,您将受到昂贵的布局计算和以 CPU 为中心的渲染的影响。使用 GLSurfaceView,您可以将其卸载到 GPU,并对布局进行严格有效的控制。

If you want high frame rates, you really need to read up on GLSurfaceView. Under a normal surface view you are going to be at the mercy of the expense layout calculations and CPU centric rendering. With the GLSurfaceView you can offload that to the GPU and have a tight efficient control of layouts.

吾家有女初长成 2024-10-22 06:56:25

创建一个每秒执行此操作 30 次的线程:

  1. 调用 SurfaceHolder.lockCanvas()。
  2. 绘制到画布中(必须完全重绘所有像素)。
  3. 调用 SurfaceHolder.unlockCanvasAndPost()。

事实上,如果您的线程只是坐在那里执行此操作而不尝试暂停,它将被限制为屏幕的帧速率。

您需要确保该线程与表面视图的管理正确同步 - 例如,如果表面正在更改或被破坏,您的代码应与线程同步,以确保线程在发生这种情况时停止运行。

当然,这确实意味着您正在画布中进行软件渲染。根据您正在做的事情,这可能可以为您提供 30fps 的动画。如果没有,您将需要使用 GLSurfaceView。无论如何,您可能要考虑使用它,因为它会为您处理线程部分。

Make a thread that does this 30 times a second:

  1. Call SurfaceHolder.lockCanvas().
  2. Draws into the Canvas (must completely redraw all pixels).
  3. Call SurfaceHolder.unlockCanvasAndPost().

In fact if your thread just sits there doing that without trying to pause, it will be throttled to the frame rate of the screen.

You need to make sure that this thread is correctly synchronized with management of the surface view -- for example if the surface is changing or being destroyed, your code there should synchronize with the thread to make sure the thread stops running while this happens.

Of course this does mean that you are doing software rendering into the canvas. Depending on what you are doing, this may be fine to give you 30fps animation. If not, you'll need to use GLSurfaceView. You may want to consider using that anyway, just because it takes care of the threading part for you.

挽手叙旧 2024-10-22 06:56:25

这可能是一个旧线程,但我认为这可以帮助其他人。
这是我发现的关于使用 surfaceView 的一个很好的教程:
http://www.droidnova.com/playing -with-graphics-in-android-part-i,147.html

这是一篇关于如何实现游戏循环的好文章,它可以帮助将 FPS 限制为 30:
http://www.koonsolo.com/news/dewitters-gameloop/

您可以如果您愿意,可以直接将文本添加到 SurfaceView 中,
这是一个例子

public void onDraw(Canvas canvas) {
    ...
    // Add text
    Paint paint = new Paint(); 
    paint.setAntiAlias(true);
    paint.setFakeBoldText(true);               // if you like bold
    paint.setShadowLayer(5, 5, 5, Color.GRAY); // add shadow
    paint.setColor(Color.WHITE); 
    paint.setTextSize(30); 
    canvas.drawText("FPS: " + _fps, 10, 35, paint);             
}

This maybe an old thread but I think this can help others.
Here is a nice tutorial I found on using surfaceView:
http://www.droidnova.com/playing-with-graphics-in-android-part-i,147.html

And here is a nice article on how to implement game-loop which can help limiting the FPS to 30:
http://www.koonsolo.com/news/dewitters-gameloop/

You can add text directly to your surfaceView if you like,
Here is an example

public void onDraw(Canvas canvas) {
    ...
    // Add text
    Paint paint = new Paint(); 
    paint.setAntiAlias(true);
    paint.setFakeBoldText(true);               // if you like bold
    paint.setShadowLayer(5, 5, 5, Color.GRAY); // add shadow
    paint.setColor(Color.WHITE); 
    paint.setTextSize(30); 
    canvas.drawText("FPS: " + _fps, 10, 35, paint);             
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文