protected override void OnDraw(Canvas canvas) { // Background Bitmap to Cover all Area canvas.DrawBitmap(mBitmap, null, mMeasuredRect, null); // Just draw the points only if it has already been initiated if (points[3] != null) { int left, top, right, bottom; left = points[0].X; top = points[0].Y; right = points[0].X; bottom = points[0].Y; // Sets the circles' locations for (int i = 1; i < points.Length; i++) { left = left > points[i].X ? points[i].X : left; top = top > points[i].Y ? points[i].Y : top; right = right < points[i].X ? points[i].X : right; bottom = bottom < points[i].Y ? points[i].Y : bottom; } mRectPaint.AntiAlias = true; mRectPaint.Dither = true; mRectPaint.StrokeJoin = Paint.Join.Round; mRectPaint.StrokeWidth = 5; mRectPaint.SetStyle(Paint.Style.Stroke); mRectPaint.Color = Color.ParseColor("#0079A3"); canvas.DrawRect( left + circles[0].GetCircleWidth() / 2, top + circles[0].GetCircleWidth() / 2, right + circles[2].GetCircleWidth() / 2, bottom + circles[2].GetCircleWidth() / 2, mRectPaint); // Fill The Rectangle mRectPaint.SetStyle(Paint.Style.Fill); mRectPaint.Color = Color.ParseColor("#B2D6E3"); mRectPaint.Alpha = 75; mRectPaint.StrokeWidth = 0; canvas.DrawRect( left + circles[0].GetCircleWidth() / 2, top + circles[0].GetCircleWidth() / 2, right + circles[2].GetCircleWidth() / 2, bottom + circles[2].GetCircleWidth() / 2, mRectPaint); // DEBUG mRectPaint.Color = Color.Red; mRectPaint.TextSize = 18; mRectPaint.StrokeWidth = 0; // Draw every circle on the right position for (int i = 0; i < circles.Count(); i++) { ResizeCircle circle = circles[i]; float x = circle.GetX(); float y = circle.GetY(); canvas.DrawBitmap(circle.GetBitmap(), x, y, mRectPaint); // DEBUG // canvas.DrawText ("" + (i + 1), circle.GetX (), circle.GetY (), mRectPaint); } } }
private int getTouchedCircle(int xTouch, int yTouch) { int groupId = -1; for (int i = 0; i < circles.Count; i++) { ResizeCircle circle = circles[i]; // Check if the touch was inside the bounds of the circle int centerX = circle.GetX() + circle.GetCircleWidth(); int centerY = circle.GetY() + circle.GetCircleHeight(); // Calculate the radius from the touch to the center of the circle double radCircle = Math.Sqrt((double)(((centerX - xTouch) * (centerX - xTouch)) + (centerY - yTouch) * (centerY - yTouch))); // If the touch was on one of the circles if (radCircle < circle.GetCircleWidth()) { circleId = circle.GetID(); if (circleId == 1 || circleId == 3) { groupId = 2; break; } else { groupId = 1; break; } } else { // User didn't touch any of the circles nor the inside area groupId = -1; } } // If the touch wasn't on one of the circles, check if it was inside the rectangle if (groupId == -1) { List <int> xCoords = new List <int>(); List <int> yCoords = new List <int>(); // Gather Coordinates from all circles foreach (ResizeCircle circle in circles) { xCoords.Add(circle.GetX()); yCoords.Add(circle.GetY()); } // Store the max and min coordinates int minX = xCoords.Min(); int maxX = xCoords.Max(); int minY = yCoords.Min(); int maxY = yCoords.Max(); // Check if user has touched inside the rectangle if ((xTouch > minX && xTouch < maxX) && (yTouch > minY && yTouch < maxY)) { // User has touched inside the Rectangle groupId = 0; } } return(groupId); }