android canvas - 我可以有多种绘制方法吗?
我制作了自己的画布类,它扩展了 imageView。我的 onDraw() 方法绘制出用户的 GPS 位置,并且每次用户移动时我都会调用此 onDraw 方法。我的问题是我还想绘制一条 GPS 轨迹,只需绘制一次(用户移动时不需要更新)。我想知道是否可以有超过 1 个 onDraw 方法,或者是否有任何方法可以分离 1)用户位置和 2)gps 轨迹?
我的原因是我不想每次用户 GPS 位置发生变化时都重新绘制 GPS 路线来浪费内存。这是一种浪费。
I have made my own canvas class which extends an imageView. My onDraw() method draws out the users gps position and I keep calling this onDraw method every time the user moves. My problem is I also want to draw out a gps trail which only needs to be drawn once (doesnt need to be updated when a user moves). I am wondering is it possible to have more than 1 onDraw method or is there any way of separating 1) the user location and 2) the gps trail??
My reason is I do not want to waste memory by redrawing the gps route everytime the users gps position changes. It is a waste.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否看到性能受到影响?如果没有,不用担心。我认为这会浪费 CPU 周期(如果有的话)……而不是内存。因此,如果应用程序看起来已经足够快,则不必担心优化它。
如果您的应用程序有点滞后,并且您发现路径是瓶颈...我建议将其缓存到位图中。这样,您仍然需要绘制轨迹,但不必计算每一帧上轨迹的坐标。
Have you seen performance take a hit? If not, don't worry about it. I would think that this would be wasting CPU cycles if anything... not memory. So if the app seems fast enough already, don't worry about optimizing it.
If your app is a bit laggy, and you've found that the trail is the bottleneck... I would suggest caching it into a bitmap. This way, you will still have to draw the trail, but you will not have to calculate the coordinates of the trail on each frame.
我最近不得不解决一个有点类似的问题,我将简要解释一下我所做的事情,以防有任何帮助。
你可以做的是使用多个重叠的View,其中一个可能包含你不希望经常重绘的背景图形,另一个前景View包含经常更新的图形。然后,为了获得性能,您可以设计背景视图的
onDraw()
,以便它由位图支持,然后将其保留为类变量。在背景图形的第一个onDraw()
中,您对 Canvas 进行相对较慢的绘制。在随后调用onDraw()
时,您只需将该位图绘制到 Canvas 即可。我自己刚刚做过这个。基本上我的应用程序所做的是显示许多图形仪表。这些仪表有很多只绘制一次的图形(仪表面、数字图例),并且随着模型数据的变化需要一遍又一遍地重新绘制指针图形。首先,我将背景图形和移动前景图形分割成单独的重叠视图。现在,使前景指针图形无效并重绘当然会导致其重叠的任何内容也无效,因此每次重绘指针 View 时都会调用背景图形 View 的 onDraw() 方法。背景View只需要绘制一次背景图形,但保留支持画布的Bitmap,并在后续的onDraw()调用中将此位图绘制回Canvas(这比最初快很多)使用 Path() 对象创建图形)。
I have had to solve a somewhat similar problem recently and I'll explain briefly what I did in case it's of any help.
What you can do is use multiple overlapping Views, where one may contain the background graphics that you don't want to redraw often, and a foreground View that contains the graphics that are frequently updated. Then, to gain performance, you can design the background View's
onDraw()
so that it is backed by a Bitmap that you then retain as a class variable. In the very firstonDraw()
of your background graphics, you do the relatively slow drawing to Canvas. In subsequent calls toonDraw()
, you simply draw that Bitmap to Canvas.I've just done this myself. Basically what my application does is display a number of graphical gauges. Those gauges have lots of graphics that are drawn just once (gauge face, number legends), and the pointer graphic that needs to be redrawn over and over as the model data changes. First of all, I split the background graphics and moving foreground graphics into separate overlapping Views. Now, invalidating and redrawing the foreground pointer graphic of course causes anything it overlaps to be invalidated too, so the
onDraw()
method for the background graphics View is being called each time the pointer View is redrawn. The background View only needs to draw the background graphics once, but retains the Bitmap that backs the canvas, and in subsequentonDraw()
calls it draws this bitmap back to Canvas (which is a lot faster than initially creating the graphics using Path() objects).好吧,假设我正确理解你的问题,onDraw 方法不能超过 1 个。您需要考虑如何处理此问题的替代方法。
@DeeV 建议,这可能是您的解决方案。
Well, there can't be more than 1 onDraw method, assuming that I understood your question correctly. You will need to think about alternate approaches about how to handle this.
@DeeV suggested, that can be a solution for you.