Even overriding the OnDraw () method in a class that inherits Surfaceview is useless, because Surfaceview inherits from view, but does not
overriding OnDraw (), whose subclasses can override OnDraw () but cannot automatically invoke
Workaround:
First Surfaceview subclass XXX to implements Surfaceholder.callback, then define private surfaceholder sh = null;
In its constructor:
Public XXX (context context, AttributeSet attrs) {
super (context, attrs);
//TODO auto-generated constructor stub
sh = Getholder ();
Sh.addcallback (this);
}
//Custom draw function
public void Dodraw () {
paint p = new paint ();//Stroke
P.setantialias (TRUE); Anti-aliasing
p.setcolor (color.red);
P.setstyle (Style.stroke);
canvas Canvas = Sh.lockcanvas ();
Canvas.drawcolor (color.white);//Background
Canvas.drawrect (Ten, ten, +, p) ;
sh.unlockcanvasandpost (canvas);//Submit Drawing content
}
Call Dodraw () where you want to draw.
OnDraw method that inherits from view
@Override
protected void OnDraw (canvas canvas) {
TODO auto-generated Method Stub
Super.ondraw (canvas);
Paint p = new paint (); Stroke
P.setantialias (TRUE); Anti-aliasing
P.setcolor (color.red);
P.setstyle (Style.stroke);
Canvas = Sh.lockcanvas ();
Canvas.drawcolor (Color.White);//Background color
Canvas.drawrect (Ten, Ten, Max, p);
Sh.unlockcanvasandpost (canvas); Submit Drawing Content
}
Call OnDraw (NULL) where you want to draw.
Note: overriding Surfaceview's draw () will eventually call the OnDraw (), but there is an exception when calling draw (null).
The use of Android Surfaceview