Android:lockCanvas()和lockCanvas(null)之间有什么区别?

时间:2012-09-06 06:03:02

标签: android surfaceview

标题说得很清楚,尽管我会给出一个代码示例以防万一。这来自Android SDK中的LunarLander示例:

    @Override
    public void run() {
        while (mRun) {
            Canvas c = null;
            try {
                c = mSurfaceHolder.lockCanvas(null);
                synchronized (mSurfaceHolder) {
                    if (mMode == STATE_RUNNING) updatePhysics();
                    doDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }

lockCanvas()的文档说明必须重新绘制所有像素,而lockCanvas(Rect dirty)表示您只需重新绘制dirty中的像素。我看到了将null传递给此函数的两种可能解释:它不需要重新绘制任何像素,或者行为方式与lockCanvas()相同。

1 个答案:

答案 0 :(得分:5)

它是开源的

public Canvas lockCanvas() {
  return internalLockCanvas(null);
}
public Canvas lockCanvas(Rect dirty) {
  return internalLockCanvas(dirty);
}