当前位置: 首页 > news >正文

【MAUI】模糊控件(毛玻璃高斯模糊亚克力模糊)

在这里插入图片描述

文章目录

  • XAML
  • .CS
  • ToBytes方法
  • 使用
  • 效果

常试过AcrylicView.MAUI和Sharpnado.MaterialFrame,对于二者教程很少,使用直接写控件然后调属性,没有报错但也并没有效果所幸就自己写一个

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"x:Class="Namespace.BackgroundBlurView"xmlns:skia="clr-namespace:SkiaSharp.Views.Maui.Controls;assembly=SkiaSharp.Views.Maui.Controls"
><ContentView.Content><Grid RowDefinitions="*"><skia:SKCanvasView  Grid.Row="0"x:Name="canvasView"HorizontalOptions="Fill"PaintSurface="OnCanvasViewPaintSurface"VerticalOptions="Fill" /></Grid></ContentView.Content>
</ContentView>

.CS

using CSharpFunctionalExtensions;
using SkiaSharp;
using SkiaSharp.Views.Maui;namespace Namespace ;public partial class BackgroundBlurView : ContentView
{#region 可绑定属性public static readonly BindableProperty PathProperty =BindableProperty.Create(propertyName: nameof(Path),returnType: typeof(string),declaringType: typeof(BackgroundBlurView),defaultBindingMode: BindingMode.OneWay,propertyChanged: PathPropertyChanged);public string Path{get { return (string)base.GetValue(PathProperty); }set { base.SetValue(PathProperty, value); }}private static void PathPropertyChanged(BindableObject bindable, object oldValue, object newValue){var control = (BackgroundBlurView)bindable;MainThread.BeginInvokeOnMainThread(() =>{control.canvasView.InvalidateSurface();});}#endregionbyte[] _musicbgBytes;public BackgroundBlurView(){InitializeComponent();}void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args){SKImageInfo info = args.Info;SKSurface surface = args.Surface;SKCanvas canvas = surface.Canvas;canvas.Clear();float sigmaX = 20f;float sigmaY = 20f;using (SKPaint paint = new SKPaint()){// Set SKPaint propertiespaint.ImageFilter = SKImageFilter.CreateBlur(sigmaX, sigmaY);// Calculate rectangle for bitmapSKRect bitmapRect = new SKRect(0, 0, info.Width, info.Height);//bitmapRect.Inflate(-50, -50);using var stream = FileSystem.OpenAppPackageFileAsync("imgs/"+ Path).Result;_musicbgBytes = stream.ToBytes();if (_musicbgBytes==null){return;}SKBitmap bitmap = SKBitmap.Decode(_musicbgBytes);//SKBitmap.Decode("/Resources/Images/music/yinhe_bg.png");canvas.DrawBitmap(bitmap, bitmapRect, paint: paint);}}
}

ToBytes方法

public static byte[] ToBytes(this Stream stream)
{MemoryStream memoryStream = new MemoryStream();stream.CopyTo(memoryStream);return memoryStream.ToArray();
}

使用

        <view:BackgroundBlurView VerticalOptions="Fill" HorizontalOptions="Fill" Padding="-10" Path="{Binding BGImg}"></view:BackgroundBlurView>

效果

在这里插入图片描述

http://www.lryc.cn/news/467458.html

相关文章:

  • 深度学习:pandas篇
  • Redis学习文档(Redis基本数据类型【Hash、Set】)
  • 15分钟学Go 第9天:函数的定义与调用
  • Java虚拟机:JVM介绍
  • R数据科学 16.5.3练习题
  • 通过conda install -c nvidia cuda=“11.3.0“ 安装低版本的cuda,但是却安装了高版本的12.4.0
  • 简易CPU设计入门:验证取指令模块
  • 【MySQL数据库】MySQL主从复制
  • CDC变更数据捕捉技术是什么?和ETL有什么不同?
  • 一种用于推进欧洲临床中心中风管理的联邦学习平台即服务
  • 给哔哩哔哩bilibili电脑版做个手机遥控器
  • opencv dnn模块 示例(27) 目标检测 object_detection 之 yolov11
  • 鸿蒙开发融云demo初始化和登录
  • 手机防窥膜的工作原理是怎样的?有必要使用防窥膜吗?
  • 【Python_PySide6学习笔记(三十九)】基于QLineEdit实现自定义文本框,用于格式化文本,每四个字符后添加一个空格
  • 23种设计模式口诀速记
  • n > m 将输出文件 m 和 n 合并。 n < m 将输入文件 m 和 n 合并。 有什么区别
  • 语言障碍在自闭症儿童中的表现及应对
  • (成功解决)ubuntu22.04不小心更新成了atzlinux12.7.1,右上角出现红色错误符号
  • 005 C#语言基本元素概览,初识类型,变量与方法
  • Spring Cloud --- Sentinel 授权规则
  • 计算机网络基础 - 传输层(1)
  • Chrome DevTools:Console Performance 汇总篇
  • 【Spark | Spark-Core篇】RDD行动算子action
  • 23.Redis核心数据结构
  • 免费送源码:Node.JS+Express+MySQL Express 流浪动物救助系统 计算机毕业设计原创定制
  • 基于Java+Springboot+Vue开发的旅游景区管理系统
  • Python 实现的风控系统(使用了kafka、Faust、模拟drools、redis、分布式数据库)
  • Linux运维_Rocky8 安装配置Zabbix
  • jQuery Mobile 滚屏事件