Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent drawing beyond canvas with SurfaceView

Is there way to prevent drawing of parts of objects, like bitmaps or paths, which go beyond the borders of Canvas in SurfaceView?

When I gradually scale an object beyond the Canvas' size, especially with blurred paint, it all slows down to a stand still and I have to wait few seconds to get the control back -- it is not responsive. I scale an object by moving my finger over the screen; if I do it too fast and scale it up, then it really slows down drawing.

I did not have the same problem when using just ordinary View canvas, so don't know what is slowing it down. It's as though SurfaceView responds too fast and then gets congested.

So one idea to improve this, is to prevent drawing outside canvas, but not sure if SurfaceView has such clipping options.

like image 615
Lumis Avatar asked Nov 03 '22 02:11

Lumis


1 Answers

One solution perhaps is by using one of the Canvas .drawBitmap methods that accepts a source Rect / RectF argument, so that only the visible portion of the Bitmap is rendered. I've just done this on my own project to speed up SurfaceView performance where I have several Bitmaps that are sometimes only partially in view.

like image 53
Trevor Avatar answered Nov 15 '22 13:11

Trevor