views:

262

answers:

1

I have subclassed the SurfaceView and instantiating it in onCreate of the Activity. The preview is generated but the control never enters onDraw() which is overriden in the subclass of SurfaceView. Why is that?

class ActivityClass extends Activity{

onCreate(){

mPreview = new Preview(this);
setContentView(mPreview);

}

public void startPreview(){

rec = new MediaRecorder();
rec.setVideoSource();.......
rec.setPreviewDisplay(mPreview.getSurfaceHolder.getSurface());
 } 

}

class Preview extends SurfaceView implements SurfaceHolder.Callback{

SurfaceHolder mHolder;

public Preview(Context context){
super(context);
mHolder = getHolder();     
mHolder.addCallback(this);      
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
bringToFront();//this is not
invalidate();//making a difference

}

 SurfaceHolder getSurfaceHolder(){

return mHolder; }

  //Surface callback methods implemented here
}

Before drawing the preview on the Surface, shouldn't the control be given to the onDraw callback if it is implemented?

Because onDraw callback says to the Android framework 'you don't draw the view. I will draw it since I have been implemented'. Am I right?

Why then, is the control failing to enter onDraw()? Please help.

A: 

public class SelectedRect extends View { public SelectedRect(Context context) { super(context);
bringToFront(); }

    @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    drawLine(canvas);
    invalidate();       
    this.bringToFront();    
}

}

And in activity class : View rect = new SelectedRect(this); rect.bringToFront(); this.addView(rect, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

Nishant Shah
I'm not able to make the suggested changes since my program is as above. Please advice
Namratha
I made the suggested changes-I extended View instead of SurfaceView but in the onCreate() in the Activity, I'm not able to use the foll statement:this.addView(rect, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));error: the method addView(View,LinearLayout.LayoutParams) is undefined for the type myActivity.Please help.
Namratha