private Path CreateMotherMovePath() { Path path = new Path(); float CenterX = mCurrentBounds.CenterX(); float CenterY = mCurrentBounds.CenterY(); float currentPathLength = 0.0f; path.MoveTo(CenterX, CenterY); //forward top left path.QuadTo(CenterX - mMotherOvalHalfWidth * 2.0f, CenterY, CenterX - mMotherOvalHalfWidth * 2.0f, CenterY - mMotherOvalHalfHeight); mStageMotherForwardTopLeftLength = GetRestLength(path, currentPathLength); currentPathLength += mStageMotherForwardTopLeftLength; //backward top left path.QuadTo(CenterX - mMotherOvalHalfWidth * 1.0f, CenterY - mMotherOvalHalfHeight, CenterX, CenterY); mStageMotherBackwardTopLeftLength = GetRestLength(path, currentPathLength); currentPathLength += mStageMotherBackwardTopLeftLength; //forward bottom left path.QuadTo(CenterX, CenterY + mMotherOvalHalfHeight, CenterX - mMotherOvalHalfWidth / 2, CenterY + mMotherOvalHalfHeight * 1.1f); mStageMotherForwardBottomLeftLength = GetRestLength(path, currentPathLength); currentPathLength += mStageMotherForwardBottomLeftLength; //backward bottom left path.QuadTo(CenterX - mMotherOvalHalfWidth / 2, CenterY + mMotherOvalHalfHeight * 0.6f, CenterX, CenterY); mStageMotherBackwardBottomLeftLength = GetRestLength(path, currentPathLength); return(path); }
private void touch_move(float x, float y) { float dx = Math.Abs(x - mX); float dy = Math.Abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { mPath.QuadTo(mX, mY, (x + mX) / 2, (y + mY) / 2); mX = x; mY = y; } }
private Path CreateChildMovePath() { Path path = new Path(); float CenterX = mCurrentBounds.CenterX(); float CenterY = mCurrentBounds.CenterY(); float currentPathLength = 0.0f; //start path.MoveTo(CenterX, CenterY); //pre forward top left path.LineTo(CenterX + mMotherOvalHalfWidth * 0.75f, CenterY); mStageChildPreForwardTopLeftLength = GetRestLength(path, currentPathLength); currentPathLength += mStageChildPreForwardTopLeftLength; //forward top left path.QuadTo(CenterX - mMotherOvalHalfWidth * 0.5f, CenterY, CenterX - mMotherOvalHalfWidth * 2.0f, CenterY - mMotherOvalHalfHeight); mStageChildForwardTopLeftLength = GetRestLength(path, currentPathLength); currentPathLength += mStageChildForwardTopLeftLength; //pre backward top left path.LineTo(CenterX - mMotherOvalHalfWidth * 2.0f + mMotherOvalHalfWidth * 0.2f, CenterY - mMotherOvalHalfHeight); path.QuadTo(CenterX - mMotherOvalHalfWidth * 2.5f, CenterY - mMotherOvalHalfHeight * 2, CenterX - mMotherOvalHalfWidth * 1.5f, CenterY - mMotherOvalHalfHeight * 2.25f); mStageChildPreBackwardTopLeftLength = GetRestLength(path, currentPathLength); currentPathLength += mStageChildPreBackwardTopLeftLength; //backward top left path.QuadTo(CenterX - mMotherOvalHalfWidth * 0.2f, CenterY - mMotherOvalHalfHeight * 2.25f, CenterX, CenterY); mStageChildBackwardTopLeftLength = GetRestLength(path, currentPathLength); currentPathLength += mStageChildBackwardTopLeftLength; //forward bottom left path.CubicTo(CenterX, CenterY + mMotherOvalHalfHeight, CenterX - mMotherOvalHalfWidth, CenterY + mMotherOvalHalfHeight * 2.5f, CenterX - mMotherOvalHalfWidth * 1.5f, CenterY + mMotherOvalHalfHeight * 2.5f); mStageChildForwardBottomLeftLength = GetRestLength(path, currentPathLength); currentPathLength += mStageChildForwardBottomLeftLength; //backward bottom left path.CubicTo(CenterX - mMotherOvalHalfWidth * 2.0f, CenterY + mMotherOvalHalfHeight * 2.5f, CenterX - mMotherOvalHalfWidth * 3.0f, CenterY + mMotherOvalHalfHeight * 0.8f, CenterX, CenterY); mStageChildBackwardBottomLeftLength = GetRestLength(path, currentPathLength); return(path); }
private Path CreateRightEyeCircle(RectF arcBounds, float offsetY) { Path path = new Path(); //the center of the right eye float rightEyeCenterX = arcBounds.CenterX() + mEyeInterval / 2.0f + mEyeCircleRadius; float rightEyeCenterY = arcBounds.CenterY() + offsetY; //the bounds of left eye RectF leftEyeBounds = new RectF(rightEyeCenterX - mEyeCircleRadius, rightEyeCenterY - mEyeCircleRadius, rightEyeCenterX + mEyeCircleRadius, rightEyeCenterY + mEyeCircleRadius); path.AddArc(leftEyeBounds, 180, -(DEGREE_180 + 15)); //the above radian of of the eye path.QuadTo(leftEyeBounds.Right - mAboveRadianEyeOffsetX, leftEyeBounds.Top + mEyeCircleRadius * 0.2f, leftEyeBounds.Right - mAboveRadianEyeOffsetX / 4.0f, leftEyeBounds.Top - mEyeCircleRadius * 0.15f); return(path); }
private void TouchMove(float x, float y) { var dx = (float)Math.Abs(x - _currentTouch.X); var dy = (float)Math.Abs(y - _currentTouch.Y); // add movement to stroke if outside the area of tolerence (a square with side length of TouchTolerance * 2, centered at currentTouch) if (!(dx >= TouchTolerance) && !(dy >= TouchTolerance)) { return; } if (_currentStroke.IsEmpty) { _currentStroke.MoveTo((float)_currentTouch.X, (float)_currentTouch.Y); } _currentStroke.QuadTo((float)_currentTouch.X, (float)_currentTouch.Y, (float)(x + _currentTouch.X) / 2, (float)(y + _currentTouch.Y) / 2); }
protected override void OnDraw(Canvas canvas) { var rect = new Rect(); this.GetDrawingRect(rect); var path = new Android.Graphics.Path(); var lastPoint = new PointF(); foreach (var command in PathDataParser.Parse(Element.Data)) { switch (command.Type) { case PathDataParser.CommandType.MoveTo: lastPoint = new PointF(command.Arguments[0], command.Arguments[1]); path.MoveTo(lastPoint.X, lastPoint.Y); break; case PathDataParser.CommandType.LineTo: lastPoint = new PointF(command.Arguments[0], command.Arguments[1]); path.LineTo(lastPoint.X, lastPoint.Y); break; case PathDataParser.CommandType.LineHor: lastPoint = new PointF(command.Arguments[0], lastPoint.Y); path.LineTo(lastPoint.X, lastPoint.Y); break; case PathDataParser.CommandType.LineVer: lastPoint = new PointF(lastPoint.X, command.Arguments[0]); path.LineTo(lastPoint.X, lastPoint.Y); break; case PathDataParser.CommandType.RelativeLineHor: lastPoint = new PointF(command.Arguments[0] + lastPoint.X, lastPoint.Y); path.LineTo(lastPoint.X, lastPoint.Y); break; case PathDataParser.CommandType.RelativeLineVer: lastPoint = new PointF(lastPoint.X, lastPoint.Y + command.Arguments[0]); path.LineTo(lastPoint.X, lastPoint.Y); break; case PathDataParser.CommandType.RelativeLineTo: lastPoint = new PointF(lastPoint.X + command.Arguments[0], lastPoint.Y + command.Arguments[1]); path.LineTo(lastPoint.X, lastPoint.Y); break; case PathDataParser.CommandType.RelativeMoveTo: lastPoint = new PointF(lastPoint.X + command.Arguments[0], lastPoint.Y + command.Arguments[1]); path.MoveTo(lastPoint.X, lastPoint.Y); break; case PathDataParser.CommandType.QBezier: path.QuadTo(command.Arguments[0], command.Arguments[1], command.Arguments[2], command.Arguments[3]); lastPoint = new PointF(command.Arguments[2], command.Arguments[3]); break; case PathDataParser.CommandType.Bezier: path.CubicTo( command.Arguments[0], command.Arguments[1], command.Arguments[2], command.Arguments[3], command.Arguments[4], command.Arguments[5]); lastPoint = new PointF(command.Arguments[4], command.Arguments[5]); break; case PathDataParser.CommandType.RelativeBezier: var p1 = new PointF(lastPoint.X + command.Arguments[0], lastPoint.Y + command.Arguments[1]); var p2 = new PointF(p1.X + command.Arguments[2], p1.Y + command.Arguments[3]); lastPoint = new PointF(p2.X + command.Arguments[4], p2.Y + command.Arguments[5]); path.CubicTo(p1.X, p1.Y, p2.X, p2.Y, lastPoint.X, lastPoint.Y); break; case PathDataParser.CommandType.Close: path.Close(); break; } } var paint = new Paint(PaintFlags.AntiAlias); paint.StrokeWidth = Element.StrokeThickness; paint.StrokeMiter = 10f; canvas.Save(); paint.SetStyle(Paint.Style.Stroke); paint.Color = Element.Stroke.ToAndroid(); canvas.DrawPath(path, paint); canvas.Restore(); }
public static APath ToAPath(this Geometry geometry, Context context) { APath path = new APath(); float density = context.Resources.DisplayMetrics.Density; if (geometry is LineGeometry) { LineGeometry lineGeometry = geometry as LineGeometry; path.MoveTo( density * (float)lineGeometry.StartPoint.X, density * (float)lineGeometry.StartPoint.Y); path.LineTo( density * (float)lineGeometry.EndPoint.X, density * (float)lineGeometry.EndPoint.Y); } else if (geometry is RectangleGeometry) { FormsRectangle rect = (geometry as RectangleGeometry).Rect; path.AddRect( density * (float)rect.Left, density * (float)rect.Top, density * (float)rect.Right, density * (float)rect.Bottom, APath.Direction.Cw); } else if (geometry is EllipseGeometry) { EllipseGeometry ellipseGeometry = geometry as EllipseGeometry; path.AddOval(new RectF( density * (float)(ellipseGeometry.Center.X - ellipseGeometry.RadiusX), density * (float)(ellipseGeometry.Center.Y - ellipseGeometry.RadiusY), density * (float)(ellipseGeometry.Center.X + ellipseGeometry.RadiusX), density * (float)(ellipseGeometry.Center.Y + ellipseGeometry.RadiusY)), APath.Direction.Cw); } else if (geometry is GeometryGroup) { GeometryGroup geometryGroup = geometry as GeometryGroup; path.SetFillType(geometryGroup.FillRule == FillRule.Nonzero ? APath.FillType.Winding : APath.FillType.EvenOdd); foreach (Geometry child in geometryGroup.Children) { APath childPath = child.ToAPath(context); path.AddPath(childPath); } } else if (geometry is PathGeometry) { PathGeometry pathGeometry = geometry as PathGeometry; path.SetFillType(pathGeometry.FillRule == FillRule.Nonzero ? APath.FillType.Winding : APath.FillType.EvenOdd); foreach (PathFigure pathFigure in pathGeometry.Figures) { path.MoveTo( density * (float)pathFigure.StartPoint.X, density * (float)pathFigure.StartPoint.Y); Point lastPoint = pathFigure.StartPoint; foreach (PathSegment pathSegment in pathFigure.Segments) { // LineSegment if (pathSegment is LineSegment) { LineSegment lineSegment = pathSegment as LineSegment; path.LineTo( density * (float)lineSegment.Point.X, density * (float)lineSegment.Point.Y); lastPoint = lineSegment.Point; } // PolylineSegment else if (pathSegment is PolyLineSegment) { PolyLineSegment polylineSegment = pathSegment as PolyLineSegment; PointCollection points = polylineSegment.Points; for (int i = 0; i < points.Count; i++) { path.LineTo( density * (float)points[i].X, density * (float)points[i].Y); } lastPoint = points[points.Count - 1]; } // BezierSegment else if (pathSegment is BezierSegment) { BezierSegment bezierSegment = pathSegment as BezierSegment; path.CubicTo( density * (float)bezierSegment.Point1.X, density * (float)bezierSegment.Point1.Y, density * (float)bezierSegment.Point2.X, density * (float)bezierSegment.Point2.Y, density * (float)bezierSegment.Point3.X, density * (float)bezierSegment.Point3.Y); lastPoint = bezierSegment.Point3; } // PolyBezierSegment else if (pathSegment is PolyBezierSegment) { PolyBezierSegment polyBezierSegment = pathSegment as PolyBezierSegment; PointCollection points = polyBezierSegment.Points; if (points.Count >= 3) { for (int i = 0; i < points.Count; i += 3) { path.CubicTo( density * (float)points[i + 0].X, density * (float)points[i + 0].Y, density * (float)points[i + 1].X, density * (float)points[i + 1].Y, density * (float)points[i + 2].X, density * (float)points[i + 2].Y); } } lastPoint = points[points.Count - 1]; } // QuadraticBezierSegment else if (pathSegment is QuadraticBezierSegment) { QuadraticBezierSegment bezierSegment = pathSegment as QuadraticBezierSegment; path.QuadTo( density * (float)bezierSegment.Point1.X, density * (float)bezierSegment.Point1.Y, density * (float)bezierSegment.Point2.X, density * (float)bezierSegment.Point2.Y); lastPoint = bezierSegment.Point2; } // PolyQuadraticBezierSegment else if (pathSegment is PolyQuadraticBezierSegment) { PolyQuadraticBezierSegment polyBezierSegment = pathSegment as PolyQuadraticBezierSegment; PointCollection points = polyBezierSegment.Points; if (points.Count >= 2) { for (int i = 0; i < points.Count; i += 2) { path.QuadTo( density * (float)points[i + 0].X, density * (float)points[i + 0].Y, density * (float)points[i + 1].X, density * (float)points[i + 1].Y); } } lastPoint = points[points.Count - 1]; } // ArcSegment else if (pathSegment is ArcSegment) { ArcSegment arcSegment = pathSegment as ArcSegment; List <Point> points = new List <Point>(); GeometryHelper.FlattenArc( points, lastPoint, arcSegment.Point, arcSegment.Size.Width, arcSegment.Size.Height, arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection == SweepDirection.CounterClockwise, 1); for (int i = 0; i < points.Count; i++) { path.LineTo( density * (float)points[i].X, density * (float)points[i].Y); } if (points.Count > 0) { lastPoint = points[points.Count - 1]; } } } if (pathFigure.IsClosed) { path.Close(); } } } return(path); }
public static Bitmap ToTransformedCorners(Bitmap source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Width; double sourceHeight = source.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) { desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); } else if (currentRatio < desiredRatio) { desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); } topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, Bitmap.Config.Argb8888); using (Canvas canvas = new Canvas(bitmap)) using (Paint paint = new Paint()) using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp)) using (Matrix matrix = new Matrix()) using (var path = new Path()) { if (cropX != 0 || cropY != 0) { matrix.SetTranslate(-cropX, -cropY); shader.SetLocalMatrix(matrix); } paint.SetShader(shader); paint.AntiAlias = true; // TopLeft if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut)) { path.MoveTo(0, (float)topLeftCornerSize); path.LineTo((float)topLeftCornerSize, 0); } else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded)) { path.MoveTo(0, (float)topLeftCornerSize); path.QuadTo(0, 0, (float)topLeftCornerSize, 0); } else { path.MoveTo(0, 0); } // TopRight if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut)) { path.LineTo((float)(desiredWidth - topRightCornerSize), 0); path.LineTo((float)desiredWidth, (float)topRightCornerSize); } else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded)) { path.LineTo((float)(desiredWidth - topRightCornerSize), 0); path.QuadTo((float)desiredWidth, 0, (float)desiredWidth, (float)topRightCornerSize); } else { path.LineTo((float)desiredWidth ,0); } // BottomRight if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut)) { path.LineTo((float)desiredWidth, (float)(desiredHeight - bottomRightCornerSize)); path.LineTo((float)(desiredWidth - bottomRightCornerSize), (float)desiredHeight); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded)) { path.LineTo((float)desiredWidth, (float)(desiredHeight - bottomRightCornerSize)); path.QuadTo((float)desiredWidth, (float)desiredHeight, (float)(desiredWidth - bottomRightCornerSize), (float)desiredHeight); } else { path.LineTo((float)desiredWidth, (float)desiredHeight); } // BottomLeft if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut)) { path.LineTo((float)bottomLeftCornerSize, (float)desiredHeight); path.LineTo(0, (float)(desiredHeight - bottomLeftCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded)) { path.LineTo((float)bottomLeftCornerSize, (float)desiredHeight); path.QuadTo(0, (float)desiredHeight, 0, (float)(desiredHeight - bottomLeftCornerSize)); } else { path.LineTo(0, (float)desiredHeight); } path.Close(); canvas.DrawPath(path, paint); return bitmap; } }
ObjectAnimator PrepareAnimation(View view, int multiplier) { var path = new Path(); var metrics = Resources.DisplayMetrics; var tx = multiplier * (metrics.WidthPixels - view.PaddingRight); var ty = -TypedValue.ApplyDimension(ComplexUnitType.Dip, 64, metrics); path.MoveTo(tx, ty); path.QuadTo(2 * tx / 3, 0, 0, 0); view.TranslationX = tx; view.TranslationY = ty; var anim = ObjectAnimator.OfFloat(view, "translationX", "translationY", path); anim.SetDuration(600); anim.SetInterpolator(AnimationUtils.LoadInterpolator(this, Android.Resource.Interpolator.FastOutSlowIn)); return anim; }
public static APath AsAndroidPath(this PathF path) { var nativePath = new APath(); int pointIndex = 0; int arcAngleIndex = 0; int arcClockwiseIndex = 0; foreach (var operation in path.PathOperations) { if (operation == PathOperation.MoveTo) { var point = path[pointIndex++]; nativePath.MoveTo(point.X, point.Y); } else if (operation == PathOperation.Line) { var point = path[pointIndex++]; nativePath.LineTo(point.X, point.Y); } else if (operation == PathOperation.Quad) { var controlPoint = path[pointIndex++]; var point = path[pointIndex++]; nativePath.QuadTo(controlPoint.X, controlPoint.Y, point.X, point.Y); } else if (operation == PathOperation.Cubic) { var controlPoint1 = path[pointIndex++]; var controlPoint2 = path[pointIndex++]; var point = path[pointIndex++]; nativePath.CubicTo(controlPoint1.X, controlPoint1.Y, controlPoint2.X, controlPoint2.Y, point.X, point.Y); } else if (operation == PathOperation.Arc) { var topLeft = path[pointIndex++]; var bottomRight = path[pointIndex++]; var startAngle = path.GetArcAngle(arcAngleIndex++); var endAngle = path.GetArcAngle(arcAngleIndex++); var clockwise = path.IsArcClockwise(arcClockwiseIndex++); while (startAngle < 0) { startAngle += 360; } while (endAngle < 0) { endAngle += 360; } var rect = new RectF(topLeft.X, topLeft.Y, bottomRight.X, bottomRight.Y); var sweep = GraphicsOperations.GetSweep(startAngle, endAngle, clockwise); startAngle *= -1; if (!clockwise) { sweep *= -1; } nativePath.ArcTo(rect, startAngle, sweep); } else if (operation == PathOperation.Close) { nativePath.Close(); } } return(nativePath); }
protected override void OnDraw(Canvas canvas) { base.OnDraw(canvas); switch(mShape) { case Shape.TRIANGLE: { if (mIsLoading) { mAnimPercent += 0.1611113f; Path path = new Path(); path.MoveTo(RelativeXFromView(0.5f), RelativeYFromView(0.5f)); if (mAnimPercent >= 1) { mShape = Shape.CIRCLE; mIsLoading = false; mAnimPercent = 1; } float controlX = mControlX - RelativeXFromView(mAnimPercent * triangle2Circle) * genhao3; float controlY = mControlY - RelativeYFromView(mAnimPercent * triangle2Circle); path.QuadTo(RelativeXFromView(1) - controlX, controlY, RelativeXFromView(0.5f + genhao3 / 4), RelativeYFromView(0.75f)); path.QuadTo(RelativeXFromView(0.5f), RelativeYFromView(0.75f + 2 * mAnimPercent * triangle2Circle), RelativeXFromView(0.5f - genhao3 / 4), RelativeYFromView(0.75f)); path.QuadTo(controlX, controlY, RelativeXFromView(0.5f), RelativeYFromView(0f)); path.Close(); canvas.DrawPath(path, mPaint); Invalidate(); } else { Path path = new Path(); mPaint.Color = Resources.GetColor(Resource.Color.triangle); path.MoveTo(RelativeXFromView(0.5f), RelativeYFromView(0f)); path.LineTo(RelativeXFromView(1), RelativeYFromView(genhao3 / 2f)); path.LineTo(RelativeXFromView(0), RelativeYFromView(genhao3 / 2f)); mControlX = RelativeXFromView(0.5f - genhao3 / 8f); mControlY = RelativeYFromView(3 / 8f); mAnimPercent = 0; path.Close(); canvas.DrawPath(path, mPaint); } } break; case Shape.CIRCLE: { if(mIsLoading) { float magicNumber = mMagicNumber + mAnimPercent; mAnimPercent += 0.12f; if(magicNumber + mAnimPercent > 1.9f) { mShape = Shape.RECT; mIsLoading = false; } Path path = new Path(); path.MoveTo(RelativeXFromView(0.5f), RelativeYFromView(0f)); path.CubicTo(RelativeXFromView(0.5f + magicNumber / 2), RelativeYFromView(0f), RelativeXFromView(1), RelativeYFromView(0.5f - magicNumber / 2), RelativeXFromView(1f), RelativeYFromView(0.5f)); path.CubicTo(RelativeXFromView(1), RelativeXFromView(0.5f + magicNumber / 2), RelativeXFromView(0.5f + mMagicNumber / 2), RelativeYFromView(1f), RelativeXFromView(0.5f), RelativeYFromView(1f)); path.CubicTo(RelativeXFromView(0.5f - magicNumber / 2), RelativeXFromView(1f), RelativeXFromView(0), RelativeYFromView(0.5f + magicNumber / 2), RelativeXFromView(0f), RelativeYFromView(0.5f)); path.CubicTo(RelativeXFromView(0f), RelativeXFromView(0.5f - magicNumber / 2), RelativeXFromView(0.5f - magicNumber / 2), RelativeYFromView(0), RelativeXFromView(0.5f), RelativeYFromView(0f)); path.Close(); canvas.DrawPath(path, mPaint); Invalidate(); } else { mPaint.Color = Resources.GetColor(Resource.Color.circle); Path path = new Path(); float magicNumber = mMagicNumber; path.MoveTo(RelativeXFromView(0.5f), RelativeYFromView(0f)); path.CubicTo(RelativeXFromView(0.5f + magicNumber / 2), 0, RelativeXFromView(1), RelativeYFromView(magicNumber / 2), RelativeXFromView(1f), RelativeYFromView(0.5f)); path.CubicTo( RelativeXFromView(1), RelativeXFromView(0.5f + magicNumber / 2), RelativeXFromView(0.5f + magicNumber / 2), RelativeYFromView(1f), RelativeXFromView(0.5f), RelativeYFromView(1f)); path.CubicTo(RelativeXFromView(0.5f - magicNumber / 2), RelativeXFromView(1f), RelativeXFromView(0), RelativeYFromView(0.5f + magicNumber / 2), RelativeXFromView(0f), RelativeYFromView(0.5f)); path.CubicTo(RelativeXFromView(0f), RelativeXFromView(0.5f - magicNumber / 2), RelativeXFromView(0.5f - magicNumber / 2), RelativeYFromView(0), RelativeXFromView(0.5f), RelativeYFromView(0f)); mAnimPercent = 0; path.Close(); canvas.DrawPath(path, mPaint); } } break; case Shape.RECT: { if(mIsLoading) { mAnimPercent += 0.15f; if(mAnimPercent >= 1) { mShape = Shape.TRIANGLE; mIsLoading = false; mAnimPercent = 1; } Path path = new Path(); path.MoveTo(RelativeXFromView(0.5f * mAnimPercent), 0); path.LineTo(RelativeYFromView(1 - 0.5f * mAnimPercent), 0); float distanceX = (mControlX) * mAnimPercent; float distanceY = (RelativeYFromView(1f) - mControlY) * mAnimPercent; path.LineTo(RelativeXFromView(1f) - distanceX, RelativeYFromView(1f) - distanceY); path.LineTo(RelativeXFromView(0f) + distanceX, RelativeYFromView(1f) - distanceY); path.Close(); canvas.DrawPath(path, mPaint); Invalidate(); } else { mPaint.Color = Resources.GetColor(Resource.Color.rect); mControlX = RelativeXFromView(0.5f - genhao3 / 4); mControlY = RelativeYFromView(0.75f); Path path = new Path(); path.MoveTo(RelativeXFromView(0f), RelativeYFromView(0f)); path.LineTo(RelativeXFromView(1f), RelativeYFromView(0f)); path.LineTo(RelativeXFromView(1f), RelativeYFromView(1f)); path.LineTo(RelativeXFromView(0f), RelativeYFromView(1f)); path.Close(); mAnimPercent = 0; canvas.DrawPath(path, mPaint); } } break; } }
public object ConvertToNative(Geometry geometry) { // TODO: Can we keep this object around and reset it? droidGraphics.Path path = new droidGraphics.Path(); // TODO: Might we also save a non-transformed path and a transformed path? -- No, that fails for GeometryGroup // Determine what type of Geometry we're dealing with if (geometry is LineGeometry) { LineGeometry lineGeometry = geometry as LineGeometry; path.MoveTo(PixelsPerDip * (float)lineGeometry.StartPoint.X, PixelsPerDip * (float)lineGeometry.StartPoint.Y); path.LineTo(PixelsPerDip * (float)lineGeometry.EndPoint.X, PixelsPerDip * (float)lineGeometry.EndPoint.Y); } else if (geometry is RectangleGeometry) { Rect rect = (geometry as RectangleGeometry).Rect; path.AddRect(PixelsPerDip * (float)rect.Left, PixelsPerDip * (float)rect.Top, PixelsPerDip * (float)rect.Right, PixelsPerDip * (float)rect.Bottom, droidGraphics.Path.Direction.Cw); // TODO: Check for compatibility! } else if (geometry is EllipseGeometry) { EllipseGeometry ellipseGeometry = geometry as EllipseGeometry; path.AddOval(new droidGraphics.RectF( PixelsPerDip * (float)(ellipseGeometry.Center.X - ellipseGeometry.RadiusX), PixelsPerDip * (float)(ellipseGeometry.Center.Y - ellipseGeometry.RadiusY), PixelsPerDip * (float)(ellipseGeometry.Center.X + ellipseGeometry.RadiusX), PixelsPerDip * (float)(ellipseGeometry.Center.Y + ellipseGeometry.RadiusY)), droidGraphics.Path.Direction.Cw); // TODO: Check for compatibility! } else if (geometry is GeometryGroup) { GeometryGroup geometryGroup = geometry as GeometryGroup; SetFillRule(path, geometryGroup.FillRule); foreach (Geometry child in geometryGroup.Children) { droidGraphics.Path childPath = ConvertToNative(child) as droidGraphics.Path; path.AddPath(childPath); } } else if (geometry is PathGeometry) { PathGeometry pathGeometry = geometry as PathGeometry; SetFillRule(path, pathGeometry.FillRule); foreach (PathFigure pathFigure in pathGeometry.Figures) { path.MoveTo(PixelsPerDip * (float)pathFigure.StartPoint.X, PixelsPerDip * (float)pathFigure.StartPoint.Y); Point lastPoint = pathFigure.StartPoint; foreach (PathSegment pathSegment in pathFigure.Segments) { // LineSegment if (pathSegment is LineSegment) { LineSegment lineSegment = pathSegment as LineSegment; path.LineTo(PixelsPerDip * (float)lineSegment.Point.X, PixelsPerDip * (float)lineSegment.Point.Y); lastPoint = lineSegment.Point; } // PolylineSegment else if (pathSegment is PolyLineSegment) { PolyLineSegment polylineSegment = pathSegment as PolyLineSegment; PointCollection points = polylineSegment.Points; for (int i = 0; i < points.Count; i++) { path.LineTo(PixelsPerDip * (float)points[i].X, PixelsPerDip * (float)points[i].Y); } lastPoint = points[points.Count - 1]; } // BezierSegment else if (pathSegment is BezierSegment) { BezierSegment bezierSegment = pathSegment as BezierSegment; path.CubicTo(PixelsPerDip * (float)bezierSegment.Point1.X, PixelsPerDip * (float)bezierSegment.Point1.Y, PixelsPerDip * (float)bezierSegment.Point2.X, PixelsPerDip * (float)bezierSegment.Point2.Y, PixelsPerDip * (float)bezierSegment.Point3.X, PixelsPerDip * (float)bezierSegment.Point3.Y); lastPoint = bezierSegment.Point3; } // PolyBezierSegment else if (pathSegment is PolyBezierSegment) { PolyBezierSegment polyBezierSegment = pathSegment as PolyBezierSegment; PointCollection points = polyBezierSegment.Points; for (int i = 0; i < points.Count; i += 3) { path.CubicTo(PixelsPerDip * (float)points[i + 0].X, PixelsPerDip * (float)points[i + 0].Y, PixelsPerDip * (float)points[i + 1].X, PixelsPerDip * (float)points[i + 1].Y, PixelsPerDip * (float)points[i + 2].X, PixelsPerDip * (float)points[i + 2].Y); } lastPoint = points[points.Count - 1]; } // QuadraticBezierSegment else if (pathSegment is QuadraticBezierSegment) { QuadraticBezierSegment bezierSegment = pathSegment as QuadraticBezierSegment; path.QuadTo(PixelsPerDip * (float)bezierSegment.Point1.X, PixelsPerDip * (float)bezierSegment.Point1.Y, PixelsPerDip * (float)bezierSegment.Point2.X, PixelsPerDip * (float)bezierSegment.Point2.Y); lastPoint = bezierSegment.Point2; } // PolyQuadraticBezierSegment else if (pathSegment is PolyQuadraticBezierSegment) { PolyQuadraticBezierSegment polyBezierSegment = pathSegment as PolyQuadraticBezierSegment; PointCollection points = polyBezierSegment.Points; for (int i = 0; i < points.Count; i += 2) { path.QuadTo(PixelsPerDip * (float)points[i + 0].X, PixelsPerDip * (float)points[i + 0].Y, PixelsPerDip * (float)points[i + 1].X, PixelsPerDip * (float)points[i + 1].Y); } lastPoint = points[points.Count - 1]; } // ArcSegment else if (pathSegment is ArcSegment) { ArcSegment arcSegment = pathSegment as ArcSegment; List <Point> points = new List <Point>(); Xamarin.Forms.Shapes.Shapes.FlattenArc(points, lastPoint, arcSegment.Point, arcSegment.Size.Width, arcSegment.Size.Height, arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection == SweepDirection.Counterclockwise, 1); for (int i = 0; i < points.Count; i++) { path.LineTo(PixelsPerDip * (float)points[i].X, PixelsPerDip * (float)points[i].Y); } lastPoint = points[points.Count - 1]; } } if (pathFigure.IsClosed) { path.Close(); } } } // Apply transform if (geometry.Transform != null) { path.Transform((droidGraphics.Matrix)geometry.Transform.GetNativeObject()); } return(path); }