2

标题说得很清楚,尽管我会给出一个代码示例以防万一。这是来自 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().

4

1 回答 1

5

它是开源的

public Canvas lockCanvas() {
  return internalLockCanvas(null);
}
public Canvas lockCanvas(Rect dirty) {
  return internalLockCanvas(dirty);
}
于 2012-09-06T06:14:34.633 回答