Using Hardware Layer in Custom View onDraw

后端 未结 2 1480
礼貌的吻别
礼貌的吻别 2021-02-15 12:19

So I\'m trying to understand how I can properly use hardware acceleration (when available) in a custom View that is persistently animating. This is the basic premis

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-15 13:21

    On what view(s) are you setting View.LAYER_TYPE_HARDWARE exactly? If you are setting a hardware layer on the view that contains the drawing code shown above, you are causing the system to do a lot more work than necessary. Since you are only drawing bitmaps you don't need to do anything here. If you call Canvas.drawBitmap() the framework will cache the resulting OpenGL texture on your behalf.

    You could however optimize your code a little more. Instead of calling drawBitmap(), you could use child views. If you move these children using the offset*() methods (or setX()/setY()) the framework will apply further optimizations to avoid calling the draw() methods again.

    In general, hardware layers should be set on views that are expensive to draw and whose content won't change often (so pretty much the opposite of what you're doing :)

提交回复
热议问题