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
I customize a new surface and using onTouchEvent
and draw my rectangle:
public class MySurface extends SurfaceView {
private Paint mPaint;
private Path mPath;
private Canvas mCanvas;
private SurfaceHolder mSurfaceHolder;
private float mX, mY, newX, newY;
public MSurface(Context context) {
super(context);
initi(context);
}
private void initi(Context context) {
mSurfaceHolder = getHolder();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setTextSize(12);
mPaint.setColor(Color.RED);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mX = event.getX();
mY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
newX = event.getX();
newY = event.getY();
break;
default:
// Do nothing
}
drawRect();
invalidate();
return true;
}
for drawing your rectangle use this function:
private void drawRect() {
mPath = new Path();
mPath.moveTo(mX, mY);
mCanvas = mSurfaceHolder.lockCanvas();
mCanvas.save();
mPath.addRect(mX, mY, newX, newY, Path.Direction.CCW);
mCanvas.drawPath(mPath, mPaint);
mCanvas.restore();
mSurfaceHolder.unlockCanvasAndPost(mCanvas);
mX = newX;
mY = newY;
}
}
Now you can use this customized surface in your XML files as below: