/* * public static void DrawGradientPath(Canvas canvas, List<TinyTypeLib.PointF> path, int gradientSize, Color color) * { * if (path.Count < 3) * { * throw new Exception("Path must have at least three points"); * } * * // Add the first two points to the end to make it easier to assess direction * int originalCount = path.Count; * path.Add(path[0]); * path.Add(path[1]); * * for (int i = 0; i < originalCount; i++ ) * { * Line line = new Line(path[i], path[i + 1]); * DrawGradientLine(canvas, line, gradientSize, color); * } * } * * public static void DrawGradientCap(Canvas canvas, TinyTypeLib.Rect middleSquare, int quadrant, int gradientSize, Color color) * { * Paint paint = new Paint(); * paint.SetStyle(Paint.Style.Fill); * Color transparent = color; * transparent.A = 0; * * Point center = new Point( * quadrant == 1 || quadrant == 4 ? middleSquare.Right : middleSquare.Left, * quadrant == 1 || quadrant == 2 ? middleSquare.Top : middleSquare.Bottom); * * int startAngle = (4 - quadrant) * 90; * * paint.SetShader(new RadialGradient(center.X, center.Y, gradientSize, color, transparent, Shader.TileMode.Mirror)); * canvas.DrawArc(new RectF( * center.X - gradientSize, * center.Y - gradientSize, * center.X + gradientSize, * center.Y + gradientSize), startAngle, 90, true, paint); * * } */ // gradientSize is the distance from the line to half-way through the gradient's fade // The direction of the gradient is controlled by the sign of signedGradientSize public static void DrawGradientLine(Canvas canvas, Line line, int signedGradientSize, Color color, TinyTypeLib.Rect clipRect) { // Allow for a little overlap. For some reason, Android leaves a white line even though the clipRects were lining up. clipRect = new TinyTypeLib.Rect(clipRect.Left - 1, clipRect.Top - 1, clipRect.Right + 1, clipRect.Bottom + 1); int gradientSizeAbs = Math.Abs(signedGradientSize); Line perpendicular = line.GetPerpendicular(gradientSizeAbs); Paint paint = new Paint(); paint.SetStyle(Paint.Style.Stroke); paint.StrokeCap = Paint.Cap.Butt; // Here we need to solve for the gradient's start point, or the location of B. We'll solve for angle C and length a to find that out. double c = line.Length; double b = gradientSizeAbs; /* * B____c_____A * \ / * a\ /b * \/ * C=90 */ // Solve for angle B in radians. B = sin-1(b/c) double B = Math.Asin(b / c); // Solve for length a. a^2+b^2 = c^2, so a = sqrt(c^2-b^2) double a = Math.Sqrt((c * c) - (b * b)); int direction = signedGradientSize > 0 ? -1 : 1; double finalAngle = direction * B + line.Angle; Line test = new Line(866, 313.5f, 678, 313.5f); double ang = test.Angle; // should be 3.14 Line l = new Line(line.Start, finalAngle, a); Line gradientLine = new Line(l.End, line.End); canvas.ClipRect(AndroidUtil.GetRect(clipRect)); paint.StrokeWidth = gradientSizeAbs * 2; paint.SetShader(new LinearGradient(gradientLine.Start.X, gradientLine.Start.Y, gradientLine.End.X, gradientLine.End.Y, color, AndroidUtil.GetTransparentColor(color), Shader.TileMode.Mirror)); canvas.DrawLine(l.Start.X, l.Start.Y, l.End.X, l.End.Y, paint); }
public static void DrawGradientCap(Canvas canvas, TinyTypeLib.Rect middleSquare, int quadrant, int gradientSize, Color color) { Paint paint = new Paint(); paint.SetStyle(Paint.Style.Fill); Color transparent = color; transparent.A = 0; Point center = new Point( quadrant == 1 || quadrant == 4 ? middleSquare.Right : middleSquare.Left, quadrant == 1 || quadrant == 2 ? middleSquare.Top : middleSquare.Bottom); int startAngle = (4 - quadrant) * 90; paint.SetShader(new RadialGradient(center.X, center.Y, gradientSize, color, transparent, Shader.TileMode.Mirror)); canvas.DrawArc(new RectF( center.X - gradientSize, center.Y - gradientSize, center.X + gradientSize, center.Y + gradientSize), startAngle, 90, true, paint); }
private void DrawLetters(Canvas canvas) { Paint letterPaint = new Paint(); letterPaint.StrokeWidth = 1; letterPaint.SetStyle(Paint.Style.Stroke); int rectWidth = (int)(mViewInfo.Width / 11.0); int rectHeight = (int)(mViewInfo.Height / 11.0); letterPaint.TextSize = Math.Min(rectHeight, 80); for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { letterPaint.SetStyle(Paint.Style.Stroke); //canvas.DrawRect(new Android.Graphics.Rect(i * rectWidth, j * rectHeight, (i + 1) * rectWidth, (j + 1) * rectHeight), paint); KeyValuePair <int, int> index = new KeyValuePair <int, int>(i, j); if (sSignatureLocations.ContainsKey(index)) { Signature sig = sSignatureLocations[index]; char chr = Arrangement.Layout[sig].Character; char chrToMeasure = chr == ' ' ? 'X' : chr; int x = i * rectWidth; int y = j * rectHeight; Android.Graphics.Rect rect = new Android.Graphics.Rect(x, y, x + rectWidth, y + rectHeight); // The actual character is used for horizontal positioning Android.Graphics.Rect characterBounds = new Android.Graphics.Rect(); letterPaint.GetTextBounds(chrToMeasure.ToString(), 0, 1, characterBounds); // A standard character is used for vertical positioning so all the characters line up Android.Graphics.Rect xBounds = new Android.Graphics.Rect(); letterPaint.GetTextBounds("X", 0, 1, xBounds); // Center the character in the rectangle int textLeft = rect.Left + (int)((rect.Width() - characterBounds.Width()) / 2.0); int textBottom = rect.Bottom - (int)((rect.Height() - xBounds.Width()) / 2.0); // Lift the bottom up away from the border if we're on the bottom row //int textBottom = rect.Bottom - (j == 10 ? 10 : 0); TinyTypeLib.Rect boundsInLocation = new TinyTypeLib.Rect(rect.Left, textBottom - xBounds.Height(), rect.Right, textBottom); if (sig == mViewInfo.CurrentSignature) { // Don't draw within the MiddleSquare canvas.ClipRect(new Android.Graphics.Rect(mViewInfo.Left, mViewInfo.Top, mViewInfo.Right, mViewInfo.Bottom)); Path cp = new Path(); cp.AddCircle(mViewInfo.MiddleSquare.Center.X, mViewInfo.MiddleSquare.Center.Y, mViewInfo.MiddleSquare.Width / 2f, Path.Direction.Cw); canvas.ClipPath(cp, Region.Op.Difference); //canvas.ClipRect(Converter.TinyTypeRectToAndroidRect(mViewInfo.MiddleSquare), Region.Op.Difference); int gradientSize = 20; Paint linePaint = new Paint(); // Draw a line to the circle we're about to draw Line lineToLetter = new Line(mViewInfo.MiddleSquare.Center, boundsInLocation.Center); Line perpendicular = lineToLetter.GetPerpendicular(gradientSize); Color color = GetPositionColor((Position)mViewInfo.CurrentSignature.StartHeading.Quadrant); Color transparent = AndroidUtil.GetTransparentColor(color); linePaint.SetShader(new LinearGradient(perpendicular.Start.X, perpendicular.Start.Y, perpendicular.End.X, perpendicular.End.Y, color, transparent, Shader.TileMode.Mirror)); linePaint.SetStyle(Paint.Style.Stroke); linePaint.StrokeWidth = gradientSize * 2; linePaint.StrokeCap = Paint.Cap.Round; canvas.DrawLine(lineToLetter.Start.X, lineToLetter.Start.Y, lineToLetter.End.X, lineToLetter.End.Y, linePaint); } else { //letterPaint.Color = new Color(255, 0, 255); } if (chr != char.MinValue) { // Fill a circle in the background of the circle Paint letterBackgroundPaint = new Paint(); Color letterBackgroundColor = GetPositionColor((Position)sig.StartQuadrant); Color letterBackgroundTransparent = AndroidUtil.GetTransparentColor(letterBackgroundColor); float radius = Math.Max(boundsInLocation.Width, boundsInLocation.Height) / 2f; letterBackgroundPaint.SetShader(new RadialGradient(boundsInLocation.Center.X, boundsInLocation.Center.Y, radius, letterBackgroundColor, letterBackgroundTransparent, Shader.TileMode.Mirror)); letterBackgroundPaint.SetStyle(Paint.Style.FillAndStroke); canvas.DrawCircle(boundsInLocation.Center.X, boundsInLocation.Center.Y, (float)(Math.Max(boundsInLocation.Width, boundsInLocation.Height) / 2.0), letterBackgroundPaint); letterPaint.SetStyle(Paint.Style.FillAndStroke); canvas.DrawText(chr.ToString(), textLeft, textBottom, letterPaint); } } } } letterPaint.SetStyle(Paint.Style.FillAndStroke); letterPaint.Color = GetPositionColor(Position.Middle); letterPaint.TextSize = mViewInfo.MiddleSquare.Height * 0.75f; Drawing.ClearClip(canvas, mViewInfo); // Draw the selected character in the middle of the square if (mViewInfo.CurrentSignature.Gesture == Signature.Gestures.Normal) { char chr = Arrangement.Layout[mViewInfo.CurrentSignature].Character; // Center the character in the rectangle Android.Graphics.Rect bounds = new Android.Graphics.Rect(); letterPaint.GetTextBounds(chr.ToString(), 0, 1, bounds); int textLeft = mViewInfo.MiddleSquare.Left + (int)((mViewInfo.MiddleSquare.Width - bounds.Width()) / 2.0); int textBottom = mViewInfo.MiddleSquare.Bottom - (int)((mViewInfo.MiddleSquare.Height - bounds.Height()) / 2.0); canvas.DrawText(chr.ToString(), textLeft, textBottom, letterPaint); } /* * // Draw the previous character above the center square * if (mPreviousCharacter != char.MinValue) * { * TinyTypeLib.Rect rect = mViewInfo.MiddleSquare.Offset(0, -mViewInfo.MiddleSquare.Height); * * //paint.SetStyle(Paint.Style.Stroke); * //canvas.DrawRect(Converter.TinyTypeRectToAndroidRect(rect), paint); * //paint.SetStyle(Paint.Style.FillAndStroke); * * // Center the character in the rectangle * Android.Graphics.Rect bounds = new Android.Graphics.Rect(); * letterPaint.GetTextBounds(mPreviousCharacter.ToString(), 0, 1, bounds); * * int textLeft = rect.Left + (int)((rect.Width - bounds.Width()) / 2.0); * int textBottom = rect.Bottom - (int)((rect.Height - bounds.Height()) / 2.0); * * canvas.DrawText(mPreviousCharacter.ToString(), textLeft, textBottom, letterPaint); * }*/ }