0

0

Canvas and Drawables 翻译第三集

黄舟

黄舟

发布时间:2017-01-16 16:47:02

|

1301人浏览过

|

来源于php中文网

原创


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()

.
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

class (or descendant
thereof) and define the

onDraw()

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

,
which is passed to you through the

onDraw()

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

invalidate()

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

/samples/Snake/

.

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

/samples/Snake/

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

陌言AI
陌言AI

陌言AI是一个一站式AI创作平台,支持在线AI写作,AI对话,AI绘画等功能

下载

相关专题

更多
html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

16

2026.01.21

三角洲入口地址合集
三角洲入口地址合集

本专题整合了三角洲入口地址合集,阅读专题下面的文章了解更多详细内容。

17

2026.01.21

AO3中文版入口地址大全
AO3中文版入口地址大全

本专题整合了AO3中文版入口地址大全,阅读专题下面的的文章了解更多详细内容。

215

2026.01.21

妖精漫画入口地址合集
妖精漫画入口地址合集

本专题整合了妖精漫画入口地址合集,阅读专题下面的文章了解更多详细内容。

57

2026.01.21

java版本选择建议
java版本选择建议

本专题整合了java版本相关合集,阅读专题下面的文章了解更多详细内容。

3

2026.01.21

Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

14

2026.01.21

C++多线程相关合集
C++多线程相关合集

本专题整合了C++多线程相关教程,阅读专题下面的的文章了解更多详细内容。

6

2026.01.21

无人机驾驶证报考 uom民用无人机综合管理平台官网
无人机驾驶证报考 uom民用无人机综合管理平台官网

无人机驾驶证(CAAC执照)报考需年满16周岁,初中以上学历,身体健康(矫正视力1.0以上,无严重疾病),且无犯罪记录。个人需通过民航局授权的训练机构报名,经理论(法规、原理)、模拟飞行、实操(GPS/姿态模式)及地面站训练后考试合格,通常15-25天拿证。

22

2026.01.21

Python多线程合集
Python多线程合集

本专题整合了Python多线程相关教程,阅读专题下面的文章了解更多详细内容。

1

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Canvas 绘制时钟
Canvas 绘制时钟

共7课时 | 1.5万人学习

HTML5新特性基础视频教程
HTML5新特性基础视频教程

共18课时 | 3.2万人学习

HTML5 Canvas 动画实战教程
HTML5 Canvas 动画实战教程

共28课时 | 6.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号