Home > Web Front-end > H5 Tutorial > body text

Canvas and Drawables Translation Episode 3

黄舟
Release: 2017-01-16 16:47:02
Original
1235 people have browsed it


Android official original address: http://developer.android.com/guide/topics/graphics/2d-graphics.html

------The following translation Incorporating my own thinking, it is easy to understand. The translation in many places is not very appropriate, so I added the original English text

[title3]
On a View on the view component (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()
Copy after login

.
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.

If your application does not require a lot of processing or does not have frame rate requirements, such as board games, snakes, or slow animation applications, you can consider creating a custom view component (View) And use the drawing board (Canvas) to draw in the View.onDraw() method. The greatest convenience of doing this is that the Android framework layer will provide you with a predefined artboard (Canvas) that you can use to draw.

To start, extend the

View
Copy after login
Copy after login

class (or descendant
thereof) and define the

onDraw()
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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
Copy after login

,
which is passed to you through the

onDraw()
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

callback.

First, inherit the View class (or subclass) and define the onDraw() callback method. This method will be called by the Android framework layer, causing your View to draw itself. This is where all the drawing calls you need will be performed, and the Canvas will be passed in through the onDraw() callback method.

The Android framework will only call

onDraw()
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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

invalidate()
Copy after login

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

onDraw()
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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

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

Inside your View component's

onDraw()
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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

Canvas.draw...()
Copy after login

methods,
or other class

draw()
Copy after login

methods that take your Canvas as an argument. Once your

onDraw()
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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()
Copy after login

.

For information about extending the

View
Copy after login
Copy after login

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/
Copy after login
Copy after login

.

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

<your-sdk-directory>/samples/Snake/
Copy after login
Copy after login

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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template