首頁 > web前端 > H5教程 > 主體

Canvas and Drawables 翻譯第三集

黄舟
發布: 2017-01-16 16:47:02
原創
1200 人瀏覽過


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

f

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.onDraw()
登入後複製

class (or descendant

thereof) and define the

View
登入後複製
登入後複製

callback

method. This method will be called by the Android framework request that sage sem the Android objalled by the Android fr all your calls to draw through the

onDraw()
登入後複製
登入後複製
登入後複製
登入後複製
登入後複製
登入後複製

,

which is passed to you through the

Canvas
登入後複製

callback.


首先,繼承View類別(或子類別),並且定義onDraw()回調方法。這個方法將會被Android框架層調用,使得你的View繪製自己。在這個地方將要執行你需要的所有繪製調用,並且Canvas會透過onDraw()回呼方法傳入。

The Android framework will only call

onDraw()
登入後複製
登入後複製
登入後複製
登入後複製
登入後複製
登入後複製

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

onDraw()
登入後複製
登入後複製
登入後複製
登入後複製
登入後複製
登入後複製

.

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

onDraw()
登入後複製
登入後複製
登入後複製
登入後複製
登入後複製
登入後複製

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

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

Inside your View component's

onDraw()
登入後複製
登入後複製
登入後複製
登入後複製
登入後複製
登入後複製

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

Canvas.draw...()
登入後複製

methods,
or other class

draw()
登入後複製

methods that take your Canvas as an argument. Once your

onDraw()
登入後複製
登入後複製
登入後複製
登入後複製
登入後複製
登入後複製

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()
登入後複製

.

For information about extending the

View
登入後複製
登入後複製

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/
登入後複製
登入後複製

.

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

<your-sdk-directory>/samples/Snake/
登入後複製
登入後複製

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


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!