WPF使用Animation仿WeChat(微信)播放语音消息
WPF开发者QQ群: 340500857
前言
WPF使用Animation仿WeChat(微信)播放语音消息?
效果图:
创建MyAnimationForever.cs如下:
public class MyAnimationForever : Control{private static Storyboard MyStory;private ObjectAnimationUsingKeyFrames MyAnimation;private UIElement animation;public static readonly DependencyProperty DurationProperty =DependencyProperty.Register("Duration", typeof(TimeSpan),typeof(MyAnimationForever), new PropertyMetadata(null));/// <summary>/// 动画时间/// </summary>public TimeSpan Duration{get { return (TimeSpan)GetValue(DurationProperty); }set { SetValue(DurationProperty, value); }}public static readonly DependencyProperty IsLitProperty =DependencyProperty.Register("IsLit", typeof(bool),typeof(MyAnimationForever), new PropertyMetadata(false, new PropertyChangedCallback(OnIsLitChanged)));/// <summary>/// 是否开始播放/// </summary>public bool IsLit{get { return (bool)GetValue(IsLitProperty); }set { SetValue(IsLitProperty, value); }}public override void OnApplyTemplate(){base.OnApplyTemplate();animation = Template.FindName("animation", this) as UIElement;if (animation != null && IsLit)Animate(animation);}private static void OnIsLitChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){bool newValue = (bool)e.NewValue;if (newValue){MyAnimationForever c = d as MyAnimationForever;if (c != null && c.animation != null){c.Animate(c.animation);}}else{MyStory.Stop();}}private void Animate(UIElement animation){Storyboard.SetTarget(MyAnimation, animation);Storyboard.SetTargetProperty(MyAnimation, new PropertyPath(Image.SourceProperty));MyStory.Children.Add(MyAnimation);//将动画添加到动画板中Console.WriteLine($"一共添加:{MyAnimation.KeyFrames.Count} 个 DiscreteObjectKeyFrame。");MyStory.Begin();}public MyAnimationForever(){MyStory = new Storyboard();MyAnimation = new ObjectAnimationUsingKeyFrames();MyAnimation.FillBehavior = FillBehavior.Stop;MyAnimation.RepeatBehavior = RepeatBehavior.Forever;MyStory.CurrentTimeInvalidated += (s, e) => {Clock storyboardClock = (Clock)s;Console.WriteLine(storyboardClock.CurrentTime.ToString());if (storyboardClock.CurrentTime >= Duration){IsLit = false;}};MyAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame() {Value = new BitmapImage(new Uri("pack://application:,,,/Images/0.png")), KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.33))});MyAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame(){Value = new BitmapImage(new Uri("pack://application:,,,/Images/1.png")),KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.66))});MyAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame(){Value = new BitmapImage(new Uri("pack://application:,,,/Images/2.png")),KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.99))});}}
创建MainWindow.xaml:
<Window x:Class="WpfAnimationWeChat.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfAnimationWeChat"mc:Ignorable="d" WindowState="Maximized"Title="MainWindow" Height="450" Width="800"><Window.Resources><ControlTemplate x:Key="ct" TargetType="local:MyCustomControl"><Image x:Name="animation" Height="20" Width="20" Source="/WpfAnimationWeChat;component/Images/2.png"/></ControlTemplate></Window.Resources><Grid><Viewbox><Grid Width="1240" Height="768"><Grid Height="28" Width="100" MouseLeftButtonDown="Grid_MouseLeftButtonDown"><Rectangle RadiusX="4" RadiusY="4" Fill="#9eea6a" /><StackPanel Orientation="Horizontal" Margin="4,0"><!--可以设置MyCustomControl的Duration 和 IsLit(点击的时候执行)的{binding}--><local:MyAnimationForever x:Name="AudioPlay" Template="{StaticResource ct}" Duration="0:00:10" IsLit="False"/><TextBlock Text="10ms”" VerticalAlignment="Center" FontSize="20"/></StackPanel></Grid></Grid></Viewbox></Grid>
</Window>
MainWindow.xaml.cs:
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){if (this.AudioPlay.IsLit){this.AudioPlay.IsLit = false;}else{this.AudioPlay.IsLit = true;}}
新增三张图片资源。
WPF开发者QQ群: 340500857
blogs: https://www.cnblogs.com/yanjinhua
Github:https://github.com/yanjinhuagood
作者:驚鏵
出处:https://www.cnblogs.com/yanjinhua
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
转载请著名作者 出处 https://github.com/yanjinhuagood
源码地址
Github:https://github.com/yanjinhuagood/WpfAnimationWeChat
Gitee:https://gitee.com/yanjinhua/WpfAnimationWeChat.git
WPF使用Animation仿WeChat(微信)播放语音消息相关推荐
- android仿微信语音聊天功能,Android仿微信发送语音消息的功能及示例代码
微信的发送语音是有一个向上取消的,我们使用ontouchlistener来监听手势,然后做出相应的操作就行了. 直接上代码: //语音操作对象 private mediaplayer mplayer ...
- Android仿微信发送语音消息动态提示,支持上滑取消发送
Android仿微信发送语音消息动态提示,支持上滑取消发送 先来几张图说明一下,简单直接: 是不是看了图片就秒懂 了. 下面来分析代码实现,直接撸代码. 主页面 AudioSendActivity.j ...
- 微信朋友圈点赞如何设计测试用例,微信发语音消息,购物车,支付页面如何设计软件测试用例?
做过软件测试或者面试过的人应该了解这个问题你肯定被问过,属于面试必问? 就算没遇到,预测你以后面试肯定会遇到,希望你能想到想到我分享的思路. 那么如何轻松优雅的回答这个题目,我给大家分享一个万能公式, ...
- 极客日报:砍价永远差一刀?拼多多法庭上回复:小数点后有6位;微信加入语音消息暂停功能;谷歌和IBM提议建立关键开源项目清单
一分钟速览新闻点! 砍价永远差一刀?拼多多法庭上回复:小数点后有6位 微信加入语音消息暂停功能,网友:还需要进度条 原极狐汽车"争议总裁"于立国转投小米汽车担任副总 丁磊回应&qu ...
- 腾讯否认微信测试语音消息进度调节;监证会同意蚂蚁集团科创板IPO注册;React 17 正式版发布|极客头条
整理 | 郑丽媛 头图 | CSDN 下载自东方 IC 「极客头条」-- 技术人员的新闻圈! CSDN 的读者朋友们早上好哇,「极客头条」来啦,快来看今天都有哪些值得我们技术人关注的重要新闻吧. 国内 ...
- iOS 仿微信发送语音消息按钮 - 手势按钮(一)
最近在做基于XMPP的IM,开发到发送语音消息的功能.在某度上搜了很久也没有找到适合的方法.索性自己琢磨了一个,提供给大家参考.(其中找到的很多文章都是一个复制另一个的,很烦!没格式没头没尾的.给不了 ...
- 04环信聊天界面 - 播放语音消息
分析:需要监听messageLabel的点击事件,然后播放 1.在chatCell里给messageLabel添加点击事件 /*** 初始化*/ -(void)awakeFromNib {// 1.给 ...
- android 微信播放语音文件,如何将微信语音导出为MP3音频文件-Android平台
本文对于安卓用户,提供了一种相对快捷的方法. ---------以下为图文,请在本文最后点击"阅读原文"查看视频教程-------- ios平台下,导出微信语音只需要两个步骤:使用 ...
- Android录制或播放语音消息时关闭其他媒体播放
1.发送系统广播: Intent i = new Intent("com.android.music.musicservicecommand"); i.putExtra(" ...
最新文章
- 第二届数据标准化及治理奖评选顺利结束
- php适配器模式应用,什么是适配器模式,它有哪些应用场景
- MCMC采样和M-H采样
- C:\Python27\python.exe: can't open file '2.py': [Errno 2] No such file or directory
- 为什么linux默认都没有MP3和视频或者连FLASH都没有呢?
- .net core 生成二维码
- hive求差集和交集
- POSIX 线程详解
- 【ACL 2021】基于一致性正则的跨语言微调方法
- vue后台如何刷新过期的token_Vue刷新token,判断token是否过期
- JFinal开发8个常见问题
- 翻译:算法常见的模数1000000007 模数10 ^ 9 + 7
- 使用java实现数据库编程 第一章:数据库的设计
- 怎么修改探索者服务器地址,探索者可不可以修改快捷键???
- 达尔优108机械合金版键盘驱动 正式版
- Windows开机启动项/自启动项文件夹位置
- 数据库-表的定义与操作
- phpwind不支持php7,PHP论坛程序PHPWind 7发布 新增四个功能
- OpenSSL安全特征问题漏洞(CVE-2022-2068)版本1.0.2k-fips升级到3.0.5
- 将 ChatGPT 引入你的飞书