Canvas and Drawables 翻译第三集

黄舟
Lepaskan: 2017-01-16 16:47:02
asal
1233 orang telah melayarinya


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

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

[title3]
On a View 在视图组件上(View)上[/title3]

If your application does not require a significant amount of processing or frame-rate speed (perhaps for a chess game, a snake game, or another slowly-animated application), then you should consider creating a custom View component and drawing with a Canvas
in

View.onDraw()
Salin selepas log masuk

.
The most convenient aspect of doing so is that the Android framework will provide you with a pre-defined Canvas to which you will place your drawing calls.

如果你的应用不需要大量的处理或没有帧速率要求,像棋牌类游戏、贪吃蛇、或者慢动画应用,你可以考虑创建一个自定义视图组件(View)并且使用画板(Canvas)在View.onDraw()方法里绘制。这样做最大的方便之处在于Android框架层将为你提供一个预先定义好的画板(Canvas),你可以使用该Canvas进行绘制。

To start, extend the

View
Salin selepas log masuk
Salin selepas log masuk

class (or descendant
thereof) and define the

onDraw()
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

callback
method. This method will be called by the Android framework to request that your View draw itself. This is where you will perform all your calls to draw through the

Canvas
Salin selepas log masuk

,
which is passed to you through the

onDraw()
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

callback.

首先,继承View类(或者子类),并且定义onDraw()回调方法。该方法将会被Android框架层调用,使得你的View绘制自己。在这个地方将要执行你需要的所有绘制调用,并且Canvas会通过onDraw()回调方法传入。

The Android framework will only call

onDraw()
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

as necessary. Each time that your application is prepared to be drawn, you must request your View be invalidated by calling

invalidate()
Salin selepas log masuk

.
This indicates that you'd like your View to be drawn and Android will then call your

onDraw()
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

method (though is not guaranteed that the callback will be instantaneous).

Android框架层只会在需要的时候调用onDraw()方法。每次当你的应用准备好需要去绘制的时候,你必须通过invalidate()方法使你的View无效。这表示你想要你的View被绘制并且Android将要回调你的onDraw()方法(不保证这个回调将会被及时执行)

Inside your View component's

onDraw()
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

, use the Canvas given to you for all your drawing, using various

Canvas.draw...()
Salin selepas log masuk

methods,
or other class

draw()
Salin selepas log masuk

methods that take your Canvas as an argument. Once your

onDraw()
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

is complete, the Android
framework will use your Canvas to draw a Bitmap handled by the system.

在你的View组件里的onDraw()方法中,使用给你的Canvas作你所有的绘制,使用各种各样的Canvas.draw...()方法,或者其他类的Draw()方法,这个类能够把给你的Canvas作为参数传入。一旦你的onDraw()方法完成,Android框架将要用你的Canvas去绘制一张位图。

Note: In order to request an invalidate from a thread other than your main Activity's thread, you must call

postInvalidate()
Salin selepas log masuk

.

For information about extending the

View
Salin selepas log masuk
Salin selepas log masuk

class,
read Building Custom Components.

注意:你如果要从主Activity的线程外的线程中使View无效,你必须调用postInvalidate()方法,更多关于继承View类的信息,请阅读Building
Custom Components。

For a sample application, see the Snake game, in the SDK samples folder:

<your-sdk-directory>/samples/Snake/
Salin selepas log masuk
Salin selepas log masuk

.

一个程序示例,贪吃蛇游戏,放在SDK示例文件夹中,路径:

<your-sdk-directory>/samples/Snake/
Salin selepas log masuk
Salin selepas log masuk

以上就是Canvas and Drawables 翻译第三集的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan