private void DrawTriangle(Canvas c, float startAngle, float sweepAngle, Rect bounds) { if (mShowArrow) { if (mArrow == null) { mArrow = new Path(); mArrow.SetFillType(Path.FillType.EvenOdd); } else { mArrow.Reset(); } float x = (float)(RingCenterRadius * Math.Cos(0) + bounds.ExactCenterX()); float y = (float)(RingCenterRadius * Math.Sin(0) + bounds.ExactCenterY()); mArrow.MoveTo(0, 0); mArrow.LineTo((mArrowWidth) * mArrowScale, 0); mArrow.LineTo(((mArrowWidth) * mArrowScale / 2), (mArrowHeight * mArrowScale)); mArrow.Offset(x - ((mArrowWidth) * mArrowScale / 2), y); mArrow.Close(); mArrowPaint.Color = mColors[mColorIndex]; c.Rotate(startAngle + (sweepAngle < 0 ? 0 : sweepAngle) - ARROW_OFFSET_ANGLE, bounds.ExactCenterX(), bounds.ExactCenterY()); c.DrawPath(mArrow, mArrowPaint); } }
protected override void OnDraw(Canvas canvas) { if (Element != null && Element.Text != null) { var density = DeviceDisplay.MainDisplayInfo.Density; float strokeWidth = Convert.ToSingle(Element.StrokeWidth * density); float strokeMiter = 10.0f; var rect = new Android.Graphics.Rect(); this.GetDrawingRect(rect); float progressAngle = 360 * Convert.ToSingle(Element.Progress / Element.Total); float radius = Math.Min(rect.Height(), rect.Width()) / 2 - strokeWidth; RectF circleRect = new RectF( rect.ExactCenterX() - radius, rect.ExactCenterY() - radius, rect.ExactCenterX() + radius, rect.ExactCenterY() + radius ); TextPaint paint = new TextPaint(); paint.Color = Element.TextColor.ToAndroid(); paint.TextSize = Element.TextSize; paint.GetTextBounds(Element.Text.ToString(), 0, Element.Text.ToString().Length, rect); if (((this.Width / 2) - (Element.TextMargin * 4)) < rect.Width()) { float ratio = (float)((this.Width / 2) - Element.TextMargin * 4) / (float)rect.Width(); paint.TextSize = paint.TextSize * ratio; paint.GetTextBounds(Element.Text.ToString(), 0, Element.Text.ToString().Length, rect); } int x = this.Width / 2 - rect.CenterX(); int y = this.Height / 2 - rect.CenterY(); Paint progressPaint = new Paint(PaintFlags.AntiAlias) { StrokeWidth = strokeWidth, StrokeMiter = strokeMiter, Color = Element.ProgressStrokeColor.ToAndroid() }; progressPaint.SetStyle(Paint.Style.Stroke); Paint availablePaint = new Paint(PaintFlags.AntiAlias) { StrokeWidth = strokeWidth, StrokeMiter = strokeMiter, Color = Element.AvailableStrokeColor.ToAndroid() }; availablePaint.SetStyle(Paint.Style.Stroke); Paint paintCircle = new Paint(PaintFlags.AntiAlias) { Color = Element.FillColor.ToAndroid() }; canvas.DrawArc(circleRect, -90, progressAngle, false, progressPaint); canvas.DrawArc(circleRect, -90 + progressAngle, 360 - progressAngle, false, availablePaint); canvas.DrawText(Element.Text.ToString(), x, y, paint); } }
public void Draw(Canvas c, Rect bounds) { RectF arcBounds = mTempBounds; arcBounds.Set(bounds); arcBounds.Inset(mStrokeInset, mStrokeInset); float startAngle = (mStartTrim + mRotation) * 360; float endAngle = (mEndTrim + mRotation) * 360; float sweepAngle = endAngle - startAngle; mPaint.Color = mColors[mColorIndex]; c.DrawArc(arcBounds, startAngle, sweepAngle, false, mPaint); DrawTriangle(c, startAngle, sweepAngle, bounds); if (Alpha < 255) { mCirclePaint.Color = BackgrounColor; mCirclePaint.Alpha = 255 - Alpha; c.DrawCircle(bounds.ExactCenterX(), bounds.ExactCenterY(), bounds.Width() / 2, mCirclePaint); } }
static bool IsWithinCircularBounds(Rect hit, Rect bounds) { if (!bounds.Contains(hit)) return false; // Forumula for a circle: (x-a)2 + (y-b)2 = r2 var centerX = bounds.ExactCenterX() - bounds.Left; var centerY = bounds.ExactCenterY() - bounds.Top; var radius = centerX; double x = hit.Left - bounds.Left, y = hit.Top - bounds.Top, r2 = radius * radius, xx = (x - centerX); var yy = Math.Sqrt(r2 - xx*xx) + 0.5; var upperBound = centerY - yy; var lowerBound = centerY + yy; var result = !Double.IsNaN (y) && y > upperBound && y < lowerBound; return result; }