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

SkiaSharp 触摸位图图像

如何解决SkiaSharp 触摸位图图像

在应用程序中,我试图开发的一个关键部分是获取用户触摸的位置。起初我想使用点击手势识别器,但在快速谷歌搜索后我发现这是无用的(参见 here 示例)。

然后我相信我发现了 SkiaSharp 并且在学习如何使用它之后,至少在某种程度上,我仍然不确定我如何获得正确的触摸坐标。以下是我项目中与问题相关的代码部分。

  1. 画布触控功能

     private void canvasView_Touch(object sender,SKTouchEventArgs e)
     {
         // Only carry on with this function if the image is already on screen.
         if(m_isImagedisplayed)
         {
             // Use switch to get what type of action occurred.
             switch (e.ActionType)
             {
                 case SKTouchAction.pressed:
                     TouchImage(e.Location);
    
                     // Update simply tries to draw a small square using double for loops.
                     m_editedBm = Update(sender);
    
                     // Refresh screen.
                     (sender as SKCanvasView).InvalidateSurface();
                     break;
                 default:
                     break;
             }
         }
     }
    
  2. 触摸图片

     private void TouchImage(SKPoint point)
     {
         // Is the point in range of the canvas?
         if(point.X >= m_x && point.X <= (m_editedCanvasSize.Width + m_x) &&
             point.Y >= m_y && point.Y <= (m_editedCanvasSize.Height + m_y))
         {
             // Save the point for later and set the boolean to true so the algorithm can begin.
             m_clickPoint = point;
    
             m_updatealgorithm = true;
         }
     }
    

在这里,我只是查看或尝试查看单击的点是否在图像范围内,并且我创建了一个不同的 SkSize 变量来提供帮助。忽略布尔值,没那么重要。

  1. 更新函数(尝试在按下的点上绘制的函数,因此它是最重要的)

     public SKBitmap Update(object sender) 
     {
         // Create the default test color to replace current pixel colors in the bitmap.
         SKColor color = new SKColor(255,255,255);
    
         // Create a new surface with the current bitmap.
         using (var surface = new SKCanvas(m_editedBm))
         {
             /* According to this:  https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/paths/finger-paint,the points I have to start are in Xamarin forms coordinates,but I need to translate them to SkiaSharp coordinates which are in
               pixels. */
             Point pt = new Point((double)m_touchPoint.X,(double)m_touchPoint.Y);
             SKPoint newPoint = ConvertToPixel(pt);
    
             // Loop over the touch point start,then go to a certain value (like x + 100) just to get a "block" that's been altered for pixels.
             for (int x = (int)newPoint.X; x < (int)newPoint.X + 200.0f; ++x)
             {
                 for (int y = (int)newPoint.Y; y < (int)newPoint.Y + 200.0f; ++y)
                 {
                     // According to the x and y,change the color.
                     m_editedBm.SetPixel(x,y,color);
                 }
             }
    
             return m_editedBm;
         }
     }
    

在这里,我想它会在我按下的坐标处开始(并且由于“TouchImage”功能,这些坐标已被确认在图像范围内。当它确实得到正确的坐标(或至少应该这样做)正方形将一次绘制一条“线”。我有游戏编程背景,所以这种听起来很简单,但我不敢相信我没有得到这个第一次就对了。

我还有另一个功能,它可能被证明是值得的,因为原始图像被旋转然后放在屏幕上。为什么?好吧,认情况下,图像在拍摄后显示,然后向左旋转。我不知道为什么,但我用以下功能纠正了它:

    // Just rotate the image because for some reason it's titled 90 degrees to the left.
    public static SKBitmap Rotate()
    {
        using (var bitmap = m_bm)
        {
            // The new ones width IS the old ones height. 
            var rotated = new SKBitmap(bitmap.Height,bitmap.Width);

            using (var surface = new SKCanvas(rotated))
            {
                surface.Translate(rotated.Width,0.0f);
                surface.Rotatedegrees(90);
                surface.DrawBitmap(bitmap,0);
            }

            return rotated;
        }
    }

我会继续阅读和查找有关我做错了什么的东西,但如果能得到任何帮助,我将不胜感激。

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