简体   繁体   English

在 android 中查看 onDraw(Canvas c) 与 draw(Canvas c)?

[英]View onDraw(Canvas c) versus draw(Canvas c) in android?

I am new to android development, I am exploring about View .我是 android 开发的新手,我正在探索View I come across to known two methods onDraw(Canvas c) and draw(Canvas c) .我遇到了已知的两种方法onDraw(Canvas c)draw(Canvas c)

Could please explain me the difference and usage of these two methods?请解释一下这两种方法的区别和用法? Which method will give better performance(FPS) when updating canvas with images?使用图像更新画布时,哪种方法会提供更好的性能(FPS)?

There is difference between them他们之间是有区别的

  1. The onDraw(Canvas c) is a override method and automatically called when the view is being rendered. onDraw(Canvas c)是一种重写方法,会在渲染视图时自动调用。 Here you can do your additional drawing like make circles, lines or whatever you want.在这里你可以做额外的绘图,比如画圆圈、线条或任何你想要的东西。

  2. The draw(Canvas c) is used to manually render this view (and all of its children) to the given canvas. draw(Canvas c)用于手动将此视图(及其所有子视图)渲染到给定的画布。 The view must have already done a full layout before this function is called.在调用此函数之前,视图必须已经完成了完整的布局。 When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method.实现视图时,实现onDraw(android.graphics.Canvas)而不是覆盖此方法。 If you do need to override this method, call the superclass version.如果确实需要覆盖此方法,请调用超类版本。

Or in simple words draw(Canvas c) is simply a function of a view that you can call after the view is rendered for the first time.或者简单来说, draw(Canvas c)只是一个视图函数,您可以在视图首次呈现后调用它。 This function can be used for custom drawing on any view.此功能可用于在任何视图上自定义绘图。 You need to provide the canvas on which this view will rendered and also you have to do all the drawing on the canvas before calling this function.您需要提供此视图将在其上呈现的画布,并且您还必须在调用此函数之前在画布上完成所有绘图。

Just if someone was still looking for answer like me and didn't find it.就好像有人像我一样还在寻找答案但没有找到。

The draw() method is called by the framework when the view need to be re-drawn and the draw() method then calls the onDraw() to draw the view's content.当需要重新绘制视图时,框架会调用 draw() 方法,然后 draw() 方法会调用 onDraw() 来绘制视图的内容。

void draw(Canvas canvas)
{
     ..... do default stuff (background, layers)
     onDraw(canvas)
     ..... do other stuff ( scroll bars, fading edges, children)

}

There is a misconception about this as a result of awkward API documentation.由于笨拙的 API 文档,人们对此存在误解。
The short answer is that draw(Canvas) is an inbound call on a View to do some important stuff and somewhere in the middle of the draw(Canvas) implementation it will also trigger an onDraw(Canvas) callback.简短的回答是draw(Canvas)是对 View 的入站调用以执行一些重要的事情,并且在draw(Canvas)实现中间的某个地方它还会触发onDraw(Canvas)回调。

  • Don't override draw(Canvas) when implementing a custom View intended to be used inside a layout.在实现打算在布局内使用的自定义View时,不要覆盖draw(Canvas)
  • If your custom view is intended to be used as a full screen game, then overriding draw() will save you some unneeded calls on every cycle.如果您的自定义视图打算用作全屏游戏,那么重写draw()将为您节省每个周期中一些不需要的调用。

Longer Answer更长的答案

  • The framework uses draw(Canvas) in its draw cycle.该框架在其绘制周期中使用draw(Canvas) This is what I could find in the View code:这是我在 View 代码中可以找到的内容:
    • Step 1: draw the background, if needed第 1 步:根据需要绘制背景
    • Step 2: save the canvas' layers第 2 步:保存画布的图层
    • Step 3: draw the content --- onDraw() comes here第 3 步:绘制内容 --- onDraw()来了
    • Step 4: draw the children第四步:画孩子们
    • Step 5: draw the fade effect and restore layers第五步:绘制淡入淡出效果和还原图层
    • Step 6: draw the scrollbars第六步:绘制滚动条

Useful tip有用的提示

  • You can render any view into an offscreen Bitmap you create, and later use this bitmap anywhere:您可以将任何视图渲染到您创建的离屏位图中,然后在任何地方使用此位图:

     Canvas c = new Canvas(); c.setBitmap(myOffscreenBitmap); myView.draw(c);
    • for simple cases (when it's not a ViewGroup and no scrollbars are needed), the simple onDraw(Canvas) could also do the trick.对于简单的情况(当它不是 ViewGroup 并且不需要滚动条时),简单的onDraw(Canvas)也可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM