Android game Development Surfaceview use-android learning Journey (Fri)

Source: Internet
Author: User

The difference between Surfaceview and view

View is in the main thread of the UI that responds directly to the user's actions, as well as the distribution of tasks, but the task is more complex and can become blocked.
Surfaceview does not have this problem, assuming that it obtains images directly from memory, and more importantly, Surfaceview can change the UI through threads outside the main thread.

Use

Updates to the UI are divided into active and passive updates, and for passive updates, they are updated with time-based controls, often with low frequencies, so they tend to choose view to complete.
For the active update, update frequency faster, such as Timer update screen, a version will take Surfaceview.
Instance code:

 Public  class MyView extends surfaceview implements Surfaceholder . Callback {     Public MyView(Context context) {Super(context); Getholder (). Addcallback ( This); } Public void Draw(){//Lock CanvasCanvas canvas = Getholder (). Lockcanvas ();//After closing, remember to unlock the canvasGetholder (). Unlockcanvasandpost (canvas); }@Override     Public void surfacecreated(Surfaceholder Holder) {//TODO auto-generated method stub}@Override     Public void surfacechanged(Surfaceholder holder,intFormatintWidthintHeight) {//TODO auto-generated method stub}@Override     Public void surfacedestroyed(Surfaceholder Holder) {//TODO auto-generated method stub}}
Draw simple graphics with Suifaceview

A red Square is drawn:

 Public  class MyView extends surfaceview implements Surfaceholder . Callback {    PrivatePaint paint =NULL; Public MyView(Context context) {Super(context); Paint =NewPaint ();        Paint.setcolor (color.red); Getholder (). Addcallback ( This); } Public void Draw() {Canvas canvas = Getholder (). Lockcanvas ();        Canvas.drawcolor (Color.White); Canvas.drawrect (0,0, -, -, paint);    Getholder (). Unlockcanvasandpost (canvas); }@Override     Public void surfacecreated(Surfaceholder Holder) {//TODO auto-generated method stubDraw (); }@Override     Public void surfacechanged(Surfaceholder holder,intFormatintWidthintHeight) {//TODO auto-generated method stub}@Override     Public void surfacedestroyed(Surfaceholder Holder) {//TODO auto-generated method stub}}

Note Be sure to start drawing after the Surfacecreated method, and be sure to finish drawing before the Surfacedestroyed method.

Android game Development Surfaceview use-android learning Journey (Fri)

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.