Canvas and Drawables 翻译第四集

来源:互联网 发布:excel sql建立数据库 编辑:程序博客网 时间:2024/06/06 09:01

原文地址:http://blog.csdn.net/wcs542882916


Android官方原文地址:http://developer.android.com/guide/topics/graphics/2d-graphics.html

------以下的翻译融入了自己的思考,便于理解,很多地方翻译并不是很恰当,所以配上了英文原文

On a SurfaceView

SurfaceView控件上绘制

The SurfaceView is a special subclass of View that offers a dedicated drawing surface within the View hierarchy. The aim is to offer this drawing surface to an application's secondary thread, so that the application isn't required to wait until the system's View hierarchy is ready to draw. Instead, a secondary thread that has reference to a SurfaceView can draw to its own Canvas at its own pace.

SurfaceView是View的一个特别的子类,提供一个在View层级内绘制的界面。目标在于给应用的第二线程提供一个绘制界面,所以应用不用等待系统View准备好就可以绘制了。而且,与SurfaceView相关的第二线程能够以它自己的速度画在它自己的Canvas上。 

To begin, you need to create a new class that extends SurfaceView. The class should also implementSurfaceHolder.Callback. This subclass is an interface that will notify you with information about the underlying Surface, such as when it is created, changed, or destroyed. These events are important so that you know when you can start drawing, whether you need to make adjustments based on new surface properties, and when to stop drawing and potentially kill some tasks. Inside your SurfaceView class is also a good place to define your secondary Thread class, which will perform all the drawing procedures to your Canvas.

首先,你应该创建一个类继承SurfaceView。这个类也应该实现SurfaceView.Callback接口。这个接口在Surface创建、改变或销毁的时候将会通知你。这些事件非常重要,它让你知道该什么时候开始绘制,是否需要根据界面属性来做调整,或者什么时候停止绘制和杀死潜在的一些任务。在你的SurfaceView类里面也是定义你的第二线程的好地方,这个第二线程将会在Canvas上执行所有的绘制。

Instead of handling the Surface object directly, you should handle it via a SurfaceHolder. So, when your SurfaceView is initialized, get the SurfaceHolder by calling getHolder(). You should then notify the SurfaceHolder that you'd like to receive SurfaceHolder callbacks (fromSurfaceHolder.Callback) by calling addCallback() (pass it this). Then override each of theSurfaceHolder.Callback methods inside your SurfaceView class.

避免直接操作Surface对象,你应该通过SurfaceHolder来处理。所以,当你的SurfaceView被初始化后,通过调用getHolder()来获取SurfaceHolder。然后你应该通知SurfaceHolder你想要通过调用addCallback()(传入this)来接收SurfaceHolder 的回调。然后重写你的SurfaceView类里的SurfaceHolder.Callback接口的每一个方法。

In order to draw to the Surface Canvas from within your second thread, you must pass the thread your SurfaceHandler and retrieve the Canvas with lockCanvas(). You can now take the Canvas given to you by the SurfaceHolder and do your necessary drawing upon it. Once you're done drawing with the Canvas, call unlockCanvasAndPost(), passing it your Canvas object. The Surface will now draw the Canvas as you left it. Perform this sequence of locking and unlocking the canvas each time you want to redraw.

为了从第二线程绘制到Surface Canvas上,你必须把SurfaceHandler 传给这个线程,然后调用lockCanvas()来取回Canvas。现在你就可以使用取回的Canvas来进行你需要的绘制。一旦你使用该Canvas进行绘制,调用unlockCanvasAndPost(),传入你的Canvas对象。Surface就会绘制你的Canvas。如果你每次想要重绘,执行这些锁定Canvas和解锁Canvas序列。

Note: On each pass you retrieve the Canvas from the SurfaceHolder, the previous state of the Canvas will be retained. In order to properly animate your graphics, you must re-paint the entire surface. For example, you can clear the previous state of the Canvas by filling in a color withdrawColor() or setting a background image with drawBitmap(). Otherwise, you will see traces of the drawings you previously performed.

注意:每次传入从SurfaceHolder取回的Canvas,这个Canvas先前的状态将会被保留。为了能使图形正确的过度,你必须重绘整个Surface。例如:通过调用drawColor()填充一种颜色来清除先前的Canvas的状态,或者调用drawBitmap()方法设置一张背景图片。否则,你将会看到先前绘制的痕迹。

For a sample application, see the Lunar Lander game, in the SDK samples folder: <your-sdk-directory>/samples/LunarLander/. Or, browse the source in the Sample Code section.

例程请看Lunar Lander游戏,SDK路径:<your-sdk-directory>/samples/LunarLander/。或者浏览Sample Code块的源码。


0 0
原创粉丝点击