使用预定义的“路径”优化“Canvas.drawCircle”

Optimizing `Canvas.drawCircle` with predefined `Path`

我目前正在 android 学习动画,这是我在手册中读到的内容:

A common mistake is to create a new Paint or a new Path every time a rendering method is invoked. This forces the garbage collector to run more often and also bypasses caches and optimizations in the hardware pipeline.

Complex shapes, paths, and circles for instance, are rendered using texture masks. Every time you create or modify a path, the hardware pipeline creates a new mask, which can be expensive.

在我的应用程序中,我计划不断地画几十个圆圈,我想尽可能地优化这个过程。我有一个方法 Canvas.drawCircle(X, Y, RADIUS, Paint),我可以在其中提供预定义的 Paint 而不是预定义的 Path

问题是:如果我可以提供预定义的 PathPaint,这会提高渲染性能吗?如果是,我应该使用什么方法或技术来代替 drawCircle?

您在这里根本没有使用路径,所以不用担心预定义的路径。 您不需要优化您的代码,除非您发现它变慢了,但事实并非如此。 一种可能的优化是,如果您多次绘制相同半径和相同油漆(但在不同位置)的圆,将其渲染一次到位图中,然后在每个位置绘制该位图。如果这样做,请不要在 onDraw 方法中准备位图,仅在必须更改时才准备。