VIEW and SurfaceView Differences and Contact

In addition to the control class, the main Android game is the display class View. SurfaceView is a display class derived from the View base class. The three commonly used views in android game development are: view, SurfaceView and GLSurfaceView.

  View: display view, built-in canvas, provide graphics drawing functions, touch screen events, key event functions, etc.; must be in the main UI Update the screen in the thread, the speed is slower.

  SurfaceView: A view class expanded based on the view view, which is more suitable for the development of 2D games; is the child of view Class, similar to the use of double buffer mechanism, update the screen in a new thread, so the refreshing interface speed is faster than the view.

  GLSurfaceView: A view class expanded again based on the SurfaceView view, dedicated to 3D game development; is SurfaceView A subclass, dedicated to openGL.

  

   in 2D game development Among them, it can be roughly divided into two game frameworks, View and SurfaceView. Difference between View and SurfaceView:

  View: The screen must be updated in the main thread of the UI for passively updating the screen.

  surfaceView: Both in UI thread and sub-thread. Redraw the picture in a newly started thread, and actively update the picture.

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

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

1 被动更新画面的。 Such as chess, this kind of use view is just fine. Because the screen update depends on onTouch to update, you can use invalidate directly. Because in this case, the time required for this touch and the next touch is longer and will not have any effect. 

2 主动更新。 For example, a person is constantly running. This requires a separate thread to continuously redraw the state of the person to avoid blocking the main UI thread. So obviously the view is not suitable and needs surfaceView to control it.

The following is a basic frame using sufaceView:

copy code
 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 < span style="color:rgb(0,0,255); line-height:1.5!important">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 span> 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 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 canvas) {// TODO Auto-generated method stub  canvas=holder.lockCanvas(); //Swipe screen canvas.drawColor(Color.BLACK); canvas.drawCircle(x, y, 10, paint); holder.unlockCanvasAndPost(canvas);} private void paint(Paint paint) {Canvas canvas=holder.lockCanvas(); //swipe screen  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 < /span> 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;}}} 

copy code
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 {/**< span style="color:rgb(0,128,0); line-height:1.5!important"> Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this));} class< /span> MyView extends SurfaceView implements 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 public void surfaceChanged(SurfaceHolder holder, int< /span> 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 < span style="color:rgb(0,128,0); line-height:1.5!important"> isRunning=false;} @Override protected void onDraw(Canvas canvas) {// TODO Auto-generated method stub  canvas=holder.lockCanvas(); //Swipe screen  canvas.drawColor(Color.BLACK); canvas.drawCircle(x, y, 10, paint); holder.unlockCanvasAndPost(canvas);} private void paint(Paint paint) {Canvas canvas=holder.lockCanvas(); //Swipe screen 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;}}} 

< img src="/wp-content/uploadshttp:/img.voidcn.com/vcimg/static/loading.png" alt="copy code" style="max-width:900px; border:none!important" d=" 5338" s="48f_eef" t="gif">

Leave a Comment

Your email address will not be published.