I saw this in Textview class of android:
@Override
protected void onDraw(Canvas canvas) {
restartMarqueeIfNeeded();
// Draw the background for this view
super.onDraw(canvas);
In View
class of android, 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 anybody call method of super class if it's empty?
Where is the method called with parameter canvas?
Is the parameter canvas passed automatically by the android system?
When is ondraw method called and by whom?
When it's overridden ,is the method of subclass called instead of 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 calls MyView.OnDraw method?
There must be some one line of code that calls Myview.Ondraw method.Isnt'it?
To answer your questions:
Why would anybody call method of super class if it's empty?
Because it is good practice to call the super if you're not sure what the implementation is. For example, if you have an extended class of a class in a library and you do not have the code of that, you should call the super method. In this case however, it is not necessary but I would recommend it
Where is the method called with parameter canvas?
I'm not entirely sure what you mean by this, but when you let your class override View
, you may override onDraw
so you can decide how your view looks
Is the parameter canvas passed automatically by the android system?
Yes
When is ondraw method called and by whom?
It is called by the Activity it is attached to somewhere in its lifecycle when it becomes visible for the first time. You do not have to worry about that. It is also called when you call invalidate
on the View or when it is considered dirty (needs to be redrawn). In your example, onDraw
will be called but since you do not draw anything on the supplied canvas, you won't see anything on your Activity. If you add some log to the function you will see it coming by in logcat
When it's overridden ,is the method of subclass called instead of superclass'?
Yes, that's how it works with extending classes and overriding methods
The onDraw
is called by ViewRoot when someone has requested redrawing of one it's child Views by calling invalidate
or postInvalidateOnAnimation
. The ViewRoot is calling onDraw
of each Window child, and those in turn draw their visible children (in case of ViewGroup) or themselves (in case of Views) until the last View is drawn.
The drawing may performed anytime on the main thread, which is ok and used to be default on older versions, but not visually perfect. Alternatively it may postponed until next vertical sync via Choreographer. Modern Android versions schedule all calls to onDraw
, caused by invalidation and Drawable animation, via Choreographer.
The Canvas is provided by SurfaceFlinger — Android Window system, which internally uses OpenGL ES and hardware composition to manage displayed windows.
When it's overridden ,is the method of subclass called instead of superclass'?
Yes. But not calling onDraw
of superclass is rarely a valid tactic (why are you subclassing it in the first place?!) If you do not want anything to be drawn you can call setWillNotDraw(true)
(the default for the base View class is already true
, by the way).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With