Ejemplo n.º 1
0
        private void Draw(ImageView imageView, int times)
        {
            var start = DateTime.Now;
            PixelsBuffer buffer = new PixelsBuffer(600, 600);
            IDrawer drawer = new Drawer(buffer);
            drawer.Clear(Colors.Transparent);
            //create fill for drawing
            Fill fill = new Fill(Colors.Transparent);
            //fill.Opacity = 0.3;
            //draw content
            var coords1 = GetPath(1);
            var coords2 = GetPath(0);
            var coords3 = GetPath(2);

            var path = GetPath();
            var startDraw = DateTime.Now;
            int step = 5;
            for (int i = 0; i < times; i++)
            {
                //drawer.DrawRectangle(fill, 10, 10, 300, 300);
                //drawer.DrawEllipse(fill, 200, 200, 120, 200);
                //drawer.DrawPolygon(fill, coords1);
                //drawer.DrawPolygon(fill, coords2);
                //drawer.DrawPolygon(fill, coords3);

                //draw content
                //drawer.Rotate(15);
                //drawer.Scale(0.3, 0.3);
                //drawer.DrawPath(fill, path);

                var margin = i / 10 * step;

                PixelsBuffer view = buffer.CreateView(margin, margin, buffer.Width - margin * 2, buffer.Height - margin * 2, true);
                DrawFrame(view, Colors.OrangeRed);
                DrawLine(view, Colors.Olive);

            }
            DrawLion(buffer, 200, 200);

            if (bmp != null)
                bmp.Dispose();
            bmp = BufferToBitmap.GetBitmap(buffer);

            //show to screen
            var icon = BitmapFactory.DecodeResource(this.Resources, Resource.Drawable.Icon);

            imageView.SetImageBitmap(BufferToBitmap.Overlay(new Bitmap[] { icon, bmp }));
            Android.Util.Log.Debug("Draw " + times, "Draw: " + (DateTime.Now - startDraw).TotalSeconds.ToString() + " Total: " + (DateTime.Now - start).TotalSeconds.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Render an ellipse
        /// </summary>
        /// <param name="stroke">The stroke style to draw border</param>
        /// <param name="fill">The fill style to render inner region</param>
        /// <param name="centerX">X-axis coordinate of the center point</param>
        /// <param name="centerY">Y-axis coordinate of the center point</param>
        /// <param name="radiusX">horizontal radius of the ellipse</param>
        /// <param name="radiusY">vertical radius of the ellipse</param>
        /// <remarks>The stroke and the fill can both be a null reference (Nothing in Visual Basic). If the stroke is null, no stroke is performed. If the fill is null, no fill is performed.</remarks>
        public void DrawEllipse(Fill fill, double centerX, double centerY, double radiusX, double radiusY)
        {
            #region Generate coordinate from a ellipse data
            //double mDa = Math.Acos((radiusX + radiusY) / (radiusX + radiusY + 0.4)) * 2.0;

            #region HACKED by HaiNM - 2008 Dec 15
            //To fixed the bug where current local space is too small compared to world space,
            //the ellipse increment step is too big -> not smooth
            double mDa;
            if (currentTransform.IsTransformed)
            {
                double scaleX = Math.Abs(currentTransform.ScaleXFactor);
                double scaleY = Math.Abs(currentTransform.ScaleYFactor);
                mDa = Math.Acos((radiusX * scaleX + radiusY * scaleY) / (radiusX * scaleX + radiusY * scaleY + 0.4)) * 1.25;
            }
            else mDa = Math.Acos((radiusX + radiusY) / (radiusX + radiusY + 0.4)) * 2.0;

            #endregion

            double startAngle = mDa, endAngle = 6.28318;//Math.PI * 2.0 - mDa

            double[] coordinates = new double[((int)(endAngle / mDa) + 2) << 1];
            int i = 2;
            coordinates[0] = centerX + radiusX;
            coordinates[1] = centerY;
            while (startAngle < endAngle)
            {
                coordinates[i] = centerX + Math.Cos(startAngle) * radiusX;
                coordinates[i + 1] = centerY + Math.Sin(startAngle) * radiusY;
                i += 2;
                startAngle += mDa;
            }
            coordinates[i] = coordinates[0];
            coordinates[i + 1] = centerY;

            #endregion
            DrawPolygon(fill, coordinates);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Render a rounded rectangle from top-left point (x, y) with the specified width and height
        /// </summary>
        /// <param name="stroke">The stroke style to draw border</param>
        /// <param name="fill">The fill style to render inner region</param>
        /// <param name="x">X-axis of starting point</param>
        /// <param name="y">Y-axis of starting point</param>
        /// <param name="width">vertical size</param>
        /// <param name="height">horizontal size</param>
        /// <param name="radiusX">The radius in the x dimension of the rounded corners. This value will be clamped to the range of 0 to width/2</param>
        /// <param name="radiusY">The radius in the y dimension of the rounded corners. This value will be clamped to the range of 0 to width/2</param>
        /// <remarks>The stroke and the fill can both be a null reference (Nothing in Visual Basic). If the stroke is null, no stroke is performed. If the fill is null, no fill is performed.</remarks>
        public void DrawRoundedRectangle(Fill fill, double x, double y, double width, double height, double radiusX, double radiusY)
        {
            double x2 = x + width;
            double y2 = y + height;

            #region calculate number of points which need to draw
            #region calculate Da
            double ra = (radiusX + radiusY);
            double mDa = Math.Acos(ra / (ra + 0.5)) * 2.0;
            #endregion
            double[] coordinates = new double[((int)(Math.PI / (2 * mDa)) << 3) + 18];
            #endregion

            #region change rounded rectangle to coordinate polygon

            double startAngle, endAngle;
            double centerX, centerY;
            int i = 2;

            #region start point
            coordinates[0] = x;
            coordinates[1] = y + radiusY;
            #endregion

            #region top-left arc
            startAngle = 3.14159 - mDa; // Math.PI - mDa
            endAngle = 1.570796;// Math.PI/2
            centerX = x + radiusX;
            centerY = y + radiusY;
            while (startAngle > endAngle)
            {
                coordinates[i] = centerX + Math.Cos(startAngle) * radiusX;
                coordinates[i + 1] = centerY - Math.Sin(startAngle) * radiusY;
                i += 2;
                startAngle -= mDa;
            }
            coordinates[i] = x + radiusX;
            coordinates[i + 1] = y;
            i += 2;
            #endregion

            #region top-right arc
            startAngle = 1.570796;// Math.PI / 2
            centerX = x2 - radiusX;
            centerY = y + radiusY;
            while (startAngle > 0)
            {
                coordinates[i] = centerX + Math.Cos(startAngle) * radiusX;
                coordinates[i + 1] = centerY - Math.Sin(startAngle) * radiusY;
                i += 2;
                startAngle -= mDa;
            }
            coordinates[i] = x2;
            coordinates[i + 1] = y + radiusY;
            i += 2;

            #endregion

            #region bottom-right arc
            startAngle = 0;
            endAngle = -1.570796; // -Math.PI / 2
            centerX = x2 - radiusX;
            centerY = y2 - radiusY;
            while (startAngle > endAngle)
            {
                coordinates[i] = centerX + Math.Cos(startAngle) * radiusX;
                coordinates[i + 1] = centerY - Math.Sin(startAngle) * radiusY;
                i += 2;
                startAngle -= mDa;
            }
            coordinates[i] = x2 - radiusX;
            coordinates[i + 1] = y2;
            i += 2;
            #endregion

            #region bottom-left arc
            startAngle = -1.570796;// - Math.PI/2
            endAngle = -3.14159; // -Math.PI
            centerX = x + radiusX;
            centerY = y2 - radiusY;
            while (startAngle > endAngle)
            {
                coordinates[i] = centerX + Math.Cos(startAngle) * radiusX;
                coordinates[i + 1] = centerY - Math.Sin(startAngle) * radiusY;
                i += 2;
                startAngle -= mDa;
            }
            coordinates[i] = x;
            coordinates[i + 1] = y2 - radiusY;
            #endregion

            #region left edge
            coordinates[i + 2] = x;
            coordinates[i + 3] = y + radiusY;
            #endregion

            #endregion

            //drawing rounded rectangle
            DrawPolygon(fill, coordinates);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Render a rectangle from top-left point (x, y) with the specified width and height
        /// </summary>
        /// <param name="stroke">The stroke style to draw border</param>
        /// <param name="fill">The fill style to render inner region</param>
        /// <param name="x">X-axis of starting point</param>
        /// <param name="y">Y-axis of starting point</param>
        /// <param name="width">vertical size</param>
        /// <param name="height">horizontal size</param>
        /// <remarks>The stroke and the fill can both be a null reference (Nothing in Visual Basic). If the stroke is null, no stroke is performed. If the fill is null, no fill is performed.</remarks>
        public void DrawRectangle(Fill fill, double x, double y, double width, double height)
        {
            //calculate coordinates of a rectangle
            double[] coordinates = new double[]
            {
                x, y,
                x + width, y,
                x + width, y+height,
                x, y+height,
                x, y
            };

            DrawPolygon(fill, coordinates);
        }
Ejemplo n.º 5
0
        Fill GetLinearBackwardFill()
        {
            //Create a linear gradient paint with
            // + mode = forward diagonal
            // + start point: (0,0)
            // + end point: (w/2, h/2)

            LinearGradient paint = new LinearGradient();
            paint.StartX = 0;
            paint.StartY = 0;
            paint.EndX = w / 2;
            paint.EndY = h / 2;
            paint.Ramp = GetColorRamp();
            paint.Mode = LinearGradientMode.BackwardDiagonal;
            paint.Style = gradientStyle;

            Fill result = new Fill(paint);
            return result;
        }
Ejemplo n.º 6
0
        Fill GetLinearHorizontalFill()
        {
            //Create a linear gradient paint with
            // + mode = horizontal
            // + fill from left to 1/3 of width
            LinearGradient paint = new LinearGradient();
            paint.StartX = 0;
            paint.EndX = w / 3;
            paint.Ramp = GetColorRamp();
            paint.Mode = LinearGradientMode.Horizontal;
            paint.Style = gradientStyle;

            Fill result = new Fill(paint);
            return result;
        }
Ejemplo n.º 7
0
        Fill GetLinearVerticalFill()
        {
            //Create a linear gradient paint with
            // + mode = vertical
            // + fill from top to 1/2 of height (middle)
            LinearGradient paint = new LinearGradient();
            paint.StartY = 0;
            paint.EndY = h / 2;
            paint.Ramp = GetColorRamp();
            paint.Mode = LinearGradientMode.Vertical;
            paint.Style = gradientStyle;

            Fill result = new Fill(paint);
            return result;
        }
Ejemplo n.º 8
0
        void DrawMaskRegion()
        {
            double[] coordinates = TestFactory.Star();
            TestFactory.Scale(coordinates, 8.0);

            Fill fill = new Fill(Colors.DodgerBlue);
            fill.Opacity = 0.3;

            drawer.DrawPolygon(fill, coordinates);
        }
Ejemplo n.º 9
0
        Fill GetRadialCircleFill()
        {
            //Create a radial gradient paint with
            // + Center point and Focal point is at the center of viewing region
            // + Radius X = Radius Y
            RadialGradient paint = new RadialGradient();
            paint.CenterX = w / 2;
            paint.CenterY = h / 2;
            paint.FocusX = w / 2;
            paint.FocusY = h / 2;
            paint.Radius = Math.Min(w, h) / 3;
            paint.Style = gradientStyle;
            paint.Ramp = GetColorRamp();

            //create fill and return result
            Fill result = new Fill(paint);
            return result;
        }
Ejemplo n.º 10
0
        Fill GetUniformFill()
        {
            Fill result = null;

            //there are several ways to create a solid fills.
            //The following methods give the same result

            //method 1
            result = Fills.Red;

            //method 2
            result = new Fill(Colors.Red);

            //method 3
            ColorPaint paint = new ColorPaint(Colors.Red);
            result = new Fill(paint);

            return result;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Render a star shape to buffer
        /// </summary>
        void DrawStar(PixelsBuffer buffer, Color color, Color background)
        {
            IDrawer drawer = new Drawer(buffer);
            drawer.Clear(background);

            Fill fill = new Fill(color);

            double[] coordinates = TestFactory.Star();
            TestFactory.Scale(coordinates, 2.0);

            //drawer.DrawRectangle(stroke, 0, 0, buffer.Width, buffer.Height);
            drawer.DrawPolygon(fill, coordinates);
        }
Ejemplo n.º 12
0
        void DrawLion(PixelsBuffer buffer, int x, int y)
        {
            //create a new drawing context
            //PixelBuffer buffer = new PixelBuffer(400, 400);
            IDrawer drawer = new Drawer(buffer);

            //get coordinates and colors
            double[][] polygons = LionPathHelper.GetLionPolygons();
            Cross.Drawing.Color[] colors = LionPathHelper.GetLionColors();

            //iterate all polygons and draw them
            double[] coordinates = null;
            drawer.Translate(x, y);
            for (int i = 0; i < polygons.Length; i++)
            {
                coordinates = polygons[i];
                Fill fill = new Fill(colors[i]);
                drawer.DrawPolygon(fill, coordinates);
            }
        }
Ejemplo n.º 13
0
        void DrawLion()
        {
            //create a new drawing context
            PixelsBuffer buffer = new PixelsBuffer(400, 400);
            IDrawer drawer = new Drawer(buffer);
            drawer.Clear(Colors.White);

            //get coordinates and colors
            double[][] polygons = LionPathHelper.GetLionPolygons();
            Color[] colors = LionPathHelper.GetLionColors();

            //iterate all polygons and draw them
            double[] coordinates = null;
            for (int i = 0; i < polygons.Length; i++)
            {
                coordinates = polygons[i];
                Fill fill = new Fill(colors[i]);
                drawer.DrawPolygon(fill, coordinates);
            }

            //show to screen
            DisplayBuffer(buffer);
        }
Ejemplo n.º 14
0
        void DrawLion()
        {
            //set gamma settings
            drawer.GammaCorrected = chkUseGamma.Checked;
            drawer.SetGamma(gammaFactorRed, gammaFactorGreen, gammaFactorBlue);

            //clear background
            drawer.Clear(Colors.White);

            //get coordinates and colors
            double[][] polygons = LionPathHelper.GetLionPolygons();
            Color[] colors = LionPathHelper.GetLionColors();

            //iterate all polygons and draw them
            double[] coordinates = null;
            for (int i = 0; i < polygons.Length; i++)
            {
                coordinates = polygons[i];
                Fill fill = new Fill(colors[i]);
                drawer.DrawPolygon(fill, coordinates);
            }

            //show to screen
            DisplayBuffer(buffer);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Render a polygon.
        /// </summary>
        /// <param name="stroke">The stroke style to draw border</param>
        /// <param name="fill">The fill style to render inner region</param>
        /// <param name="coordinates">Coordinate data. Must be in format [x1, y1, x2, y2, ...]</param>
        /// <remarks>The stroke and the fill can both be a null reference (Nothing in Visual Basic). If the stroke is null, no stroke is performed. If the fill is null, no fill is performed.</remarks>
        public void DrawPolygon(Fill fill, double[] coordinates)
        {
            double[] data = null;

            if (preparationRequired) Prepare();

            //transform input coordinates
            if (transformRequired) data = TransformCoordinates(coordinates);
            else data = coordinates;

            //render the inner region
            if (fill != null)
            {
                rasterizer = GetRasterizer(fill.Paint);
                if ((rasterizer is TranformableRasterizer) && (transformRequired))
                {
                    // set transform matrix to current transformable matrix
                    ((TranformableRasterizer)rasterizer).SetTransformMatrix(currentTransform);
                }
                rasterizer.FillPolygon(fill, data, coordinates.Length / 2, 0);
            }
        }
Ejemplo n.º 16
0
        Fill GetRadialFocalFill()
        {
            //Create a radial gradient paint with
            // + Center point is at the center of viewing region
            // + Different radius X, radius Y
            // + Focal point is near the top-left of viewing region
            RadialGradient paint = new RadialGradient();
            paint.CenterX = w / 2;
            paint.CenterY = h / 2;
            paint.FocusX = w / 4;
            paint.FocusY = h / 2.5;
            paint.RadiusX = w / 3;
            paint.RadiusY = h / 3;
            paint.Style = gradientStyle;
            paint.Ramp = GetColorRamp();

            //create fill and return result
            Fill result = new Fill(paint);
            return result;
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Render a path
        /// </summary>
        /// <param name="stroke">The stroke style to draw border</param>
        /// <param name="fill">The fill style to render inner region</param>
        /// <param name="path">The path geometry</param>
        /// <remarks>The stroke and the fill can both be a null reference (Nothing in Visual Basic). If the stroke is null, no stroke is performed. If the fill is null, no fill is performed.</remarks>
        public void DrawPath(Fill fill, DrawingPath path)
        {
            if (preparationRequired) Prepare();

            #region detemin current scale factor
            double sizeScale = 1.0;
            if (currentTransform != null)
            {
                sizeScale = (currentTransform.ScaleXFactor > currentTransform.ScaleYFactor) ?
                    currentTransform.ScaleXFactor : currentTransform.ScaleYFactor;
            }
            #endregion

            // this need scale factor before generate
            double[][] coordinates = mPathGenerator.Generate(path, 1.0);
            //transform input coordinates

            //render the inner region
            if (fill != null)
            {
                rasterizer = GetRasterizer(fill.Paint);
                rasterizer.Paint = fill;
                rasterizer.Begin();
                if ((transformRequired))
                {
                    if ((rasterizer is TranformableRasterizer))
                    {
                        // set transform matrix to current transformable matrix
                        ((TranformableRasterizer)rasterizer).SetTransformMatrix(currentTransform);
                    }
                    for (int coordinateIndex = 0; coordinateIndex < coordinates.Length; coordinateIndex++)
                    {
                        rasterizer.AddPolygon(
                            TransformCoordinates(coordinates[coordinateIndex]),
                            coordinates[coordinateIndex].Length / 2, 0);
                    }
                }
                else
                {
                    // set paint before using approach 2

                    for (int coordinateIndex = 0; coordinateIndex < coordinates.Length; coordinateIndex++)
                    {
                        rasterizer.AddPolygon(coordinates[coordinateIndex], coordinates[coordinateIndex].Length / 2, 0);
                    }
                }
                rasterizer.Finish();
            }
        }
Ejemplo n.º 18
0
        void DrawLion()
        {
            //get coordinates and colors
            double[][] polygons = LionPathHelper.GetLionPolygons();
            Color[] colors = LionPathHelper.GetLionColors();

            drawer.GammaCorrected = false;//DEBUG - ColorRasterizer is not yet finished

            //iterate all polygons and draw them
            double[] coordinates = null;
            for (int i = 0; i < polygons.Length; i++)
            {
                coordinates = polygons[i];
                Fill fill = new Fill(colors[i]);
                fill.FillingRule = FillingRule.EvenOdd;//DEBUG - ColorRasterizer is not yet finished

                drawer.DrawPolygon(fill, coordinates);
            }
        }