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);
            }
        }
Пример #2
0
        private void DrawRing(Canvas c)
        {
            var sc = c.Save();

            c.Rotate(-this.Degree, c.Width / 2, c.Height / 2);

            //中心点
            var cx = c.Width / 2;
            var cy = c.Height / 2;

            //用两个圆 Path 构造一个 圆环显示区域, 即挖空内圆
            var pInner = new Path();

            pInner.AddCircle(cx, cy, this.BitmapRadius + SPACE / 2, Path.Direction.Cw);
            var pOut = new Path();

            pOut.AddCircle(cx, cy, this.Radius, Path.Direction.Cw);

            c.ClipPath(pOut);
            c.ClipPath(pInner, Region.Op.Difference);

            //var color = new Color((int)(DateTime.Now.Ticks % 0xFFFFFFFF));
            //c.DrawColor(color);

            //用角度渐变填充外圆的范围
            var g     = new SweepGradient(cx, cy, Color.Green, Color.Transparent);
            var paint = new Paint();

            paint.SetShader(g);
            c.DrawCircle(cx, cy, this.Radius, paint);

            c.RestoreToCount(sc);
        }
Пример #3
0
        private Bitmap createColorWheelBitmap(int width, int height)
        {
            Bitmap bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);

            int colorCount     = 12;
            int colorAngleStep = 360 / 12;

            int[]   colors = new int[colorCount + 1];
            float[] hsv    = new float[] { 0f, 1f, 1f };
            for (int i = 0; i < colors.Length; i++)
            {
                hsv[0]    = (i * colorAngleStep + 180) % 360;
                colors[i] = Color.HSVToColor(hsv);
            }
            colors[colorCount] = colors[0];

            SweepGradient sweepGradient = new SweepGradient(width / 2, height / 2, colors, null);

            RadialGradient radialGradient = new RadialGradient(width / 2, height / 2, colorWheelRadius, new Color(0xFFFFFF), new Color(0x00FFFFFF), Shader.TileMode.Clamp);
            ComposeShader  composeShader  = new ComposeShader(sweepGradient, radialGradient, PorterDuff.Mode.SrcOver);

            colorWheelPaint.SetShader(composeShader);

            Canvas canvas = new Canvas(bitmap);

            canvas.DrawCircle(width / 2, height / 2, colorWheelRadius, colorWheelPaint);

            return(bitmap);
        }
Пример #4
0
        public void DrawPieChart(float average, float sum, Color start, Color end, float width = 0.13f,
                                 float radius = -1, bool text = true)
        {
            float Cx          = _width / 2f,
                  Cy          = _height / 2f,
                  strokeWidth = width * (_width <= _height ? _width : _height);

            radius = radius <= 0 ? (_width <= _height ? _width / 2f : _height / 2f) - strokeWidth : radius;

            Shader shader    = new SweepGradient(Cx, Cy, end, start);
            var    paintMain = new Paint {
                Color = start, StrokeWidth = strokeWidth, AntiAlias = true
            };
            var paintBack = new Paint {
                Color = Color.Rgb(29, 43, 59), StrokeWidth = strokeWidth, AntiAlias = true
            };

            paintMain.SetStyle(Paint.Style.Stroke);
            paintMain.SetShader(shader);
            paintBack.SetStyle(Paint.Style.Stroke);

            Canvas.DrawArc(new RectF(Cx - radius, Cy - radius, Cx + radius, Cy + radius), 0f, 360f, false, paintBack);
            Canvas.Rotate(-90f, Cx, Cy);
            Canvas.DrawArc(new RectF(Cx - radius, Cy - radius, Cx + radius, Cy + radius), 0.5f,
                           360f - average * (360f / sum), false, paintMain);
            Canvas.Rotate(90f, Cx, Cy);
            if (text)
            {
                DrawText(_width * 20f / 100f, $"{RoundOfNumber(100 - average * 100f / sum)}%", 2.4f * _width / 10f,
                         Cy - 33f * radius / 100);
            }
        }
Пример #5
0
        //@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);
            }
        }
            internal ColorPickerView(ColorPickerDialog outerInstance, Context c, OnColorChangedListener l, int color) : base(c)
            {
                this.outerInstance = outerInstance;
                mListener          = l;
                mColors            = new int[] { unchecked ((int)0xFFFF0000), unchecked ((int)0xFFFF00FF), unchecked ((int)0xFF0000FF), unchecked ((int)0xFF00FFFF), unchecked ((int)0xFF00FF00), unchecked ((int)0xFFFFFF00), unchecked ((int)0xFFFF0000) };
                Shader s = new SweepGradient(0, 0, mColors, null);

                mPaint             = new Paint(Paint.ANTI_ALIAS_FLAG);
                mPaint.Shader      = s;
                mPaint.Style       = Paint.Style.STROKE;
                mPaint.StrokeWidth = 32;

                mCenterPaint             = new Paint(Paint.ANTI_ALIAS_FLAG);
                mCenterPaint.Color       = color;
                mCenterPaint.StrokeWidth = 5;
            }
            public ColorPickerView(Context c, Color color)
                : base(c)
            {
                _colors = new[] {
                    new Color(255, 0, 0, 255).ToArgb(), new Color(255, 0, 255, 255).ToArgb(), new Color(0, 0, 255, 255).ToArgb(),
                    new Color(0, 255, 255, 255).ToArgb(), new Color(0, 255, 0, 255).ToArgb(), new Color(255, 255, 0, 255).ToArgb(),
                    new Color(255, 0, 0, 255).ToArgb()
                };
                Shader s = new SweepGradient(0, 0, _colors, null);

                _paint = new Paint(PaintFlags.AntiAlias);
                _paint.SetShader(s);
                _paint.SetStyle(Paint.Style.Stroke);
                _paint.StrokeWidth = 32;

                _centerPaint = new Paint(PaintFlags.AntiAlias)
                {
                    Color       = color,
                    StrokeWidth = 5
                };
            }
Пример #8
0
            public ColorPickerView(Context c, OnColorChangedListener l, int color) : base(c)
            {
                mListener = l;
                mColors   = new int[] {
                    Color.White.ToArgb(),
                        Color.Black.ToArgb(),
                        Color.Red.ToArgb(),
                        Color.Yellow.ToArgb(),
                        Color.Pink.ToArgb(),
                        Color.Green.ToArgb(),
                        Color.Purple.ToArgb(),
                        Color.Blue.ToArgb()
                };
                Shader s = new SweepGradient(0, 0, mColors, null);

                mPaint = new Paint(PaintFlags.AntiAlias);
                mPaint.SetShader(s);
                mPaint.SetStyle(Paint.Style.Stroke);
                mPaint.StrokeWidth = 32;

                mCenterPaint             = new Paint(PaintFlags.AntiAlias);
                mCenterPaint.Color       = new Color(color);
                mCenterPaint.StrokeWidth = 5;
            }
Пример #9
0
        private void init(IAttributeSet attrs, int defStyle)
        {
            TypedArray a = Context.ObtainStyledAttributes(attrs,
                                                          Resource.Styleable.ColorPicker, defStyle, 0);
            Resources b = Context.Resources;

            mColorWheelThickness = a.GetDimensionPixelSize(
                Resource.Styleable.ColorPicker_color_wheel_thickness,
                b.GetDimensionPixelSize(Resource.Dimension.color_wheel_thickness));
            mColorWheelRadius = a.GetDimensionPixelSize(
                Resource.Styleable.ColorPicker_color_wheel_radius,
                b.GetDimensionPixelSize(Resource.Dimension.color_wheel_radius));
            mPreferredColorWheelRadius = mColorWheelRadius;
            mColorCenterRadius         = a.GetDimensionPixelSize(
                Resource.Styleable.ColorPicker_color_center_radius,
                b.GetDimensionPixelSize(Resource.Dimension.color_center_radius));
            mPreferredColorCenterRadius = mColorCenterRadius;
            mColorCenterHaloRadius      = a.GetDimensionPixelSize(
                Resource.Styleable.ColorPicker_color_center_halo_radius,
                b.GetDimensionPixelSize(Resource.Dimension.color_center_halo_radius));
            mPreferredColorCenterHaloRadius = mColorCenterHaloRadius;
            mColorPointerRadius             = a.GetDimensionPixelSize(
                Resource.Styleable.ColorPicker_color_pointer_radius,
                b.GetDimensionPixelSize(Resource.Dimension.color_pointer_radius));
            mColorPointerHaloRadius = a.GetDimensionPixelSize(
                Resource.Styleable.ColorPicker_color_pointer_halo_radius,
                b.GetDimensionPixelSize(Resource.Dimension.color_pointer_halo_radius));

            mShowCenterOldColor = a.GetBoolean(Resource.Styleable.ColorPicker_color_center_show_old_color, false);

            a.Recycle();

            mAngle = (float)(-Math.PI / 2);

            Shader s = new SweepGradient(0, 0, COLORS, null);

            mColorWheelPaint = new Paint(PaintFlags.AntiAlias);
            mColorWheelPaint.SetShader(s);
            mColorWheelPaint.SetStyle(Paint.Style.Stroke);
            mColorWheelPaint.StrokeWidth = (mColorWheelThickness);

            mPointerHaloPaint       = new Paint(PaintFlags.AntiAlias);
            mPointerHaloPaint.Color = (Color.Black);
            mPointerHaloPaint.Alpha = (0x50);

            mPointerColor       = new Paint(PaintFlags.AntiAlias);
            mPointerColor.Color = new Color(calculateColor(mAngle));

            mCenterNewPaint       = new Paint(PaintFlags.AntiAlias);
            mCenterNewPaint.Color = new Color(calculateColor(mAngle));
            mCenterNewPaint.SetStyle(Paint.Style.Fill);

            mCenterOldPaint       = new Paint(PaintFlags.AntiAlias);
            mCenterOldPaint.Color = new Color(calculateColor(mAngle));
            mCenterOldPaint.SetStyle(Paint.Style.Fill);

            mCenterHaloPaint       = new Paint(PaintFlags.AntiAlias);
            mCenterHaloPaint.Color = (Color.Black);
            mCenterHaloPaint.Alpha = (0x00);

            mCenterNewColor = calculateColor(mAngle);
            mCenterOldColor = calculateColor(mAngle);
        }
			internal ColorPickerView(ColorPickerDialog outerInstance, Context c, OnColorChangedListener l, int color) : base(c)
			{
				this.outerInstance = outerInstance;
				mListener = l;
				mColors = new int[]{unchecked((int)0xFFFF0000), unchecked((int)0xFFFF00FF), unchecked((int)0xFF0000FF), unchecked((int)0xFF00FFFF), unchecked((int)0xFF00FF00), unchecked((int)0xFFFFFF00), unchecked((int)0xFFFF0000)};
				Shader s = new SweepGradient(0, 0, mColors, null);

				mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
				mPaint.Shader = s;
				mPaint.Style = Paint.Style.STROKE;
				mPaint.StrokeWidth = 32;

				mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
				mCenterPaint.Color = color;
				mCenterPaint.StrokeWidth = 5;
			}