各大公司开源网站,企业网站维护工作内容,一般通过少女,陕西网站建设托管熟悉下SkiaSharp的基础操作#xff0c;这次搞个弹跳球效果#xff0c;实现后#xff0c;发现效果还真不错。大概效果如下:原理分析先是实现了网格效果#xff0c;这个比较简单#xff0c;直接横线#xff0c;竖线#xff0c;就OK了。另外一个就是随机一个圆形#xff0… 熟悉下SkiaSharp的基础操作这次搞个弹跳球效果实现后发现效果还真不错。大概效果如下:原理分析先是实现了网格效果这个比较简单直接横线竖线就OK了。另外一个就是随机一个圆形我这边随机了一百个初始位置大致都一样但是每个方向角度随机颜色随机移动速度随机。然后它们移动起来遇到了墙壁就会自动回弹回去形成了不错的视觉效果。Wpf 和 SkiaSharp新建一个WPF项目然后Nuget包即可 要添加Nuget包Install-Package SkiaSharp.Views.WPF -Version 2.88.0其中核心逻辑是这部分会以我设置的60FPS来刷新当前的画板。skContainer.PaintSurface SkContainer_PaintSurface;
_ Task.Run(()
{while (true){try{Dispatcher.Invoke(() {skContainer.InvalidateVisual();});_ SpinWait.SpinUntil(() false, 1000 / 60);//每秒60帧}catch{break;}}
});实现代码的圆形逻辑/// summary
/// 圆圈
/// /summary
internal class Circles
{private Random r new Random();public Circles(){VelocityX GetRandom(0, 3);VelocityY GetRandom(0, 3);Radius GetRandom(0, 50);Color new SKColor((byte)r.Next(0, 255), (byte)r.Next(0, 255), (byte)r.Next(0, 255));}public float X { get; set; } 100;public float Y { get; set; } 100;public float VelocityX { get; set; }public float VelocityY { get; set; }public float Radius { get; set; }public SKColor Color { get; set; }public float GetRandom(int min, int max){var result r.Next(min * 100, max * 100);return (float)(result / 100.0);}
}圆形的移动逻辑/// summary
/// 调整位置
/// /summary
public void AdjustPosition(SKCanvas canvas, SKTypeface Font, int Width, int Height)
{foreach (var circle in circles){using var paint new SKPaint{Color circle.Color,Style SKPaintStyle.Fill,IsAntialias true,StrokeWidth 1};canvas.DrawCircle(circle.X, circle.Y, circle.Radius, paint);if (circle.X circle.VelocityX circle.Radius Width || circle.X circle.VelocityX - circle.Radius 0){circle.VelocityX -circle.VelocityX;}if (circle.Y circle.VelocityY circle.Radius Height || circle.Y circle.VelocityY - circle.Radius 0){circle.VelocityY -circle.VelocityY;}circle.X circle.VelocityX;circle.Y circle.VelocityY;}
}实现网格的逻辑/// summary
/// 画格子
/// /summary
public void DrawGrid(SKCanvas canvas, SKColor sKColor, int Width, int Height, int StepX, int StepY)
{using var paint new SKPaint{Color sKColor,Style SKPaintStyle.Stroke,StrokeWidth 0.5f,IsStroke true,IsAntialias true};for (var i 0.5; i Width; i StepX){var path new SKPath();path.MoveTo((float)i, 0);path.LineTo((float)i, Height);path.Close();canvas.DrawPath(path, paint);}for (var i 0.5; i Height; i StepY){var path new SKPath();path.MoveTo(0, (float)i);path.LineTo(Width, (float)i);path.Close();canvas.DrawPath(path, paint);}
}
}效果看着效果还是真不错。总结这个案例搞定下一次想想做个啥案例好点。代码地址https://github.com/kesshei/BouncingBallsDemo.githttps://gitee.com/kesshei/BouncingBallsDemo.git阅一键三连呦感谢大佬的支持您的支持就是我的动力!