subclassing SurfaceView and overriding onDraw() to

2019-04-12 10:43发布

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.

3条回答
Ridiculous、
2楼-- · 2019-04-12 11:09

You simply have to add

setWillNotDraw(false)

To the constructor.

And its done..:)

查看更多
ゆ 、 Hurt°
3楼-- · 2019-04-12 11:20
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));
查看更多
做自己的国王
4楼-- · 2019-04-12 11:26

Something fishy is going on. It seems that you're trying to set the SurfaceView as a preview display. In that case, the Activity should implement SurfaceHolder.Callback (not the SurfaceView). This is so that the Activity can know when the SurfaceView has been created and is ready for drawing. Furthermore, it shouldn't even be necessary to extend SurfaceView: you should be able to user SurfaceView itself.

Assuming I misunderstood and you really want to extend surfaceview for some reason... I don't see where you overrode onDraw in your Preview class. Did you not include this code?

When you say that the onDraw callback tells android 'you don't draw the view. I will draw it since I have been implemented', I disagree. onDraw is a function where a View draws stuff that all views have, such as the background. SurfaceView extends view. When you extend SurfaceView, you can override onDraw to tell android to draw stuff in addition to what the original onDraw in the View draws. In other words, Android always does all the drawing; overriding onDraw just tells it to draw a few more things when it's the right time to draw.

查看更多
登录 后发表回答