A display list records a series of graphics related operations and can replay them later.
display list,记录了一系列和图形有关的操作,并在稍后重播它们。
Display lists are usually built by recording operations on a DisplayListCanvas.
display list的建立通常用的是一个DisplayListCanvas来记录相关操作。
Replaying the operations from a display list avoids executing application code on every frame, and is thus much more efficient.
从显示列表重放操作避免了在每一帧上执行应用程序代码,因此效率更高。
Display lists are used internally for all views by default, and are not typically used directly.
默认情况下,显示列表在内部用于所有视图,通常不直接使用。
One reason to consider using a display is a custom View implementation that needs to issue a large number of drawing commands.
考虑使用显示的一个原因是需要发出大量绘图命令的自定义视图实现。
When the view invalidates, all the drawing commands must be reissued,
当View重绘时,所有的drawing命令都需要重新发出。
even if large portions of the drawing command stream stay the same frame to frame, which can become a performance bottleneck.
即使绘图命令流的大部分保持相同的帧到帧,这也可能成为性能瓶颈。
To solve this issue, a custom View might split its content into several display lists.
为了解决这个问题,自定义视图可能会将其内容拆分为多个显示列表。
A display list is updated only when its content, and only its content, needs to be updated.
显示列表仅在其内容且仅其内容需要更新时才更新。
A text editor might for instance store each paragraph into its own display list.
例如,文本编辑器可以将每个段落存储到它自己的显示列表中。
Thus when the user inserts or removes characters, only the display list of the affected paragraph needs to be recorded again.
这样当用户插入或删除字符时,只需重新记录受影响段落的显示列表即可。
Hardware acceleration
Display lists can only be replayed using a DisplayListCanvas.They are not supported in software.
Display lists 只能用DisplayListCanvas代替。在软件中不收支持。
Always make sure that the android.graphics.Canvas you are using to render a display list is hardware accelerated using android.graphics.Canvas.isHardwareAccelerated().
始终确保您用于呈现显示列表的 android.graphics.Canvas 使用 android.graphics.Canvas.isHardwareAccelerated() 进行了硬件加速。
Creating a display list
ThreadedRenderer renderer = myView.getThreadedRenderer();
if (renderer != null) {
DisplayList displayList = renderer.createDisplayList();
DisplayListCanvas canvas = displayList.start(width, height);
try {
// Draw onto the canvas
// For instance: canvas.drawBitmap(...);
} finally {
displayList.end();
}
}
Rendering a display list on a View
protected void onDraw(Canvas canvas) {
if (canvas.isHardwareAccelerated()) {
DisplayListCanvas displayListCanvas = (DisplayListCanvas) canvas;
displayListCanvas.drawDisplayList(mDisplayList);
}
}
Releasing resources
This step is not mandatory but recommended if you want to release resources held by a display list as soon as possible.
// Mark this display list invalid, it cannot be used for drawing anymore,
// and release resources held by this display list
displayList.clear();
Properties
In addition, a display list offers several properties, such as setScaleX(float) or setLeft(int), that can be used to affect all the drawing commands recorded within. For instance, these properties can be used to move around a large number of images without re-issuing all the individual drawBitmap() calls.
private void createDisplayList() {
mDisplayList = DisplayList.create("MyDisplayList");
DisplayListCanvas canvas = mDisplayList.start(width, height);
try {
for (Bitmap b : mBitmaps) {
canvas.drawBitmap(b, 0.0f, 0.0f, null);
canvas.translate(0.0f, b.getHeight());
}
} finally {
displayList.end();
}
}
protected void onDraw(Canvas canvas) {
if (canvas.isHardwareAccelerated()) {
DisplayListCanvas displayListCanvas = (DisplayListCanvas) canvas;
displayListCanvas.drawDisplayList(mDisplayList);
}
}
private void moveContentBy(int x) {
// This will move all the bitmaps recorded inside the display list
// by x pixels to the right and redraw this view. All the commands
// recorded in createDisplayList() won't be re-issued, only onDraw()
// will be invoked and will execute very quickly
mDisplayList.offsetLeftAndRight(x);
invalidate();
}
Threading
Display lists must be created on and manipulated from the UI thread only.
网友评论