protected override void OnSizeChanged(int w, int h, int oldw, int oldh) { base.OnSizeChanged(w, h, oldw, oldh); _length = w > h ? h : w; var thickness = _length / 20; _rectBackground = new RectF(thickness / 2, thickness / 2, _length - thickness / 2, _length - thickness / 2); if (_separateProgressFromItsBackground) { _rectForeground = new RectF(thickness * 3 / 2, thickness * 3 / 2, _length - thickness * 3 / 2, _length - thickness * 3 / 2); } else { _rectForeground = _rectBackground; } _percentagePaint.TextSize = _length / 6; _textPaint.TextSize = _length / 8; _backgroundWidth = thickness; _progressWidth = thickness; _arcPaintBackground.StrokeWidth = _backgroundWidth; _arcPaintProgress.StrokeWidth = _progressWidth; var gradientRadius = _length / 2; var sweepOffsetIndex = GetSweep() / 360.0f; var sweepGradient = new SweepGradient(gradientRadius, gradientRadius, new int[] { Color.Red, Color.Yellow, Color.Yellow, Color.Green }, new[] { 0.0f, 0.375f * sweepOffsetIndex, 0.625f * sweepOffsetIndex, sweepOffsetIndex }); if (_startAngle != 0) { var matrix = new Matrix(); matrix.SetRotate(_startAngle - 0.5f, gradientRadius, gradientRadius); // There is some issues with floating calculations, which affects gradient draw. Thus - 0.5f to hide it. sweepGradient.SetLocalMatrix(matrix); } _arcPaintProgress.SetShader(sweepGradient); if (ArrowDrawable != null) { _arrowBitmap = Bitmap.CreateBitmap(_length, _length, Bitmap.Config.Argb8888); _arrowCanvas = new Canvas(_arrowBitmap); var arrowSize = thickness * 3; var arrowLeftPos = _length - thickness - arrowSize; var arrowTopPos = _length / 2 - arrowSize / 2; if (_separateProgressFromItsBackground) { arrowLeftPos -= thickness; } var arrowRightPos = arrowLeftPos + arrowSize; var arrowBottomPos = arrowTopPos + arrowSize; ArrowDrawable.SetBounds(arrowLeftPos, arrowTopPos, arrowRightPos, arrowBottomPos); ArrowDrawable.Draw(_arrowCanvas); } }
//@SuppressLint("DrawAllocation") //@Override protected override void OnDraw(Canvas canvas) { int centerX = Width / 2; int centerY = Height / 2; // drawing color wheel canvas.DrawBitmap(colorWheelBitmap, centerX - colorWheelRadius, centerY - colorWheelRadius, null); // drawing color view colorViewPaint.Color = Color.HSVToColor(colorHSV); canvas.DrawPath(colorViewPath, colorViewPaint); // drawing value slider float[] hsv = new float[] { colorHSV[0], colorHSV[1], 1f }; SweepGradient sweepGradient = new SweepGradient(centerX, centerY, new int[] { Color.Black, Color.HSVToColor(hsv), Color.White }, null); sweepGradient.SetLocalMatrix(gradientRotationMatrix); valueSliderPaint.SetShader(sweepGradient); canvas.DrawPath(valueSliderPath, valueSliderPaint); // drawing color wheel pointer float hueAngle = (float)Math.ToRadians(colorHSV[0]); int colorPointX = (int)(-Math.Cos(hueAngle) * colorHSV[1] * colorWheelRadius) + centerX; int colorPointY = (int)(-Math.Sin(hueAngle) * colorHSV[1] * colorWheelRadius) + centerY; float pointerRadius = 0.075f * colorWheelRadius; int pointerX = (int)(colorPointX - pointerRadius / 2); int pointerY = (int)(colorPointY - pointerRadius / 2); colorPointerCoords.Set(pointerX, pointerY, pointerX + pointerRadius, pointerY + pointerRadius); canvas.DrawOval(colorPointerCoords, colorPointerPaint); // drawing value pointer valuePointerPaint.Color = Color.HSVToColor(new float[] { 0f, 0f, 1f - colorHSV[2] }); double valueAngle = (colorHSV[2] - 0.5f) * Math.Pi; float valueAngleX = (float)Math.Cos(valueAngle); float valueAngleY = (float)Math.Sin(valueAngle); canvas.DrawLine(valueAngleX * innerWheelRadius + centerX, valueAngleY * innerWheelRadius + centerY, valueAngleX * outerWheelRadius + centerX, valueAngleY * outerWheelRadius + centerY, valuePointerPaint); // drawing pointer arrow if (arrowPointerSize > 0) { drawPointerArrow(canvas); } }