简体   繁体   中英

Does android canvas draw on a bitmap?

I am working with the canvas of a SurfaceView and don't quite understand how canvas handles its draw calls. What I think so far: Canvas is backed by a Bitmap so things I draw are directly saved as pixel values in that Bitmap.

What I then don't get is this: Canvas allows operations like scale, translation and "out of screen" drawing but all those things would require a dynamically sizeable Bitmap (because I can for example translate to wherever I want) so how is this possible? Also I noticed when scaling drawn things like lines and circles up, they do not get pixelated at all which is strange if they where written on a bitmap first and then scaled up considerably.

Right now I implemented scaling and translation by modifying the variables in the draw calls directly. Is it more performant/best practise to skip this and use canvas' scale and translate functions instead?

What would that mean for the image quality of non Bitmap shapes when I scale them up considerably?

Can I savely draw at coordinates way "out of screen" and translate them back without any problems?

You scale shapes as well using Matrix() eg

private fun scalePath(path: Path, scale: Float) {
    val scaleMatrix = Matrix()
    val rectF = RectF()
    path.computeBounds(, true)
    scaleMatrix.setScale(scale, scale, rectF.centerX(), rectF.centerY())
    path.transform(scaleMatrix)
}

Scale you path by giving any scalling point, maybe 1.25f and the shape will transform itself.

Does android Canvas draw on a bitmap?

The answer is yes and no.

It depends, it can be backed by a bitmap or it can be hardware rendered which might not use a bitmap and it can also use a PDF document as used by Android's PdfDocument class.

Android canvas uses the Skia graphics library to do it's drawing.

For "What would that mean for the image quality of non Bitmap shapes when I scale them up considerably?"

From memory if your Paint has anti alias on, then the quality is quite ok.

For "Can I safely draw at coordinates way "out of screen" and translate them back without any problems?"

Yes up to a limit, as there are limits on the size especially if using a bitmap backed canvas because bitmaps are memory hungry and the VM has a limited amount of memory it can use.

Obviously if using scale and translate with user input then you might need to handle adjusting user input coordinates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM