Android Canvas and Drawables (android 图形绘制 canvas 和 Drawables)

The Android framework APIs provides a set of 2D-drawing APIs that allow you to render your own custom graphics onto a canvas or to modify existing Views to customize their look and feel. When drawing 2D graphics, you'll typically do so in one of two ways:


通过canvas绘制你自己的图像,或者修改现存的views达到特制的外观和感觉,下面是两种途径:

a

Draw your graphics or animations into a View object from your layout. In this manner, the drawing of your graphics is handled by the system's normal View hierarchy drawing process — you simply define the graphics to go inside the View.

在你的layout中的view对象中绘制进你自己的图像或者是动画。在这种方式下面,绘制是被系统正常的view hierarchy drawing process所处理的。你仅仅只是需要在你的view中定义好你的图像。


b

Draw your graphics directly to a Canvas. This way, you personally call the appropriate class's onDraw() method (passing it your Canvas), or one of the Canvas draw...() methods (likedrawPicture()). In doing so, you are also in control of any animation.

在Canvas对象中直接绘制你的图像。在这种方法下,你应该自己适当的调用OnDraw()函数(将你自己的Canvas对象传进去),或则是Canvas draw()方法(有点类似于 drawPicture())。这么做你也同样可以控制动画。


a

操作,在view中绘制,当你想要绘制一个简单的图像,它不需要动态的变化,也不是性能敏感游戏的一部分是,a操作是最好的选择。举个例子啊,当你想要在静态程序中绘制一张静态的图像,或者是预定义好的动画至view中,这是好的选择。可以去看看Drawables


b

操作,当你的app需要常规的re-draw它的时候最好使用Canvas。比如是视频游戏。还有其他方法来完成这个工作:


1在你的ui thread,在你自己的layout中的custom view组件中,调用 invalidata() 再处理 onDraw()回调函数。


2在另一个线程中,在你管理的SurfaceView中,在你的线程能承受的情况下执行最快的执行绘图工作到Canvas



Draw with a Canvas

当你在写一个你学要表现出特别的绘制和控制图像的动画的程序时,你就应该通过Canvas来绘制。一个Canvas能够为你像pretense,interface在你图像的实际表面上绘制,它会帮你维持所有的“draw”方法调用。通过canvas,你可以再一张显示于窗口的图像上直接做动作。


On a View

如果你的程序不需要大良的处理和帧率速度(比如棋类游戏,贪吃蛇游戏,或者是其他慢动作的游戏)你就应该考虑创建一个custom view组件,在View.onDraw()方法中通过canvas绘制。这样做最方便的方面是,android框架会将提供给你预定义好的画布让你在上面使用drawing calls。

On a SurfaceView


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值