画上SurfaceView对象的小径 [英] Draw object's trails on a SurfaceView

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

问题描述

我要模拟一些物体的运动,所以我建立了我画他们有专门的线程一个SurfaceView。每一个循环我叫canvas.drawColor()清理所有previous对象的位置,并绘制新的状态。一切工作正常和帧速率是体面的。

I have to simulate the motion of some objects, so I have created a SurfaceView on which I draw them with a dedicated Thread. Every loop I call canvas.drawColor() to clean up all previous object's positions and to draw the new states. Everything works fine and the frame rate is decent.

问题是:如果我想要的绘制对象的轨迹的路径呢?在这种情况下,我不得不记住每个对象的位置,并在每个循环,绘制数百点的所有过去的立场。这个任务保持帧速率较低,在我看来荒谬的唯一方法是重绘每次同分!有一种方法来保持画布上的点,而不是与canvas.drawColor()在每个循环(所必需的其他任务)?

The problem is: what if I want to draw the trails of the objects' trajectories? In that case I have to memorize the positions for every object and, at every loop, draw all past positions that are hundreds of points. This task keep the frame rate lower and it seems to me absurd that the only way is to redraw every time the same points! There is a way to keep the points painted on the canvas and not to cancel them with the canvas.drawColor() at every loop (that is necessary for others tasks)?

推荐答案

排序。

该SurfaceView的表面采用多个缓冲区。如果是双缓冲,你不清除屏幕每帧,那么你就必须从一个缓冲区中的所有奇数帧,在所有其他偶数帧渲染。每次你画一个新的帧的时候,它会翻转到另一个缓冲区,你的职位将消失一半(貌似一切振动)。

The SurfaceView's Surface uses multiple buffers. If it's double-buffered, and you don't clear the screen every frame, then you'll have the rendering from all the odd-numbered frames in one buffer, and all the even-numbered frames in the other. Every time you draw a new frame, it'll flip to the other buffer, and half of your positions will disappear (looks like everything is vibrating).

您可以,每个框架上,在当前位置和previous位置绘制每个对象。这样两个框架将得到每一个对象的位置。

You could, on each frame, draw each object at its current position and its previous position. That way both frames would get every object position.

这一理念的实际问题是,你不知道表面上是多少缓​​冲区使用。如果是三重缓冲(这是很可能的),那么你就需要借助当前,previous和previous- previous位置,以确保每个缓冲区发生的每个位置。缓冲区的高数是理论上是可能的,但可能性不大。

The practical problem with this idea is that you don't know how many buffers the Surface is using. If it's triple-buffered (which is very possible) then you would need to draw the current, previous, and previous-previous positions to ensure that each buffer had every position. Higher numbers of buffers are theoretically possible but unlikely.

说了这么多,你不想去追求这种方法的原因很简单:当你锁定在画布上,即表示您同意修改每个像素中的脏区。如果不这样做,结果未predictable,和您的应用程序可以在操作系统的未来版本古怪打破。

Having said all this, you don't want to pursue this approach for a simple reason: when you lock the canvas, you are agreeing to modify every pixel in the dirty area. If you don't, the results are unpredictable, and your app could break weirdly in a future version of the operating system.

最好的办法做你想做的是画到离屏位图,然后的blit整个事情在表面上。这是第一次在一个巨大的浪费,因为你复制一个屏幕大小的位图只是一对夫妇的对象,但很快的缩小绘制调用将开始取胜。

The best way to do what you want is to draw onto an off-screen Bitmap and then blit the entire thing onto the Surface. It's a huge waste at first, since you're copying a screen-sized bitmap for just a couple of objects, but very shortly the reduced draw calls will start to win.

创建位图是这样的尺寸与表面一样的,然后使用创建一个画布的<一个href=\"http://developer.android.com/reference/android/graphics/Canvas.html#Canvas(android.graphics.Bitmap)\"相对=nofollow>构造函数位图。是否所有绘图通过这个画布。当你想更新屏幕,使用上SurfaceView的画布 drawBitmap()方法。

Create a Bitmap that's the same size as the Surface, then create a Canvas using the constructor that takes a Bitmap. Do all your drawing through this Canvas. When you want to update the screen, use a drawBitmap() method on the SurfaceView's Canvas.

我不推荐使用软件缩放由于性能成本 - 确保你正在做一个1:1拷贝。您可以使用 setFixedSize() 的SurfaceView表面上呼吁,使其特定大小,如果这是有帮助的 - 它可以更大的像素密度的设备改善你的帧速率和降低电池用量(博客文章这里)。

I recommend against using software scaling due to the performance cost -- make sure you're doing a 1:1 copy. You can use the setFixedSize() call on the SurfaceView surface to make it a specific size if that's helpful -- for devices with larger pixel densities it can improve your frame rates and reduce battery usage (blog post here).

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

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