Prevent painting outside the canvas using SurfaceView

Is there a way to prevent parts of objects from drawing, such as bitmaps or paths, that go beyond the borders Canvasin SurfaceView?

When I gradually scale the object outside the size Canvas, especially with blurry paint, it all slows down until it is ready, and I have to wait a few seconds to return control - it does not respond. I scale the object by moving my finger across the screen; if I do it too fast and scale it, then it really slows down the drawing.

I didn’t have the same problem when using just a regular canvas View, so I don’t know what slows it down. As if SurfaceViewreacting too quickly, and then overloaded.

So, one idea to improve this is to prevent drawing outside the canvas, but I'm not sure that it SurfaceViewhas such clipping options.

+5
source share
3 answers

One solution may be to use one of the methods Canvas .drawBitmapthat takes the original argument Rect/ RectF, so that only the visible part is visualized Bitmap. I just did this in my own project to speed up performance SurfaceView, where I have several Bitmapsthat sometimes only partially display.

+1
source

SurfaceView , :

android:paddingLeft="20dp"
android:paddingTop="20dp"
android:paddingRight="20dp"
android:paddingBottom="20dp"

:

Padding depiction

, :

canvas.drawLine(1,1,canvas.getWidth()-1,1);  //Top border
canvas.drawLine(1,1,1,canvas.getHeight()-1);  //Left border
canvas.drawLine(1,canvas.getHeight()-1,canvas.getWidth()-1,canvas.getHeight()-1);  //Bottom border
canvas.drawLine(canvas.getWidth()-1,1,canvas.getWidth()-1,canvas.getHeight()-1);  //Right border
0

All Articles