paland3
paland3

Reputation: 61

How to update a surfaceview encapsulated in a Framelayout?

i have a surfaceview update problem. I would like to create a lightweight augmented reality app, so i made a framelayout with camerapreview in the back, and an extended surfaceview on the front.

alParent = new FrameLayout(this);
        alParent.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));

        // Create a new camera view and add it to the layout
        cv = new CameraPreview(this,c);
        alParent.addView(cv);

        // Create a new draw view and add it to the layout
        dv = new DrawView(this);
        dv.reDraw();
        alParent.addView(dv);

After that, i am able to draw a circle on the camera picture, and that is great:

public class DrawView extends SurfaceView

{

private Paint textPaint = new Paint();
int x;
int y;
public DrawView(Context context) {

    super(context);

    // Create out paint to use for drawing
    textPaint.setARGB(255, 255, 255, 10);
    textPaint.setTextSize(60);
    x=100;
    y=100;
    /* This call is necessary, or else the 
     * draw method will not be called. 
     */
    setWillNotDraw(false);
}

@Override
protected void onDraw(Canvas canvas){
    // A Simple Text Render to test the display
    canvas.drawText("Sunfinder", 50, 50, textPaint);
    canvas.drawCircle(x, y, 30, textPaint);
}

the problem is, that i would like to change x, and y, and redraw the circle often. I already know i have to use something like this:

protected void reDraw()
{
    x+=10;
    SurfaceHolder surfaceHolder;
    surfaceHolder = getHolder();
     if(surfaceHolder.getSurface().isValid())
    {onDraw(surfaceHolder.lockCanvas());}
     else System.out.println("problem");
}

(Note this is an example just to test if the circle moves) My question is how and where to call this function? I think it is complicated caused by the framelayout.

If i call it in the activity i coipied first (dv.redraw) i got a null pointer exception. Anyway, is it ok to have a while loop in the activity to recall this function all the time? i know it would be better in a thread, but the frame layout mess it up. Thank you for reading and for any help!

Upvotes: 1

Views: 1943

Answers (1)

paland3
paland3

Reputation: 61

I found a solution:

I have to use the invalidate() function, and override the ondraw() method.

So the following link will redraw and update the screen. It changes nothing that cv is a in a Framelayout.

public void onSensorChanged(SensorEvent event)
{ 
    dv.invalidate(); 
}

Hope it helps someone!

Upvotes: 1

Related Questions