I would like to know how to draw a rectangle in specific position and size.
I tried a couple of trials and examples here and on web, but nothing worked for me.
I
Ok, Thanks to fadden I found the easiest solution(at least for me).
First I created another Surface view and my XML file look like this:
And than I'm created another holder for the new surface, this is a 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);
}
At last 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's 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 clear the canvas every time, if I don't do this, the surface will display in addition more rectangles on each others.
This is how I call to this method from a '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 would be helpfull for someone else.