The use of Android Surfaceview

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.