Drawing a rectangle on SurfaceView

I would like to know how to draw a rectangle in a specific position and size.

I tried a couple of samples and examples here and on the Internet, but nothing worked for me.

I use SurfaceView and SurfaceHolder to preview the camera.

I just want to draw a rectangle when the user touches the screen.

I do not want to use an external library, and when I try to get the canvas from the holder, I get zero.

There is no code yet, because nothing works for me, if someone knows how to do this, I will try.

This is what I have already tried:

Link 1

Link 2

Link 3

Thanks for the help!

+5
source share
1 answer

Well, thanks to fadden, I found the easiest solution (at least for me).

First I created another Surface view, and my XML file looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:opencv="http://schemas.android.com/apk/res-auto" android:layout_gravity="left|top"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <SurfaceView android:layout_alignParentTop="true" android:id="@+id/CameraView" android:layout_width="match_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> </RelativeLayout> 

And than I create another holder for a new surface, this is part of my java code:

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video_capture); // Create first surface with his holder(holder) cameraView = (SurfaceView)findViewById(R.id.CameraView); cameraView.setOnTouchListener(onTouchListner); holder = cameraView.getHolder(); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Create second surface with another holder (holderTransparent) transparentView = (SurfaceView)findViewById(R.id.TransparentView); holderTransparent = transparentView.getHolder(); holderTransparent.setFormat(PixelFormat.TRANSPARENT); holderTransparent.addCallback(callBack); holderTransparent.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } 

Finally, I implement a method called 'DrawFocusRect'

 private void DrawFocusRect(float RectLeft, float RectTop, float RectRight, float RectBottom, int color) { canvas = holderTransparent.lockCanvas(); canvas.drawColor(0,Mode.CLEAR); //border properties paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setColor(color); paint.setStrokeWidth(3); canvas.drawRect(RectLeft, RectTop, RectRight, RectBottom, paint); holderTransparent.unlockCanvasAndPost(canvas); } 

As you can see, I clean the canvas every time, if I do not, the surface will display additionally more rectangles on top of each other.

This is how I call this method from the OnTouchListener event:

 OnTouchListener onTouchListner = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { RectLeft = event.getX() - 100; RectTop = event.getY() - 100 ; RectRight = event.getX() + 100; RectBottom = event.getY() + 100; DrawFocusRect(RectLeft , RectTop , RectRight , RectBottom , Color.BLUE); } }; 

Hope this will be helpful to someone else.

+6
source

Source: https://habr.com/ru/post/1202851/


All Articles