I am new to android development, I am exploring about View
. I come across to known two methods onDraw(Canvas c)
and 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?
There is difference between them
The onDraw(Canvas c) is a override method and automatically called when the view is being rendered. Here you can do your additional drawing like make circles, lines or whatever you want.
The draw(Canvas c) is used to manually render this view (and all of its children) to the given canvas. 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. 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. 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.
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.
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)
when implementing a custom View
intended to be used inside a layout.draw()
will save you some unneeded calls on every cycle.Longer Answer
draw(Canvas)
in its draw cycle. This is what I could find in the View code:
onDraw()
comes hereUseful 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);
onDraw(Canvas)
could also do the trick.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.