android - Draw object's trails on a SurfaceView -
i have simulate motion of objects, have created surfaceview on draw them dedicated thread. every loop call canvas.drawcolor() clean previous object's positions , draw new states. works fine , frame rate decent.
the problem is: if want draw trails of objects' trajectories? in case have memorize positions every object and, @ every loop, draw past positions hundreds of points. task keep frame rate lower , seems me absurd way redraw every time same points! there way keep points painted on canvas , not cancel them canvas.drawcolor() @ every loop (that necessary others tasks)?
sort of.
the surfaceview's surface uses multiple buffers. if it's double-buffered, , don't clear screen every frame, you'll have rendering odd-numbered frames in 1 buffer, , even-numbered frames in other. every time draw new frame, it'll flip other buffer, , half of positions disappear (looks vibrating).
you could, on each frame, draw each object @ current position , previous position. way both frames every object position.
the practical problem idea don't know how many buffers surface using. if it's triple-buffered (which possible) need draw current, previous, , previous-previous positions ensure each buffer had every position. higher numbers of buffers theoretically possible unlikely.
having said this, don't want pursue approach simple reason: when lock canvas, agreeing modify every pixel in dirty area. if don't, results unpredictable, , app break weirdly in future version of operating system.
the best way want draw onto off-screen bitmap , blit entire thing onto surface. it's huge waste @ first, since you're copying screen-sized bitmap couple of objects, shortly reduced draw calls start win.
create bitmap that's same size surface, create canvas using constructor takes bitmap. drawing through canvas. when want update screen, use drawbitmap()
method on surfaceview's canvas.
i recommend against using software scaling due performance cost -- make sure you're doing 1:1 copy. can use setfixedsize()
call on surfaceview surface make specific size if that's helpful -- devices larger pixel densities can improve frame rates , reduce battery usage (blog post here).
Comments
Post a Comment