微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 SkiaSharp 在线上绘制路径

如何解决使用 SkiaSharp 在线上绘制路径

当我尝试使用 SkiaSharp 绘制路径时,如您所见,路径位于线下方。我怎样才能把它带过线?

enter image description here

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

using SkiaSharp;
using SkiaSharp.Views.Forms;
using TouchTracking;

namespace SenzorApp
{
    public partial class MainPage : ContentPage
    {
        Dictionary<long,SKPath> inProgresspaths = new Dictionary<long,SKPath>();
        List<SKPath> completedpaths = new List<SKPath>();
        SKBitmap saveBitmap;

        SKPaint paint = new SKPaint
        {
            Style = SKPaintStyle.stroke,strokeWidth = 80,strokeCap = SKstrokeCap.Round,strokeJoin = SKstrokeJoin.Round,};

        public MainPage()
        {
            InitializeComponent();
        }

        float scale;            // ranges from 0 to 1 to 0
        void OnCanvasViewPaintSurface(object sender,SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info = args.Info;
            SKSurface surface = args.Surface;
            SKCanvas canvas = surface.Canvas;

            // Create bitmap the size of the display surface
            if (saveBitmap == null)
            {
                saveBitmap = new SKBitmap(info.Width,info.Height);
            }
            // Or create new bitmap for a new size of display surface
            else if (saveBitmap.Width < info.Width || saveBitmap.Height < info.Height)
            {
                SKBitmap newBitmap = new SKBitmap(Math.Max(saveBitmap.Width,info.Width),Math.Max(saveBitmap.Height,info.Height));

                using (SKCanvas newCanvas = new SKCanvas(newBitmap))
                {
                    newCanvas.Clear();
                    newCanvas.DrawBitmap(saveBitmap,0);
                }

                saveBitmap = newBitmap;
                
            }

            // Render the bitmap
            canvas.Clear();
            canvas.DrawBitmap(saveBitmap,0);

            using (SKPaint paint = new SKPaint())
            {
                paint.Style = SKPaintStyle.Fill;
                paint.Color = SKColors.Red;
                paint.strokeWidth = 50;
                canvas.DrawLine(-info.Width,info.Height / 2,info.Width,paint);
            }
        }

        void OnTouchEffectAction(object sender,TouchActionEventArgs args)
        {
            switch (args.Type)
            {
                case TouchActionType.pressed:
                    if (!inProgresspaths.ContainsKey(args.Id))
                    {
                        SKPath path = new SKPath();
                        
                        path.Moveto(ConvertToPixel(args.Location));
                        inProgresspaths.Add(args.Id,path);
                        UpdateBitmap();
                    }
                    break;

                case TouchActionType.Moved:
                    if (inProgresspaths.ContainsKey(args.Id))
                    {
                        SKPath path = inProgresspaths[args.Id];
                        path.Lineto(ConvertToPixel(args.Location));
                        UpdateBitmap();
                    }
                    break;

                case TouchActionType.Released:
                    if (inProgresspaths.ContainsKey(args.Id))
                    {
                        completedpaths.Add(inProgresspaths[args.Id]);
                        inProgresspaths.Remove(args.Id);
                        UpdateBitmap();
                    }
                    break;

                case TouchActionType.Cancelled:
                    if (inProgresspaths.ContainsKey(args.Id))
                    {
                        inProgresspaths.Remove(args.Id);
                        UpdateBitmap();
                    }
                    break;
            }
        }

        void UpdateBitmap()
        {
            using (SKCanvas saveBitmapCanvas = new SKCanvas(saveBitmap))
            {
                saveBitmapCanvas.Clear();

                foreach (SKPath path in completedpaths)
                {
                    saveBitmapCanvas.DrawPath(path,paint);
                }

                foreach (SKPath path in inProgresspaths.Values)
                {
                    saveBitmapCanvas.DrawPath(path,paint);
                }
            }

            canvasView.InvalidateSurface();
        }

        SKPoint ConvertToPixel(Point pt)
        {
            return new SKPoint((float)(canvasView.CanvasSize.Width * pt.X / canvasView.Width),(float)(canvasView.CanvasSize.Height * pt.Y / canvasView.Height));
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。