使用Canvas的Andr​​oid FingerPaint例子,什么是屏幕外的画布?画布、例子、屏幕、Andr

2023-09-07 16:27:44 作者:不念过往如此安好

您好我正在读 fingerpaint 的例子,因为我建立一个签名活动,即允许用户利用该手机的签名,然后将其保存到SD。

Hi I was reading the fingerpaint example, because I'm building a signature activity, that allows the user to draw a signature on the cellphone and then save it to SD.

到目前为止,我已经看到了的mpath变量认为,用户正在绘制的路径,这条路径是通过调用绘制到上的OnDraw(..)方法屏幕

So far I've seen that the mPath variables holds the path that the user is currently drawing, and this path is drawn onto the screen on the onDraw(..) method by calling

canvas.drawPath(mPath, mPaint);

但在这个例子还有另外一个画布mCanvas绘制在触摸监听器的路径:

However on the example there is another canvas "mCanvas" that draws the path on the touch listener:

private void touch_up() {
    mPath.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    mPath.reset();
}

这是我没有得到什么。究竟是什么这个mCanvas对象,他们为什么使用它在这个例子中,似乎只有从OnDraw的方法和变量的mpath正规画布就足够了这样做?

And this is what I don't get. what is exactly this mCanvas object, and why are they using it in the example, it seems that only the regular canvas from the onDraw method and the mPath variable would have been enough for doing this?

推荐答案

的OnDraw 方法是在UI线程上执行。虽然我们没有进入到UI线程(你不想使用UI线程经常),我们保持一个关闭屏幕 位图画布,我们用它来画就可以了。

The onDraw method is executed on the UI thread. While we don't have access to the UI thread (you don't want to be using the UI thread that often) we keep an off-screen Bitmap with a Canvas that we use to draw on it.

为什么这样做?这是因为它使我们能够专注于绘图/处理,而无需担心阻塞UI线程。

Why do this? This is because it allows us to focus on the drawing/processing without having to worry about blocking the UI thread.

注意:调用方法废止(或 postInvalidate )不会立即阻止并调用的OnDraw - 它只是排队与操作系统平局通话

Note: Calling the method invalidate (or postInvalidate) does not instantaneously block and call onDraw - it just queues up a draw call with the OS.

 
精彩推荐
图片推荐