Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View.onDraw() --- when does it get called?

I put a Log.d() call into the onDraw() of my extended View, so I could see how often and when it's getting called. It gets called upon instantiation of the view, which is not surprising. But then I notice, it gets called on every tap that is handled by onTouchEvent(), even though my code there isn't doing anything remotely related to graphics. However, in the documentation for Views, I can't seem to find anything about when onDraw() is actually called. I'm not really concerned about my particular project here (this doesn't cause a problem for me), I would just like to know if there is a list somewhere or something that shows the order of operations for a View, particularly what causes onDraw() to get called.

like image 635
CptSupermrkt Avatar asked Aug 11 '12 06:08

CptSupermrkt


People also ask

When onDraw called?

The onDraw method is called whenever android thinks that your view should be redrawn. This can be tha case when your view is animated, in which case onDraw is called for every frame in the animation. It is also called when the layout changes and your view is re-positioned on the screen.

How do I call onDraw?

Call mView. invalidate(); in your onClick() method for that View. This will call onDraw() (eventually), which will then run the drawing code for your view. This will allow you to also invoke onDraw() of the view in the main activity.

What is onDraw method?

Override onDraw() 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).


1 Answers

AFAIK, a View's onDraw() is called when:

  1. The view is initially drawn
  2. Whenever invalidate() is called on the view

Invalidate can be called by you or the system whenever needed. For example, a lot of Views change how they look onTouch, like an EditText getting an outline and cursor, or a button being in the pressed state. Due to this, Views are redrawn on touch.

I agree that it would be nice to have a document that detailed the working of Views, and if one exists and somebody knows where to find it, please let us know.

like image 71
Raghav Sood Avatar answered Oct 12 '22 00:10

Raghav Sood