Android view and surfaceview

Source: Internet
Author: User
Tags android games

In Android games, only the control class is the display Class View. Surfaceview is a display class derived from the view base class.Three commonly used views in android game development are view, surfaceview, and glsurfaceview.

View: display view, built-in canvas, drawing function, touch screen event, button event function, etc;The screen must be updated in the main UI thread, which is slow.

Surfaceview: Expanded View Class Based on view, more suitable for 2D game development;Is a subclass of the view, similar to the double easing mechanism, to update the screen in the new thread, so refreshing the interface is faster than the view.

Glsurfaceview: Expanded View Class Based on surfaceview, dedicated to 3D game development view;It is a subclass of surfaceview and is dedicated to OpenGL.

In 2D game development, there are roughly two game frameworks: View and surfaceview.Difference between view and surfaceview:

 View: You must update the screen in the main UI thread for passive update.

Surfaceview: both the UI thread and sub-thread can be used. Re-draw the screen in a new thread to take the initiative to update the screen.

Updating the screen in the main UI thread may cause problems. For example, if you update the screen for a long time, your main UI thread will be blocked by the function you are painting. Then, messages such as buttons and touch screens cannot be responded.
When surfaceview is used to update the screen in a new thread, it will not block your main UI thread. But this also brings about another problem: event synchronization, involving thread synchronization.

 

Based on the above, the game features are generally divided into two categories.

1. passively update the image. For example, you can use view for chess. Because image update relies on ontouch for update, you can use invalidate directly. In this case, this touch and the next touch take a longer time and will not be affected.

2. Actively update. For example, a person is always running. This requires a separate thread to repeatedly repaint the state of the person, to avoid blocking the main UI thread. Obviously, the view is not suitable and needs surfaceview for control.

Below is a basic framework using sufaceview:

 Import Android. App. activity;
Import Android. content. context;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. OS. Bundle;
Import Android. View. surfaceholder;
Import Android. View. surfaceview;

Public Class Testsurfaceview Extends Activity {
/** Called when the activity is first created. */
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview ( New Myview (This ));
}

Class Myview Extends Surfaceview Implements Surfaceholder. Callback, runnable {
Surfaceholder holder = Null ;
Paint paint;
Public Myview (context ){
Super (Context );
// Todo auto-generated constructor stub
Holder = getholder ();
Holder. addcallback ( This );
Paint = New Paint (paint. anti_alias_flag );
Paint. setcolor (color. Red );

This . Setfocusable ( True );
}

@ Override
Public Void Surfacechanged (surfaceholder holder,Int Format, Int Width,
Int Height ){
// Todo auto-generated method stub

}

@ Override
Public Void Surfacecreated (surfaceholder holder ){
// Todo auto-generated method stub
Thread t = New Thread ( This );
T. Start ();
}

@ Override
Public Void Surfacedestroyed (surfaceholder holder ){
// Todo auto-generated method stub
Isrunning = False ;
}

@ Override
Protected Void Ondraw (canvas ){
// Todo auto-generated method stub
Canvas = holder. lockcanvas ();
// Screen Flushing
Canvas. drawcolor (color. Black );

Canvas. drawcircle (X, Y, 10, paint );

Holder. unlockcanvasandpost (canvas );
}
Private Void Paint (paint ){
Canvas canvas = holder. lockcanvas ();
// Screen Flushing
Canvas. drawcolor (color. Black );

Canvas. drawcircle (X, Y, 10, paint );

Holder. unlockcanvasandpost (canvas );
}

Boolean Isrunning = True ;
@ Override
Public Void Run (){
// Todo auto-generated method stub
While (Isrunning ){
// Ondraw (null );
Paint (paint );
Move ();
Try {
Thread. Sleep (50 );
}Catch (Interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

}
}

Private Int X, Y;
Private Void Move (){
X + = 2;
Y + = 2;
}
}

}
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.