Android Canvas and Hardware Acceleration?

Hi everyone, I was wondering if it is possible to draw Canvas / Bitmap on the screen and use hardware acceleration or do I need to draw inside the onDraw() View method

For example, I draw on a screen bitmap by doing the following:

 Bitmap.Config config = Bitmap.Config.ARGB_8888; Bitmap buffer = Bitmap.createBitmap(200, 200, config); Canvas canvas = new Canvas(buffer); Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawLine(0, 0, 100, 100, paint); 

However, canvas.isHardwareAccelerated() returns false, and the drawing is sluggish compared to:

 protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawLine(0, 0, 100, 100, paint); } 

where canvas.isHardwareAccelerated() returns true. Is there a way to draw in Bitmap, taking advantage of hardware acceleration? Or do I need to draw directly on the screen in the onDraw method?

Thanks for the help :) I know that in Java I can draw BufferedImage on the screen, and this will be hardware acceleration, but maybe this is not the same on the phone ...

+4
source share
1 answer

Since in the first case you create a canvas, it is not supported by the hardware layer. In addition, at the moment you can’t include HWA only on any canvas, it must belong to the HWA view.

On the other hand, it has access to hardware capabilities. You can use this by calling setLayerType( LAYER_TYPE_HARDWARE) , as described in the docs:

Indicates that the view is hardware level. The hardware level supports hardware-specific textures (typically frame buffer or FBO objects on OpenGL hardware) and forces you to render a view using the Android hardware rendering pipeline, but only if hardware acceleration is enabled for the view hierarchy. When hardware acceleration is turned off, hardware levels behave exactly like software layers.

In addition, the performance issue relates more to the rendering part and less to the drawing. All drawing operations are recorded as a Picture object. Its a rendering operation in which HWA plays an important role.

+1
source

All Articles