C# Avalonia 16- Animation- SampleViewer - FollowExample

news/2025/10/22 6:14:43/文章来源:https://www.cnblogs.com/dalgleish/p/19156804

FollowExample.axaml代码

<UserControl xmlns="https://github.com/avaloniaui"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"x:Class="AvaloniaUI.FollowExample"><Canvas x:Name="containerCanvas" Background="transparent" PointerMoved="OnPointerMoved"><Rectangle x:Name="followRectangle" Canvas.Left="0" Canvas.Top="0" Fill="red" Width="50" Height="50" /></Canvas>
</UserControl>

FollowExample.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Shares.Avalonia;
using System;namespace AvaloniaUI;public partial class FollowExample : UserControl
{private readonly AnimationPlayer animation = new AnimationPlayer();private Point lastMousePosition = new Point(300, 200);private Vector rectangleVelocity = new Vector(0, 0);public FollowExample(){InitializeComponent();// 初始位置Canvas.SetLeft(followRectangle, lastMousePosition.X);Canvas.SetTop(followRectangle, lastMousePosition.Y);// 配置 AnimationPlayeranimation.Duration = double.MaxValue; // 无限播放animation.Loop = true;animation.Fps = 60;// 每帧执行“跟随逻辑”animation.At(0).PlayGlobal(UpdateRectangle);animation.Start();}private void OnPointerMoved(object? sender, PointerEventArgs e){lastMousePosition = e.GetPosition(containerCanvas);}private void UpdateRectangle(double globalProgress){// 当前矩形位置var location = new Point(Canvas.GetLeft(followRectangle),Canvas.GetTop(followRectangle));// 指向鼠标的向量Vector toMouse = lastMousePosition - location;// 施加“跟随力”double followForce = 0.01;rectangleVelocity += toMouse * followForce;// 阻尼系数(防止发散)double drag = 0.8;rectangleVelocity *= drag;// 更新位置location += rectangleVelocity;Canvas.SetLeft(followRectangle, location.X);Canvas.SetTop(followRectangle, location.Y);}
}

运行效果

image

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/942850.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

五节课掌握 Python 面向对象(以“人狗大战”为例)

五节课掌握 Python 面向对象(以“人狗大战”为例) 第一节课:OOP 入门——创建“人”和“狗”的基本模型 目标:掌握类、对象、实例属性和方法的基础语法,能创建简单的“人”和“狗”并让它们执行基本动作。什么是面…

ESP32 Arduino核心框架:全面支持多款ESP32芯片的开发平台

ESP32 Arduino核心框架是专为ESP32系列芯片打造的官方开发平台,支持ESP32、ESP32-C3、ESP32-C6、ESP32-H2、ESP32-P4、ESP32-S2和ESP32-S3等多款芯片,提供完整的Arduino兼容API和丰富的硬件外设驱动。ESP32 Arduino核…

联邦学习与AI公平性研究新进展

某中心与南加州大学联合成立的机器学习安全与可信中心宣布首批博士研究员入选,重点研究联邦学习、分布式机器学习及机器学习公平性等前沿技术领域。某中心与南加州大学联合机器学习中心选拔首批博士研究员 南加州大学…

图像分割 3D-Box-Segment-Anything(5)如何加速 - MKT

图像分割 3D-Box-Segment-Anything(5)如何加速 1算法目前有多个针对速度优化的SAM变体版本,以下是主流的快速版SAM模型: 1. ​​MobileSAM​​ ⭐ 最推荐​​特点​​:将SAM的ViT-H图像编码器替换为更轻量的Tiny…

静态方法(`@staticmethod`)和类方法(`@classmethod`)的应用场景及选择原则

在 Python 中,静态方法(@staticmethod)和类方法(@classmethod)都属于“类级别的方法”,但适用场景有明确区别。以下从核心特性出发,详细说明两者的应用场景及选择原则: 一、静态方法(@staticmethod):与类/实…

专门针对无人机分割的预训练模型​ - MKT

专门针对无人机分割的预训练模型​ 公开可用的模型​​​​UAVid数据集预训练模型​​专门用于无人机城市场景理解 包含道路、建筑物、植被等类别 下载:GitHub搜索"UAVid-Semantic-Segmentation"​​DroneS…

为什么一般教材在讲解python的多态概念时,不用抽象基类及其相关内容讲解呢?

在讲解多态时较少优先用 Python 抽象基类(ABC,Abstract Base Class),核心原因是 ABC 并非 Python 多态的“主流或默认实现方式”,它更偏向“静态语言风格的补充工具”,而 Python 多态的本质是由动态类型和鸭子类…

[Bash] bash scripting common pitfalls

Absolutely — Bash feels strange because it blends shell commands, programming constructs, and POSIX quirks. If you come from Python, it’s easy to trip over hidden pitfalls. Here’s a thorough list, o…

[Bash]让人头晕的if条件

来自chatgptAh, yes — Bash’s conditional syntax is notoriously confusing because it has several different “types” of tests, and each has subtle differences. Let’s break it down carefully. You were …

鸭子类型(Duck Typing)中的“类型”,指的是什么的类型?为什么很多人认为“Python 没有真正实现多态”?多态的核心目的是什么?鸭子类型如何实现多态?

鸭子类型(Duck Typing)中的“类型”,指的是什么的类型? 鸭子类型(Duck Typing)中的“类型”,指的是“具备特定行为的对象的类型”——它不是传统意义上“由类定义的类型”(如 int、str 或自定义类),而是“由…

tryhackme-预安全-windows基础-windows 基础知识1-16

tryhackme-Pre Security-Windows Fundamentals -Windows Fundamentals 1 房间地址:https://tryhackme.com/room/windowsfundamentals1xbx 这是网络安全入门的基础模块的计算机科学基础知识:Windows Fundamentals 1(…

YOLO11深度学习的遥感视角地面房屋建筑检测分割与分析系统 - MKT

YOLO11深度学习的遥感视角地面房屋建筑检测分割与分析系统 https://blog.csdn.net/qq_42589613/article/details/146162941一、软件核心功能介绍及效果演示软件主要功能1. 可进行遥感视角地面房屋建筑检测分割,分割一…

鸭子类型(Duck Typing)中的“类型”,指的是什么的类型?为什么很多人认为“Python 没有真正实现多态”

鸭子类型(Duck Typing)中的“类型”,指的是“具备特定行为的对象的类型”——它不是传统意义上“由类定义的类型”(如 int、str 或自定义类),而是“由对象具备的方法/属性(行为)所定义的逻辑类型”。简单说:“…

图像分割 Segment Anything(1-2)第二代 - MKT

图像分割 Segment Anything(1-2)第二代 大模型 8秒 1800*1200 压缩一半# 使用前需要先安装 SAM 2。代码需要python>=3.10、 以及torch>=2.5.1和。请按照此处的torchvision>=0.20.1说明安装 PyTorch 和 Tor…

对比c++中的多态和python的多态

C++ 和 Python 中的“多态”都围绕“同一接口、不同实现”的核心思想,但由于语言特性(静态类型 vs 动态类型)的差异,两者在实现方式、约束性、灵活性上有显著区别。以下从核心机制、实现条件、使用场景等维度对比:…

OAK-D-SR近红外相机 - MKT

OAK-D-SR近红外相机 https://www.oakchina.cn/2024/08/13/%E5%85%B7%E6%9C%89-sam2-%E5%88%86%E6%AE%B5%E7%9A%84-ndvi-%E6%97%A0%E4%BA%BA%E6%9C%BA/

结对项目-自动生成小学四则运算题目命令行程序

(一)这个作业属于哪个课程 https://edu.cnblogs.com/campus/gdgy/Class34Grade23ComputerScience这个作业要求在哪里 https://edu.cnblogs.com/campus/gdgy/Class34Grade23ComputerScience/homework/13479这个作业的…

tryhackme-预安全-linux 基础-Linux 基础知识(第二部分)-14

tryhackme-Pre Security-Linux Fundamentals-Linux Fundamentals Part 2 房间地址:https://tryhackme.com/room/linuxfundamentalspart2 这是网络安全入门的基础模块的计算机科学基础知识:Linux Fundamentals Part 2…

tryhackme-预安全-linux 基础-Linux 基础知识(第一部分)-13

tryhackme-Pre Security-Linux Fundamentals-Linux Fundamentals Part 1 房间地址:https://tryhackme.com/room/linuxfundamentalspart1 这是网络安全入门的基础模块的计算机科学基础知识:Linux Fundamentals Part 1…

我测试了七个主流后端框架的性能-结果让我重新思考了技术选型

说实话,在开始这次测试之前,我从来没想过性能差异会这么大。作为一个大三的计算机专业学生,我一直觉得框架选择主要看功能和生态,性能嘛,差不多就行了。直到上个月,我们实验室的一个项目因为并发量上来后服务器频…