Newb--Q: HOW to use SURFACEVIEW

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Newb--Q: HOW to use SURFACEVIEW

Postby doubleslash » Tue Feb 17, 2009 7:06 am

I'm trying to learn SurfaceView by using it in a very simple program. It will try to draw a blue circle fitting the entire screen. I have the draw command running on the main thread inside surfaceCreated and surfaceChanged methods. I don't know what I should do within the surfaceDestroyed method, so I just leave it blank. Does the program not work because of this? If so, how can I fix it?
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
  2.  
  3.  
  4.  
  5.  private SurfaceHolder mholder;
  6.  
  7.  private Paint mbrush;
  8.  
  9.   public MySurfaceView(Context context) {
  10.  
  11.     super(context);
  12.  
  13.     init();
  14.  
  15.   }
  16.  
  17.  
  18.  
  19.   public MySurfaceView(Context context, AttributeSet ats, int defaultStyle){
  20.  
  21.           super(context, ats, defaultStyle);
  22.  
  23.           init();
  24.  
  25.   }
  26.  
  27.   public MySurfaceView(Context context, AttributeSet ats){
  28.  
  29.           super(context, ats);
  30.  
  31.           init();
  32.  
  33.   }
  34.  
  35.  private void init() {
  36.  
  37.     // Create a new SurfaceHolder and assign this class as its callback.
  38.  
  39.     mholder = getHolder();
  40.  
  41.     mholder.addCallback(this);
  42.  
  43.     mholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  44.  
  45.     mbrush.setColor(Color.BLUE);
  46.  
  47.     }
  48.  
  49.     public void surfaceCreated(SurfaceHolder holder) {
  50.  
  51.         mholder = holder;
  52.  
  53.         Canvas c = mholder.lockCanvas();
  54.  
  55.         int h = this.getHeight();
  56.  
  57.         int w = this.getWidth();
  58.  
  59.         c.drawCircle(w/2, h/2, Math.min(w/2, h/2), mbrush);
  60.  
  61.         mholder.unlockCanvasAndPost(c);
  62.  
  63.     }
  64.  
  65.  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  66.  
  67.           mholder = holder;
  68.  
  69.           Canvas c = mholder.lockCanvas();
  70.  
  71.           c.drawCircle(w/2, h/2, Math.min(w/2, h/2), mbrush);
  72.  
  73.           mholder.unlockCanvasAndPost(c);
  74.  
  75.   }
  76.  
  77. //BLANK--problem?
  78.  
  79.     public void surfaceDestroyed(SurfaceHolder holder) {
  80.  
  81.         ;
  82.  
  83.     }
  84.  
  85. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

Thanks for looking.
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Top

Postby MrSnowflake » Tue Feb 17, 2009 12:43 pm

Look at the LunarLander demo in <sdk>/samples.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby doubleslash » Tue Feb 17, 2009 6:10 pm

The LunarLander is a bit complicated for a beginner. It has a lot of other stuff besides using the SurfaceView. Does anyone know of a simpler example of using thread and SurfaceView? I found
http://code.google.com/android/samples/ ... eView.html
but I don't understand
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. synchronized(this){
  2.  
  3. //some code
  4.  
  5. //this is a reference to a Thread object
  6.  
  7. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

So it was of limited help. Thanks for looking.
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Postby MrSnowflake » Tue Feb 17, 2009 6:33 pm

How about the tutorial I wrote some time ago.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby doubleslash » Wed Feb 18, 2009 5:08 am

In your GameTemplate tutorial, I also found
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. synchronized (mSurfaceHolder) {
  2.  
  3.                 mCanvasWidth = width;
  4.  
  5.                 mCanvasHeight = height;
  6.  
  7.             }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

Since I'm new to Android and Java too, I have never seen that kind of statement before. Could you please explain what it does?
Thanks
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Postby doubleslash » Wed Feb 18, 2009 6:24 am

Never mind my previous question. I found a Java book to explain that.
Thanks
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Top

Postby MrSnowflake » Wed Feb 18, 2009 8:56 am

I already wanted to say: If you have such question, Google is there to answer (also there are good Java tutorials on sun.com)
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Same problem for me

Postby harshadha » Wed Sep 16, 2009 1:47 pm

Hai doubleslash,

I also wanted to do small application which takes two images and when one is displaying other should buffer,in surfaceview.I used your code iam also getting only black screen.Can you please tell me how to solve it.

Thanks in Advance
Regards,
Lavanya S.R
harshadha
Freshman
Freshman
 
Posts: 9
Joined: Mon Jan 21, 2008 8:01 am
Location: India

Postby doubleslash » Wed Sep 16, 2009 3:34 pm

Not sure what exactly you want to do. If you just want to display 2 set images, you should use ImageView instead. Anyway, if you take out mHolder = holder in SurfaceChanged, it should work.
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot], Yahoo [Bot] and 5 guests