WPF轮播图动画交互 动画缩放展示图片

WPF轮播图动画交互 动画缩放展示图片

  • 效果如下图:

    XAML代码:
xml 复制代码
<Window x:Class="Caroursel.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:Caroursel"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Width="1000"
        Height="450"
        mc:Ignorable="d">
    <Window.Resources>
        <Style x:Key="MaskGrid" TargetType="Grid">
            <Setter Property="Background" Value="#5B000000" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.2" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.2" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style x:Key="ContainerGrid" TargetType="Grid">
            <EventSetter Event="MouseEnter" Handler="Grid_MouseEnter" />
            <EventSetter Event="MouseLeave" Handler="Grid_MouseLeave" />
        </Style>
        <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="18" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="TextWrapping" Value="Wrap" />
            <Setter Property="Width" Value="18" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="HorizontalAlignment" Value="Center" />
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid x:Name="Grid1"
              Grid.Column="0"
              Width="200"
              Style="{StaticResource ContainerGrid}">
            <Image HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Source="1.jpg"
                   Stretch="UniformToFill" />
            <Grid Style="{StaticResource MaskGrid}">
                <TextBlock Text="迅捷斥候" />
            </Grid>

        </Grid>

        <Grid x:Name="Grid2"
              Grid.Column="1"
              Width="200"
              Style="{StaticResource ContainerGrid}">
            <Image HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Source="2.jpg"
                   Stretch="UniformToFill" />
            <Grid Style="{StaticResource MaskGrid}">
                <TextBlock Text="黑暗之女" />
            </Grid>
        </Grid>

        <Grid x:Name="Grid3"
              Grid.Column="2"
              Width="200"
              Style="{StaticResource ContainerGrid}">
            <Image HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Source="3.jpg"
                   Stretch="UniformToFill" />
            <Grid Style="{StaticResource MaskGrid}">
                <TextBlock Text="刀锋舞者" />
            </Grid>
        </Grid>

        <Grid x:Name="Grid4"
              Grid.Column="3"
              Width="200"
              Style="{StaticResource ContainerGrid}">
            <Image HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Source="4.jpg"
                   Stretch="UniformToFill" />
            <Grid Style="{StaticResource MaskGrid}">
                <TextBlock Text="诡术妖姬" />
            </Grid>
        </Grid>

        <Grid x:Name="Grid5"
              Grid.Column="4"
              Width="200"
              Style="{StaticResource ContainerGrid}">
            <Image HorizontalAlignment="Right"
                   VerticalAlignment="Center"
                   Source="5.jpg"
                   Stretch="UniformToFill" />
            <Grid Style="{StaticResource MaskGrid}">
                <TextBlock Text="潮汐海灵" />
            </Grid>
        </Grid>
    </Grid>
</Window>

C#代码:

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Caroursel
{
	/// <summary>
	/// MainWindow.xaml 的交互逻辑
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
			SizeChanged += MainWindow_SizeChanged;
		}

		private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			var targetWidth = Width / 5;
			DoubleAnimation da = new DoubleAnimation(targetWidth, new Duration(TimeSpan.FromSeconds(0.25)));
			Grid1.BeginAnimation(WidthProperty, da);
			Grid2.BeginAnimation(WidthProperty, da);
			Grid3.BeginAnimation(WidthProperty, da);
			Grid4.BeginAnimation(WidthProperty, da);
			Grid5.BeginAnimation(WidthProperty, da);
		}

		private void Grid_MouseEnter(object sender, MouseEventArgs e)
		{
			var grid = sender as Grid;
			var targetWidth = Width / 5 * 3;
			DoubleAnimation da = new DoubleAnimation(targetWidth, new Duration(TimeSpan.FromSeconds(0.3)));
			da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
			grid.BeginAnimation(WidthProperty, da);

			var remainingWidth = Width - targetWidth;
			targetWidth = remainingWidth / 4;
			da = new DoubleAnimation(targetWidth, new Duration(TimeSpan.FromSeconds(0.25)));
			if (grid != Grid1)
			{
				Grid1.BeginAnimation(WidthProperty, da);
			}
			if (grid != Grid2)
			{
				Grid2.BeginAnimation(WidthProperty, da);
			}
			if (grid != Grid3)
			{
				Grid3.BeginAnimation(WidthProperty, da);
			}
			if (grid != Grid4)
			{
				Grid4.BeginAnimation(WidthProperty, da);
			}
			if (grid != Grid5)
			{
				Grid5.BeginAnimation(WidthProperty, da);
			}

		}

		private void Grid_MouseLeave(object sender, MouseEventArgs e)
		{
			var targetWidth = Width / 5;
			DoubleAnimation da = new DoubleAnimation(targetWidth, new Duration(TimeSpan.FromSeconds(0.25)));
			Grid1.BeginAnimation(WidthProperty, da);
			Grid2.BeginAnimation(WidthProperty, da);
			Grid3.BeginAnimation(WidthProperty, da);
			Grid4.BeginAnimation(WidthProperty, da);
			Grid5.BeginAnimation(WidthProperty, da);
		}
	}
}
相关推荐
newbe365244 小时前
我们如何使用 impeccable 优化前端界面设计与实现稳定性
前端·人工智能·分布式·github·aigc·wpf
Chris _data18 天前
WPF 学习第三天 — Modbus RTU 串口通信
hadoop·学习·wpf
布吉岛的石头19 天前
Java 程序员第 43 阶段05:微服务整合大模型,跨服务调用架构设计实战,Seata分布式事务实战
wpf
步步为营DotNet19 天前
基于.NET Aspire 实现云原生应用的高效监控与可观测性
云原生·.net·wpf
芒鸽19 天前
HarmonyOS 分布式开发实战:设备协同、数据共享与跨设备迁移
分布式·wpf·harmonyos
Volunteer Technology19 天前
Flink状态管理与容错(二)
大数据·flink·wpf
happyprince20 天前
07_verl-Trainer模块详解
人工智能·架构·wpf·强化学习
bugcome_com20 天前
WPF + Prism 技术指南与实战项目(二、模板搭建)
wpf
小满Autumn20 天前
log4net 日志框架 — 从配置到实战速查手册
笔记·c#·.net·wpf·上位机·log4net
政沅同学21 天前
基于 C# WPF + HALCON 的工业视觉算法工具框架(开源)
开发语言·c#·wpf