Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View onDraw(Canvas c) versus draw(Canvas c) in android?

Tags:

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?

like image 854
mini Avatar asked Nov 30 '12 07:11

mini


People also ask

What is onDraw method in Android?

The parameter to onDraw() is a Canvas object that the view can use to draw itself. The Canvas class defines methods for drawing text, lines, bitmaps, and many other graphics primitives. You can use these methods in onDraw() to create your custom user interface (UI).

What is Canvas drawing in Android?

Canvas is a class in Android that performs 2D drawing of different objects onto the screen. The saying “a blank canvas” is very similar to what a Canvas object is on Android. It is basically, an empty space to draw onto. The Canvas class is not a new concept, this class is actually wrapping a SKCanvas under the hood.

How do I add a view to Canvas?

This can be achieved simply by extending the View class and define an onDraw() callback method. Inside your View component's onDraw(), use the Canvas given to you for all your drawing, using various Canvas. draw... () methods (Ex: canvas.

Which method is used to redraw on screen and results to a call of the view onDraw () method?

You can't call onDraw directly. Instead you should call invalidate to tell the view that it needs to redraw itself.


2 Answers

There is difference between them

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

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

like image 60
Ali Imran Avatar answered Oct 05 '22 05:10

Ali Imran


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)  } 
like image 24
Karim Tarabishy Avatar answered Oct 05 '22 06:10

Karim Tarabishy