Android View and Surfaceview

Source: Internet
Author: User

Android View and Surfaceview

The main addition to the control class in Android games is the Display Class View. Surfaceview is a display class that derives from the view base class. the three views commonly used in Android game development are: view, Surfaceview, and Glsurfaceview.

View: Display view, built-in canvas, provides graphical drawing functions, touch-screen events, key event functions, etc. , must be updated within the UI main thread, slower.

Surfaceview: Views based on the view of the expansion of the class, more suitable for 2D game development; is a subclass of view, similar to the use of double-slow mechanism to update the screen in a new thread so the refresh interface faster than the view.

Glsurfaceview: The view class, which is extended again based on the Surfaceview view, is designed for 3D game development, and is a subclass of Surfaceview, dedicated to OpenGL.

  

In the development of 2D game, it can be divided into two kinds of game frame, view and Surfaceview. View and Surfaceview differences:

View: The screen must be updated in the main thread of the UI for the passive update screen.   

Both the Surfaceview:ui thread and the child thread are available. Redraw the screen in a newly started thread and actively update the screen.


Updating the screen in the main thread of the UI may cause problems, such as if you are updating the screen too long, then your main UI thread will be blocked by the function you are drawing. Then you will not be able to respond to keystrokes, touch screens and other messages.
When using Surfaceview because it is updating the screen in a new thread, it does not block your UI main thread. But this also brings up another problem, that is, event synchronization, which involves thread synchronization.

Therefore, based on the above, according to the characteristics of the game, generally divided into two categories.

1 Passive Update screen. Like chess, this is good with view. Because the screen update is dependent on OnTouch to update, you can use the invalidate directly. Because of this, the touch and the next touch will take longer and will not have an impact.

2 active updates. Like a man running all the time. This requires a separate thread to redraw the human state continuously, avoiding blocking the main UI thread. So obviously the view is not suitable, need surfaceview to control.

Here is a basic framework for 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;

PublicClass TestsurfaceviewExtends Activity {
/**Called when the activity is first created.*/
@Override
Publicvoid OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (New MyView (This));
}

Class MyViewExtends SurfaceviewImplements surfaceholder.callback,runnable{
Surfaceholder holder=Null
Paint paint;
Public MyView (Context 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
Publicvoid Surfacechanged (Surfaceholder holder,int format,int width,
int height) {
//TODO auto-generated Method Stub

}

@Override
Publicvoid surfacecreated (Surfaceholder holder) {
//TODO auto-generated Method Stub
Thread t=New Thread (this);
T.start ();
}

@Override
Publicvoid Surfacedestroyed (Surfaceholder holder) {
//TODO auto-generated Method Stub
isrunning=False
}

@Override
Protectedvoid OnDraw (canvas canvas) {
//TODO auto-generated Method Stub
Canvas=holder.lockcanvas ();
//Brush screen
Canvas.drawcolor (Color.Black);

Canvas.drawcircle (x, Y, ten, paint);

Holder.unlockcanvasandpost (canvas);
}
Privatevoid Paint (paint paint) {
Canvas Canvas=holder.lockcanvas ();
//Brush screen
Canvas.drawcolor (Color.Black);

Canvas.drawcircle (x, Y, ten, paint);

Holder.unlockcanvasandpost (canvas);
}

Boolean isrunning=True
@Override
Publicvoid 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;
}
}

}
/span>

Android View and Surfaceview

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.