Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Drawing on SurfaceView not appearing

I simply want to draw a square over the camera preview on my android app. The camera preview displays on a SurfaceView, and from my reading online it seems that the best way to accomplish this was to place another SurfaceView over the one the camera feed displays on and draw on that. So I added this and also added some code but it isn't working, I see the camera feed no problem but the square never gets drawn. Just wondering if anybody had any thoughts on what I have missed. My DrawFocusRect function gets called, its just my rectangle never appears.

My surface 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's 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);
}
like image 848
DukeOfMarmalade Avatar asked Apr 25 '26 09:04

DukeOfMarmalade


1 Answers

You need to set zOrder in order to handle ordering of the view drawn on SurfaceView.

Try setting setZOrderOnTop for your view :

transparentView.setZOrderOnTop(true);

Hope it helps ツ

like image 85
SweetWisher ツ Avatar answered Apr 28 '26 00:04

SweetWisher ツ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!