Android - drawing on SurfaceView not showing up

I just want to draw a square above the camera preview in an android app. The camera preview is displayed on the SurfaceView, and from my reading on the web, it seems that the best way to achieve this is to place another SurfaceView above the one that is displaying the camera feed and use that. So I added this and also added some code, but it doesn't work, I see the camera doesn't work, but the square is never drawn. Just wondering if anyone has any thoughts on what I missed. My DrawFocusRect function gets called, its just my rectangle never appears.

My shallow views in my layout XML file

     <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="vertical" >

        <SurfaceView
            android:id="@+id/full_colour"
            android:layout_width="fill_parent"
            android:layout_height="match_parent" />

        <SurfaceView
            android:layout_alignParentTop="true"
            android:id="@+id/TransparentView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RelativeLayout>

      

My drawing code called from onPreviewFrame

private static void DrawFocusRect()
{
    int hWidth = transparentView.getWidth();
    int hHeight = transparentView.getHeight();

    float f = (float) 0.5;
    float rLeft = (float) Math.floor(((1-f)/2)*hWidth);
    float rRight = (float) Math.floor((((1-f)/2)+f)*hWidth);
    float rTop = (float) Math.floor(((1-f)/2)*hHeight);
    float rBottom = (float) Math.floor((((1-f)/2)+f)*hHeight);

    canvas = holderTranspaernt.lockCanvas();

    canvas.drawColor(Color.GREEN, Mode.CLEAR);
    //border properties
    paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.YELLOW);
    paint.setStrokeWidth(3);
    canvas.drawRect(rLeft, rTop, rRight, rBottom, paint);

    holderTranspaernt.unlockCanvasAndPost(canvas);
}

      

+3


source to share


1 answer


You need to install zOrder to handle ordering of the view drawn on the SurfaceView.

Try setting setZOrderOnTop to view:



transparentView.setZOrderOnTop(true);

      

Hope this helps :)

+6


source







All Articles