SurfaceView绘制性能 [英] SurfaceView draw performance

查看:136
本文介绍了SurfaceView绘制性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android SurfaceView编写测试应用程序。我想看看这是否是2D游戏开发的可行方法。目前性能还不太好。

I am writing a test application using Android SurfaceView. I want to see if it is a viable approach to 2D game development. At the moment performance is not too great.

似乎每15秒左右就会出现一些小小的抽动和减速。我在屏幕上只画了30个矩形。我希望它能够轻松做到这一点。

There seems to be tiny little jerks and slowdowns every 15 or so seconds. I am only drawing about 30 rectangles on screen. I would hope it would be able to do this with ease.

我决定分析性能。我每10帧记录一次平均绘制时间。我注意到的是,它的抽签时间很长,然后又跳了起来。我确定我的代码没有在游戏循环中分配内存,所以我认为这个问题不是GC。

I decided to profile performance. I log the average draw time every 10 frames. What I have noticed is that it goes periods with good draw times, and then it jumps up. I am certain my code does not allocate memory in the game loop, so I think this problem is not the GC.

我注意到的是性能下降了大约100框架中,在相当模糊的日志语句之后写。有没有人知道android正在打印什么。

Something I have noticed is that the performance drops about 100 frames in, write after a rather ambiguous log statement. Does anyone have any idea what might be happening with what android is printing.

每次我启动应用程序时,在有关删除文件的日志语句之后,绘制时间都会缩短。

Everytime I start app, the draw time drops after this log statement about removing file....


01-18 16:53:20.221 19296-19370 / com.example.scott.coloursquares
I / game_screen:开奖time = 14 01-18 16:53:20.589
19296-19370 / com.example.scott.coloursquares I / game_screen:抽奖时间=
18 01-18 16:53:20.919 19296-19370 / com.example.scott.coloursquares
I / game_screen:抽奖时间= 13 01-18 16:53:21.250
19296-19370 / com.example.scott.coloursquares I / game_screen:抽奖时间=
13 01-18 16:53:21.581 19296-19370 / com.example.scott.coloursquares
I / game_screen:抽奖时间= 13 01-18 16:53:21.911
19296-19370 /com.example.scott.coloursquares I / game_screen:抽奖时间=
13 01-18 16:53:22.241 19296-19370 / com.example.scott.coloursquares
I / game_screen:抽奖时间= 13 01-18 16:53:22.572
19296-19370 / com。 example.scott.coloursquares I / game_screen:抽奖时间=
13 01-18 16:53:22.906 19296-19370 / com.example.scott.coloursquares
I / game_screen:抽奖时间= 18 01- 18 16:53:23.277 552-650 /?
D / TaskPersister:removeObsoleteFile:删除file = 5725_task.xml 01-18
16:53:23.278 552-650 /? D / TaskPersister:removeObsoleteFile:
删除file = 5725_task_thumbnail.png 01-18 16:53:23.330
19296-19370 / com.example.scott.coloursquares I / game_screen:绘制时间=
32 01-18 16:53:23.680 19296-19370 / com.example.scott.coloursquares
I / game_screen:抽奖时间= 18 01-18 16:53:24.032
19296-19370 / com。 example.scott.coloursquares I / game_screen:抽奖时间=
20 01-18 16:53:24.399 19296-19370 / com.example.scott.coloursquares
I / game_screen:抽奖时间= 31 01- 18 16:53:24.777
19296-19370 / com.example.scott.coloursquares I / game_screen:抽奖时间=
26 01-18 16:53:25.148 19296-19370 / com.example.scott .coloursquares
I / game_screen:绘制时间= 23 01-18 16:53:25.473
19296-19370 / com.example.scott.coloursquares I / game_screen:绘制时间=
12 01 -18 16:53:25.804 19296-19370 / com.example.scott.coloursquares
I / game_screen:抽奖时间= 14 01-18 16:53:26.142
19296-19370 / com.example。 scott.coloursquares I / game_scre en:抽奖时间=
14 01-18 16:53:26.467 19296-19370 / com.example.scott.coloursquares
I / game_screen:抽奖时间= 14

01-18 16:53:20.221 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 14 01-18 16:53:20.589 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 18 01-18 16:53:20.919 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 13 01-18 16:53:21.250 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 13 01-18 16:53:21.581 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 13 01-18 16:53:21.911 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 13 01-18 16:53:22.241 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 13 01-18 16:53:22.572 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 13 01-18 16:53:22.906 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 18 01-18 16:53:23.277 552-650/? D/TaskPersister﹕ removeObsoleteFile: deleting file=5725_task.xml 01-18 16:53:23.278 552-650/? D/TaskPersister﹕ removeObsoleteFile: deleting file=5725_task_thumbnail.png 01-18 16:53:23.330 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 32 01-18 16:53:23.680 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 18 01-18 16:53:24.032 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 20 01-18 16:53:24.399 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 31 01-18 16:53:24.777 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 26 01-18 16:53:25.148 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 23 01-18 16:53:25.473 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 12 01-18 16:53:25.804 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 14 01-18 16:53:26.142 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 14 01-18 16:53:26.467 19296-19370/com.example.scott.coloursquares I/game_screen﹕ draw time = 14


推荐答案

SurfaceView有两个部分,即Surface和View。 View集成到所有其他UI元素中,并且可以与标准组件一起分层。您可以创建一个自定义视图,并使用画布在其上绘制。尽管所有并非全部,但在所有最新的设备上,这都将利用硬件加速。

SurfaceView has two parts, the Surface and the View. The View integrates into all the other UI elements, and can be layered with standard components. You can create a custom View and draw on it with a Canvas. On all recent devices, that will take advantage of hardware acceleration, though not all of the Canvas API is implemented.

Surface是一个完全独立的图层,在具有渲染的View元素的图层上方或下方合成。您可以使用Canvas在软件中进行绘制,也可以通过OpenGL ES在GPU上进行绘制。 GLSurfaceView是SurfaceView的包装,用于处理EGL设置和一些线程问题,但没有什么特别的。有关将GLES与普通的SurfaceView和TextureView一起使用的示例,请参见 Grafika ,它具有一个包含所有EGL的便捷库

The Surface is a completely independent layer that is composited above or below the layer that has the rendered View elements. You can draw on it in software, using Canvas, or with the GPU by using OpenGL ES. GLSurfaceView is a wrapper around SurfaceView that takes care of EGL setup and some threading issues, but there's nothing special about it. For examples of using GLES with plain SurfaceView and TextureView, see Grafika, which has a handy library with all the EGL stuff taken care of.

画布在Surface上的渲染不是硬件加速的。随着屏幕变大(逐像素),渲染变得越来越昂贵。 Nexus 4具有相当不错的CPU +总线速度与像素数之比。在Nexus 4上流畅播放的动画会在Nexus 10上稍显窒息。(尝试Grafika多表面测试,选择弹跳,在日志中查看帧速率。)

Canvas rendering onto a Surface is not hardware accelerated. As screens get larger (pixel-wise), rendering becomes increasingly expensive. The Nexus 4 has a pretty good ratio of CPU+bus speed to pixel count. Animation that would be smooth on the Nexus 4 would choke a bit on the Nexus 10. (Try the Grafika "multi-surface test", select "bounce", watch the frame rate in the log.)

要获得良好的性能并节省大量工作,可能值得花时间研究开源图形或游戏引擎。或从一个现有的小型项目开始,例如 Android Breakout

To get good performance, and save yourself a lot of work, it might be worth your while to investigate an open-source graphics or game engine. Or start with a small existing project like Android Breakout.

这篇关于SurfaceView绘制性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆