protected override void OnPointerPressed(PointerRoutedEventArgs args) { uint id = args.Pointer.PointerId; Point pt = args.GetCurrentPoint(referencePanel).Position; if (fingerTouches.ContainsKey(id)) { fingerTouches.Remove(id); } FingerInfo fingerInfo = new FingerInfo { LastPosition = new Point(Double.PositiveInfinity, Double.PositiveInfinity), ThisPosition = pt, }; fingerTouches.Add(id, fingerInfo); CapturePointer(args.Pointer); base.OnPointerPressed(args); }
// Every 1/60th second, update bitmap with user drawings bool OnTimerTick() { if (bitmap == null) { return(true); } // Determine the current color. float tColor = stopwatch.ElapsedMilliseconds % 10000 / 10000f; fingerPaint.Color = SKColor.FromHsl(360 * tColor, 100, 50); titleLabel.TextColor = fingerPaint.Color.ToFormsColor(); // Determine the rotation angle. float tAngle = stopwatch.ElapsedMilliseconds % 5000 / 5000f; angle = 360 * tAngle; SKMatrix matrix = SKMatrix.MakeRotationDegrees(-angle, bitmap.Width / 2, bitmap.Height / 2); // Loop trough the fingers touching the screen. foreach (long id in idDictionary.Keys) { FingerInfo fingerInfo = idDictionary[id]; // Get the canvas size in pixels. It's square so it's only one number. float canvasSize = 0; if (canvasView is SKCanvasView) { canvasSize = (canvasView as SKCanvasView).CanvasSize.Width; } else { canvasSize = (canvasView as SKGLView).CanvasSize.Width; } // Convert Xamarin.Forms coordinates to pixels for drawing on the bitmap. // Also, make an offset factor if there's been resizing and the bitmap // is now larger than the canvas. (It's never smaller.) float factor = canvasSize / (float)canvasView.Width; // scaling factor float offset = (bitmapSize - canvasSize) / 2; // bitmap always >= canvas SKPoint convertedPoint = new SKPoint(factor * (float)fingerInfo.ThisPosition.X + offset, factor * (float)fingerInfo.ThisPosition.Y + offset); // Now rotate the point based on the rotation angle SKPoint pt0 = matrix.MapPoint(convertedPoint); if (!float.IsPositiveInfinity(fingerInfo.LastPosition.X)) { // Draw four lines in four quadrants. SKPoint pt1 = fingerInfo.LastPosition; bitmapCanvas.DrawLine(pt0.X, pt0.Y, pt1.X, pt1.Y, fingerPaint); float x0Flip = bitmap.Width - pt0.X; float y0Flip = bitmap.Height - pt0.Y; float x1Flip = bitmap.Width - pt1.X; float y1Flip = bitmap.Height - pt1.Y; bitmapCanvas.DrawLine(x0Flip, pt0.Y, x1Flip, pt1.Y, fingerPaint); bitmapCanvas.DrawLine(pt0.X, y0Flip, pt1.X, y1Flip, fingerPaint); bitmapCanvas.DrawLine(x0Flip, y0Flip, x1Flip, y1Flip, fingerPaint); } // Save the current point for next time through. fingerInfo.LastPosition = pt0; } // Redraw the canvas. if (canvasView is SKCanvasView) { (canvasView as SKCanvasView).InvalidateSurface(); } else { (canvasView as SKGLView).InvalidateSurface(); } return(true); }