Simple SurfaceView Example

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Simple SurfaceView Example

Postby yohosuff » Wed Mar 20, 2013 5:36 am

surface_view_demo.zip
Extract to a folder. Make 'Android Project from Existing Code' with ADT. Browse to folder. Finish.
(913.63 KiB) Downloaded 1816 times


Comments and critiques are extremely welcome!

MainActivity.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example.circle5;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class MainActivity extends Activity {
  7.  
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         //Set the content view to a new instance of DrawingBoard.
  12.         setContentView(new DrawingBoard(this));
  13.     }
  14.    
  15. }
  16.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


DrawingBoard.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example.circle5;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.view.SurfaceHolder;
  8. import android.view.SurfaceView;
  9.  
  10. public class DrawingBoard extends SurfaceView implements SurfaceHolder.Callback, Runnable {
  11.  
  12.         private SurfaceHolder surfaceHolder = null;
  13.        
  14.         public DrawingBoard(Context context) {
  15.                 super(context);
  16.  
  17.                 //Retrieve the SurfaceHolder instance associated with this SurfaceView.
  18.                 surfaceHolder = getHolder();
  19.  
  20.                 //Specify this class (DrawingBoard) as the class that implements the three callback methods required by SurfaceHolder.Callback.
  21.                 surfaceHolder.addCallback(this);
  22.  
  23.         }
  24.        
  25.         //SurfaceHolder.Callback callback method.
  26.         @Override
  27.         public void surfaceCreated(SurfaceHolder holder) {
  28.                 //Create and start a drawing thread whose Runnable object is defined by this class (DrawingBoard).
  29.                 new Thread(this).start();
  30.         }
  31.        
  32.         //Runnable callback method.
  33.         @Override
  34.         public void run() {
  35.                 //Lock the canvas before drawing.
  36.                 Canvas canvas = surfaceHolder.lockCanvas();
  37.                
  38.                 //Perform drawing operations on the canvas.
  39.                 render(canvas);
  40.                
  41.                 //After drawing, unlock the canvas and display it.
  42.                 surfaceHolder.unlockCanvasAndPost(canvas);
  43.         }
  44.  
  45.         private void render(Canvas canvas) {
  46.                 //Fill the entire canvas' bitmap with 'black'.
  47.                 canvas.drawColor(Color.BLACK);
  48.                 //Instantiate a Paint object.
  49.                 Paint paint = new Paint();
  50.                 //Set the paint color to 'white'.
  51.                 paint.setColor(Color.WHITE);
  52.                 //Draw a white circle at position (100, 100) with a radius of 50.
  53.                 canvas.drawCircle(100, 100, 50, paint);
  54.                
  55.         }
  56.  
  57.         //Neither of these two methods are used in this example, however, their definitions are required because SurfaceHolder.Callback was implemented.
  58.         @Override public void surfaceChanged(SurfaceHolder sh, int f, int w, int h) {}
  59.         @Override public void surfaceDestroyed(SurfaceHolder sh) {}
  60.  
  61. }
  62.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
yohosuff
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Mar 20, 2013 3:40 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests