What calls the OnDraw method to view and when

I saw this in the Textview class for android:

@Override protected void onDraw(Canvas canvas) { restartMarqueeIfNeeded(); // Draw the background for this view super.onDraw(canvas); 

In the View android class, I see that it is empty:

 /** * Implement this to do your drawing. * * @param canvas the canvas on which the background will be drawn */ protected void onDraw(Canvas canvas) { } 

Why would anyone call a superclass method if it is empty?

Where is the method called using the parameter canvas?

Is the parameter canvas transferred automatically by the android system?

When is the ondraw method called and by whom?

When it is redefined, is the subclass method used instead of the superclass'?

This is my custom view, for example:

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this)); } } class MyView extends View { public MyView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } } 

Who is calling the MyView.OnDraw method?

Should there be any one line of code that calls the Myview.Ondraw.Isnt'it method?

+5
source share
3 answers

To answer your questions:

Why would anyone call a superclass method if it is empty?

Because it’s good practice to call it super if you don’t know what the implementation is. For example, if you have an extended class class in a library and you do not have code for this, you must call the super method. In this case, however, this is not necessary, but I would recommend it

Where is the method called using the parameter canvas?

I'm not quite sure what you mean by that, but when you let the class override View , you can override onDraw so you can decide what your view looks like.

Is the parameter canvas transferred automatically by the android system?

Yes

When is the ondraw method called and by whom?

It is caused by an activity to which it is attached somewhere in its life cycle when it becomes visible for the first time. You have nothing to worry about. It is also called when you call invalidate in the view or when it is considered dirty (it needs to be redrawn). onDraw will be called in your example, but since you are not drawing anything on the supplied canvas, you will not see anything in your activity. If you add some log to the function, you will see that it is in logcat

When it is redefined, is the subclass method used instead of the superclass'?

Yes, how does it work with class extensions and overriding methods

+2
source

onDraw is called by ViewRoot when someone requested a redraw of one of its child views by calling invalidate or postInvalidateOnAnimation . ViewRoot calls onDraw each child Window element, and those, in turn, draw their visible child elements (in the case of ViewGroup) or themselves (in the case of Views) until the last view is displayed.

A drawing can be executed at any time in the main thread, which is fine and used by default for older versions, but not visually perfect. Alternatively, it can be delayed until the next vertical synchronization through the Choreographer . Modern versions of Android plan all onDraw calls caused by invalidation and Drawable animations through the choreographer.

Canvas is provided by SurfaceFlinger , an Android Window system that internally uses OpenGL ES and hardware composition to control displayed windows.

When it is redefined, is the subclass method used instead of the superclass'?

Yes. But not calling onDraw superclass is rarely a valid tactic (why do you first subclass it ?!) If you don't want something to be drawn, you can call setWillNotDraw(true) (the default for the base View class is already true , by the way )

+1
source

In addition to the following: the soft keyboard calls the sequence View.invalidate () -> View.onDraw () after resizing the window to intelligently place the "keyboard". Custom View.onDraw () should leave itself in a state that anticipates this feature.
This may explain why an application developed and tested on a tablet with a Bluetooth keyboard suddenly appeared in the real world.

0
source

Source: https://habr.com/ru/post/1243667/


All Articles