Please check how I did it, you can check from here form myapp how it works
// How I urge you to draw a rectangle
This is SDV.class
public static boolean isDrawing = false; public static float mStartX; public static float mStartY; public static float mx; public static float my; public static void Shape14(float x, float y, float radius) { Path path = new Path(); y -= 2 * radius; radius *= 2; path.moveTo(x + radius, y + radius); path.lineTo(x - radius, y + radius); path.lineTo(x, y); path.lineTo(x + radius, y + radius); float div = (2 * radius) / 5; float top = y + radius; RectF rect1 = new RectF(x + radius / 4, y, x + radius / 1.9f, y + radius); RectF rect2 = new RectF(x + div / 2, top, x + div / 2 + div, top + div * 2); RectF rect3 = new RectF(x - div / 2 - div, top, x - div / 2, top + div * 2); RectF rect4 = new RectF(x - div / 2, top, x + div / 2, top + div); HashMap<String, Object> hm = new HashMap<String, Object>(); hm.put("type", shape14); hm.put("paint", new Paint(DrawingView.mColorPaint)); hm.put("path", path); hm.put("rect1", rect1); hm.put("rect2", rect2); hm.put("rect3", rect3); hm.put("rect4", rect4); al.add(hm); Gmap.mDrawingView.invalidate(); }
Here is our view
public class DrawingView extends View { public static Paint mPaint; public static int mCurrentShape; Point p1, p2, p3, p4; public static Paint mDotedPaint; public static Paint mColorPaint; GoogleMap googleMap; SeekBar sbWidth; public static float sx, sy; public DrawingView(Context context, GoogleMap googleMap, SeekBar sbWidth) { super(context); this.googleMap = googleMap; this.sbWidth = sbWidth; mPaint = new Paint(Paint.DITHER_FLAG); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setColor(SDV.colorChoosen); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(SDV.width); mDotedPaint = new Paint(Paint.DITHER_FLAG); mDotedPaint.setAntiAlias(true); mDotedPaint.setDither(true); mDotedPaint.setColor(SDV.colorChoosen); mDotedPaint.setStyle(Paint.Style.STROKE); mDotedPaint.setStrokeJoin(Paint.Join.ROUND); mDotedPaint.setStrokeCap(Paint.Cap.ROUND); mDotedPaint.setStrokeWidth(SDV.width); mColorPaint = new Paint(Paint.DITHER_FLAG); mColorPaint.setAntiAlias(true); mColorPaint.setDither(true); mColorPaint.setFilterBitmap(true); mColorPaint.setStyle(Paint.Style.FILL); mColorPaint.setStrokeWidth(SDV.width); mColorPaint.setColor(SDV.colorChoosen); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); sx = super.getWidth() * 0.5f; sy = super.getHeight() * 0.5f; if (SDV.isDrawing) { new OnGoingDrawings().HandleAllOnGoingDrawings(mCurrentShape, canvas); } else { new ShapeDrawer().DrawEverything(canvas, googleMap, sbWidth); } } @Override public boolean onTouchEvent(MotionEvent event) { SDV.mx = event.getX(); SDV.my = event.getY(); switch (mCurrentShape) { case SDV.shape14: TouchEvents.Shape14(event); break; return true; }
}
Here is a touch listener
public static void Shape14(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: SDV.isDrawing = true; SDV.mStartX = SDV.mx; SDV.mStartY = SDV.my; Gmap.mDrawingView.invalidate(); break; case MotionEvent.ACTION_MOVE: Gmap.mDrawingView.invalidate(); break; case MotionEvent.ACTION_UP: SDV.isDrawing = false; float x = SDV.mStartX, y = SDV.mStartY; float DifX = Math.abs(SDV.mx - SDV.mStartX); float DifY = Math.abs(SDV.my - SDV.mStartY); float radius; if (DifY > DifX) radius = Math.abs(SDV.my - SDV.mStartY); else radius = Math.abs(SDV.mx - SDV.mStartX); SDV.Shape14(x, y, radius); break; } }