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

打印 上一主题 下一主题

主题 1764|帖子 1764|积分 5292

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



  • 效果如下图:

    XAML代码:
  1. <Window x:Class="Caroursel.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:local="clr-namespace:Caroursel"
  6.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  7.         Title="MainWindow"
  8.         Width="1000"
  9.         Height="450"
  10.         mc:Ignorable="d">
  11.     <Window.Resources>
  12.         <Style x:Key="MaskGrid" TargetType="Grid">
  13.             <Setter Property="Background" Value="#5B000000" />
  14.             <Style.Triggers>
  15.                 <Trigger Property="IsMouseOver" Value="True">
  16.                     <Trigger.EnterActions>
  17.                         <BeginStoryboard>
  18.                             <Storyboard>
  19.                                 <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.2" />
  20.                             </Storyboard>
  21.                         </BeginStoryboard>
  22.                     </Trigger.EnterActions>
  23.                     <Trigger.ExitActions>
  24.                         <BeginStoryboard>
  25.                             <Storyboard>
  26.                                 <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.2" />
  27.                             </Storyboard>
  28.                         </BeginStoryboard>
  29.                     </Trigger.ExitActions>
  30.                 </Trigger>
  31.             </Style.Triggers>
  32.         </Style>
  33.         <Style x:Key="ContainerGrid" TargetType="Grid">
  34.             <EventSetter Event="MouseEnter" Handler="Grid_MouseEnter" />
  35.             <EventSetter Event="MouseLeave" Handler="Grid_MouseLeave" />
  36.         </Style>
  37.         <Style TargetType="TextBlock">
  38.             <Setter Property="FontSize" Value="18" />
  39.             <Setter Property="FontWeight" Value="Bold" />
  40.             <Setter Property="Foreground" Value="White" />
  41.             <Setter Property="TextWrapping" Value="Wrap" />
  42.             <Setter Property="Width" Value="18" />
  43.             <Setter Property="VerticalAlignment" Value="Center" />
  44.             <Setter Property="HorizontalAlignment" Value="Center" />
  45.         </Style>
  46.     </Window.Resources>
  47.     <Grid>
  48.         <Grid.ColumnDefinitions>
  49.             <ColumnDefinition Width="Auto" />
  50.             <ColumnDefinition Width="Auto" />
  51.             <ColumnDefinition Width="Auto" />
  52.             <ColumnDefinition Width="Auto" />
  53.             <ColumnDefinition Width="Auto" />
  54.         </Grid.ColumnDefinitions>
  55.         <Grid x:Name="Grid1"
  56.               Grid.Column="0"
  57.               Width="200"
  58.               Style="{StaticResource ContainerGrid}">
  59.             <Image HorizontalAlignment="Center"
  60.                    VerticalAlignment="Center"
  61.                    Source="1.jpg"
  62.                    Stretch="UniformToFill" />
  63.             <Grid Style="{StaticResource MaskGrid}">
  64.                 <TextBlock Text="迅捷斥候" />
  65.             </Grid>
  66.         </Grid>
  67.         <Grid x:Name="Grid2"
  68.               Grid.Column="1"
  69.               Width="200"
  70.               Style="{StaticResource ContainerGrid}">
  71.             <Image HorizontalAlignment="Center"
  72.                    VerticalAlignment="Center"
  73.                    Source="2.jpg"
  74.                    Stretch="UniformToFill" />
  75.             <Grid Style="{StaticResource MaskGrid}">
  76.                 <TextBlock Text="黑暗之女" />
  77.             </Grid>
  78.         </Grid>
  79.         <Grid x:Name="Grid3"
  80.               Grid.Column="2"
  81.               Width="200"
  82.               Style="{StaticResource ContainerGrid}">
  83.             <Image HorizontalAlignment="Center"
  84.                    VerticalAlignment="Center"
  85.                    Source="3.jpg"
  86.                    Stretch="UniformToFill" />
  87.             <Grid Style="{StaticResource MaskGrid}">
  88.                 <TextBlock Text="刀锋舞者" />
  89.             </Grid>
  90.         </Grid>
  91.         <Grid x:Name="Grid4"
  92.               Grid.Column="3"
  93.               Width="200"
  94.               Style="{StaticResource ContainerGrid}">
  95.             <Image HorizontalAlignment="Center"
  96.                    VerticalAlignment="Center"
  97.                    Source="4.jpg"
  98.                    Stretch="UniformToFill" />
  99.             <Grid Style="{StaticResource MaskGrid}">
  100.                 <TextBlock Text="诡术妖姬" />
  101.             </Grid>
  102.         </Grid>
  103.         <Grid x:Name="Grid5"
  104.               Grid.Column="4"
  105.               Width="200"
  106.               Style="{StaticResource ContainerGrid}">
  107.             <Image HorizontalAlignment="Right"
  108.                    VerticalAlignment="Center"
  109.                    Source="5.jpg"
  110.                    Stretch="UniformToFill" />
  111.             <Grid Style="{StaticResource MaskGrid}">
  112.                 <TextBlock Text="潮汐海灵" />
  113.             </Grid>
  114.         </Grid>
  115.     </Grid>
  116. </Window>
复制代码
C#代码:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Animation;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace Caroursel
  17. {
  18.         /// <summary>
  19.         /// MainWindow.xaml 的交互逻辑
  20.         /// </summary>
  21.         public partial class MainWindow : Window
  22.         {
  23.                 public MainWindow()
  24.                 {
  25.                         InitializeComponent();
  26.                         SizeChanged += MainWindow_SizeChanged;
  27.                 }
  28.                 private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
  29.                 {
  30.                         var targetWidth = Width / 5;
  31.                         DoubleAnimation da = new DoubleAnimation(targetWidth, new Duration(TimeSpan.FromSeconds(0.25)));
  32.                         Grid1.BeginAnimation(WidthProperty, da);
  33.                         Grid2.BeginAnimation(WidthProperty, da);
  34.                         Grid3.BeginAnimation(WidthProperty, da);
  35.                         Grid4.BeginAnimation(WidthProperty, da);
  36.                         Grid5.BeginAnimation(WidthProperty, da);
  37.                 }
  38.                 private void Grid_MouseEnter(object sender, MouseEventArgs e)
  39.                 {
  40.                         var grid = sender as Grid;
  41.                         var targetWidth = Width / 5 * 3;
  42.                         DoubleAnimation da = new DoubleAnimation(targetWidth, new Duration(TimeSpan.FromSeconds(0.3)));
  43.                         da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  44.                         grid.BeginAnimation(WidthProperty, da);
  45.                         var remainingWidth = Width - targetWidth;
  46.                         targetWidth = remainingWidth / 4;
  47.                         da = new DoubleAnimation(targetWidth, new Duration(TimeSpan.FromSeconds(0.25)));
  48.                         if (grid != Grid1)
  49.                         {
  50.                                 Grid1.BeginAnimation(WidthProperty, da);
  51.                         }
  52.                         if (grid != Grid2)
  53.                         {
  54.                                 Grid2.BeginAnimation(WidthProperty, da);
  55.                         }
  56.                         if (grid != Grid3)
  57.                         {
  58.                                 Grid3.BeginAnimation(WidthProperty, da);
  59.                         }
  60.                         if (grid != Grid4)
  61.                         {
  62.                                 Grid4.BeginAnimation(WidthProperty, da);
  63.                         }
  64.                         if (grid != Grid5)
  65.                         {
  66.                                 Grid5.BeginAnimation(WidthProperty, da);
  67.                         }
  68.                 }
  69.                 private void Grid_MouseLeave(object sender, MouseEventArgs e)
  70.                 {
  71.                         var targetWidth = Width / 5;
  72.                         DoubleAnimation da = new DoubleAnimation(targetWidth, new Duration(TimeSpan.FromSeconds(0.25)));
  73.                         Grid1.BeginAnimation(WidthProperty, da);
  74.                         Grid2.BeginAnimation(WidthProperty, da);
  75.                         Grid3.BeginAnimation(WidthProperty, da);
  76.                         Grid4.BeginAnimation(WidthProperty, da);
  77.                         Grid5.BeginAnimation(WidthProperty, da);
  78.                 }
  79.         }
  80. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

熊熊出没

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表