先放效果图:

首先,建立一个RippleHelper.cs文件,然后建立以下附加属性:

IsFillEnable:是否扩大到整个控件

RippleDuration:持续时间

RippleRadius:不扩大到整个控件时的最大半径

RippleColor:波纹的颜色

        public static bool GetIsFillEnable(DependencyObject obj)
{
return (bool)obj.GetValue(IsFillEnableProperty);
} public static void SetIsFillEnable(DependencyObject obj, bool value)
{
obj.SetValue(IsFillEnableProperty, value);
} public static readonly DependencyProperty IsFillEnableProperty =
DependencyProperty.RegisterAttached("IsFillEnable", typeof(bool), typeof(RippleHelper), new PropertyMetadata(false)); public static TimeSpan GetRippleDuration(UIElement obj)
{
return (TimeSpan)obj.GetValue(RippleDurationProperty);
} public static void SetRippleDuration(UIElement obj, TimeSpan value)
{
obj.SetValue(RippleDurationProperty, value);
} public static readonly DependencyProperty RippleDurationProperty =
DependencyProperty.RegisterAttached("RippleDuration", typeof(TimeSpan), typeof(RippleHelper), new PropertyMetadata(TimeSpan.FromMilliseconds())); public static double GetRippleRadius(UIElement obj)
{
return (double)obj.GetValue(RippleRadiusProperty);
} public static void SetRippleRadius(UIElement obj, double value)
{
obj.SetValue(RippleRadiusProperty, value);
} public static readonly DependencyProperty RippleRadiusProperty =
DependencyProperty.RegisterAttached("RippleRadius", typeof(double), typeof(RippleHelper), new PropertyMetadata(100d)); public static Color GetRippleColor(UIElement obj)
{
return (Color)obj.GetValue(RippleColorProperty);
} public static void SetRippleColor(UIElement obj, Color value)
{
obj.SetValue(RippleColorProperty, value);
} public static readonly DependencyProperty RippleColorProperty =
DependencyProperty.RegisterAttached("RippleColor", typeof(Color), typeof(RippleHelper), new PropertyMetadata(Colors.White));

接下来再写一个附加属性和一个enum

public static RippleHelperState GetRippleHelperState(UIElement obj)
{
return (RippleHelperState)obj.GetValue(RippleHelperStateProperty);
} public static void SetRippleHelperState(UIElement obj, RippleHelperState value)
{
obj.SetValue(RippleHelperStateProperty, value);
} public static readonly DependencyProperty RippleHelperStateProperty =
DependencyProperty.RegisterAttached("RippleHelperState", typeof(RippleHelperState), typeof(RippleHelper), new PropertyMetadata(RippleHelperState.None, (s, e) =>
{
if (e.NewValue != null && e.OldValue != e.NewValue)
{
var value = (RippleHelperState)e.NewValue;
var oldvalue = (RippleHelperState)e.OldValue;
if (s is UIElement ele)
{
switch (value)
{
case RippleHelperState.Pressed:
{
ele.RemoveHandler(UIElement.PointerReleasedEvent, pointerEventHandler);
ele.AddHandler(UIElement.PointerPressedEvent, pointerEventHandler, true);
}
break; case RippleHelperState.Released:
{
ele.RemoveHandler(UIElement.PointerPressedEvent, pointerEventHandler);
ele.AddHandler(UIElement.PointerReleasedEvent, pointerEventHandler, true);
}
break; case RippleHelperState.None:
{
ele.RemoveHandler(UIElement.PointerPressedEvent, pointerEventHandler);
ele.RemoveHandler(UIElement.PointerReleasedEvent, pointerEventHandler);
ElementCompositionPreview.SetElementChildVisual(ele, null);
}
break;
}
}
}
}));

在命名空间里建立enum

    public enum RippleHelperState
{
Pressed, Released, None
}

然后编写两个鼠标事件,对应RippleHelperState的Pressed和Released两个状态

        private static void Ele_PointerPressed(object sender, PointerRoutedEventArgs e)
{
if (sender is UIElement ele)
{
var position = e.GetCurrentPoint(ele).Position.ToVector2();
StartRippleAnimation(ele, position);
}
} private static void Ele_PointerReleased(object sender, PointerRoutedEventArgs e)
{
if (sender is UIElement ele)
{
var position = e.GetCurrentPoint(ele).Position.ToVector2();
StartRippleAnimation(ele, position);
}
}
        public static void StartRippleAnimation(UIElement ele, Vector2 position)
{
StartRippleAnimation(ele, position, GetRippleColor(ele), GetIsFillEnable(ele), GetRippleDuration(ele), GetRippleRadius(ele));
} public static void StartRippleAnimation(UIElement ele, Vector2 position, Color color, bool isFillEnable, TimeSpan duration, double radius = )
{
var hostVisual = ElementCompositionPreview.GetElementVisual(ele);
var cVisual = ElementCompositionPreview.GetElementChildVisual(ele) as ContainerVisual;
if (cVisual == null)
{
cVisual = compositor.CreateContainerVisual();
SizeBind.ClearParameter("hostVisual");
SizeBind.SetReferenceParameter("hostVisual", hostVisual);
cVisual.StartAnimation("Size", SizeBind);
cVisual.Clip = compositor.CreateInsetClip();
ElementCompositionPreview.SetElementChildVisual(ele, cVisual);
} var sVisual = CreateSpriteVisual(ele, color);
cVisual.Children.InsertAtTop(sVisual);
sVisual.Offset = new Vector3(position.X, position.Y, 0f); if (isFillEnable)
{
var nWidth = Math.Max(Math.Max(position.X, ele.RenderSize.Width - position.X), Math.Max(position.Y, ele.RenderSize.Height - position.Y));
var r = Math.Sqrt(nWidth * nWidth * );
var finalScale = (float)r / 45f;
PropSet.InsertScalar("ScaleValue", finalScale);
ScaleAnimation.Duration = TimeSpan.FromMilliseconds();
OpacityAnimation.Duration = TimeSpan.FromMilliseconds();
}
else
{
if (radius == 100d)
{
PropSet.InsertScalar("ScaleValue", 2f);
}
else
{
PropSet.InsertScalar("ScaleValue", (float)GetRippleRadius(ele) / 45f);
}
} ScaleAnimation.Duration = duration;
OpacityAnimation.Duration = duration; var batch = compositor.GetCommitBatch(CompositionBatchTypes.Animation);
batch.Completed += (s1, e1) =>
{
OnRippleComplated(ele);
cVisual.Children.Remove(sVisual);
};
sVisual.StartAnimationGroup(RippleAnimationGroup);
}

动画完成的事件:

        public static event EventHandler RippleComplated;

        private static void OnRippleComplated(UIElement ele)
{
RippleComplated?.Invoke(ele, EventArgs.Empty);
}

最后在类的开头编写Composition的动画和资源:

        private static readonly PointerEventHandler pointerEventHandler = new PointerEventHandler(Ele_PointerReleased);
private static Compositor compositor => Window.Current.Compositor;
private static ExpressionAnimation _SizeBind;
private static CompositionEasingFunction _EaseOut;
private static ScalarKeyFrameAnimation _OpacityAnimation;
private static Vector3KeyFrameAnimation _ScaleAnimation;
private static CompositionAnimationGroup _RippleAnimationGroup;
private static CompositionPropertySet _PropSet;
private static CompositionBrush _Mask; private static ExpressionAnimation SizeBind
{
get
{
if (_SizeBind == null) _SizeBind = compositor.CreateExpressionAnimation("hostVisual.Size");
return _SizeBind;
}
} private static CompositionEasingFunction EaseOut
{
get
{
if (_EaseOut == null) _EaseOut = compositor.CreateCubicBezierEasingFunction(new Vector2(0f, 0f), new Vector2(0.9f, 1f));
return _EaseOut;
}
} private static ScalarKeyFrameAnimation OpacityAnimation
{
get
{
if (_OpacityAnimation == null)
{
_OpacityAnimation = compositor.CreateScalarKeyFrameAnimation();
_OpacityAnimation.InsertKeyFrame(0f, 1f, EaseOut);
_OpacityAnimation.InsertKeyFrame(1f, 0f, EaseOut);
_OpacityAnimation.Duration = TimeSpan.FromMilliseconds();
_OpacityAnimation.Target = "Opacity";
}
return _OpacityAnimation;
}
} private static Vector3KeyFrameAnimation ScaleAnimation
{
get
{
if (_ScaleAnimation == null)
{
_ScaleAnimation = compositor.CreateVector3KeyFrameAnimation();
_ScaleAnimation.InsertKeyFrame(0f, new Vector3(0f, 0f, 1f), EaseOut);
_ScaleAnimation.InsertExpressionKeyFrame(0.8f, "Vector3(propSet.ScaleValue,propSet.ScaleValue,1f)", EaseOut);
_ScaleAnimation.InsertExpressionKeyFrame(1f, "Vector3(propSet.ScaleValue,propSet.ScaleValue,1f)", EaseOut);
_ScaleAnimation.SetReferenceParameter("propSet", PropSet);
_ScaleAnimation.Duration = TimeSpan.FromMilliseconds();
_ScaleAnimation.Target = "Scale";
}
return _ScaleAnimation;
}
} private static CompositionAnimationGroup RippleAnimationGroup
{
get
{
if (_RippleAnimationGroup == null)
{
_RippleAnimationGroup = compositor.CreateAnimationGroup();
_RippleAnimationGroup.Add(OpacityAnimation);
_RippleAnimationGroup.Add(ScaleAnimation);
}
return _RippleAnimationGroup;
}
} private static CompositionPropertySet PropSet
{
get
{
if (_PropSet == null)
{
_PropSet = compositor.CreatePropertySet();
PropSet.InsertScalar("ScaleValue", 2f);
}
return _PropSet;
}
} private static CompositionBrush Mask
{
get
{
if (_Mask == null)
{
var surface = LoadedImageSurface.StartLoadFromUri(new Uri("ms-appx:///MaterialLibs/Assets/RippleMask.png"), new Windows.Foundation.Size(100d, 100d));
_Mask = compositor.CreateSurfaceBrush(surface);
}
return _Mask;
}
}

最后在Mask读取的Uri的对应位置放上如下的图片文件:

完整代码已经开源在Github:https://github.com/cnbluefire/MaterialLibs

受个人技术所限,没有想到怎么做到圆角或者不规则图形,所以目前只支持直角矩形控件

UWP:使用Composition实现类似安卓的水波纹Ripple效果的更多相关文章

  1. html5 +css3 点击后水波纹扩散效果 兼容移动端

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. css 滚动视差 之 水波纹效果

    核心属性: background-attachment 这个属性就牛逼了, 它可以定义背景图片是相对视口固定, 还是随着视口滚动, 加上这个属性网页瞬间就从屌丝变成 高大上. 我们来看个例子: htm ...

  3. Android水波纹特效的简单实现

    我的开源页面指示器框架 MagicIndicator,各位一定不要错过哦. 水波纹特效,想必大家或多或少见过,在我的印象中,大致有如下几种: 支付宝 "咻咻咻" 式 流量球 &qu ...

  4. Android 水波纹点击效果(Ripple Effect)

    上周Android发布了Android M的Preview版本.但想必Android5.0很多炫酷效果,多数开发者还没有使用过,那更不要说广大用户了. 本文介绍的是Android5.0中其中一个炫酷的 ...

  5. CSS3 水波纹

    css3 动画设置水波纹,效果如下图: 源码: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  6. android: Android水波纹点击效果

    Android API 21及以上新增了ripple标签用来实现水波纹的效果.我们可以通过设置ripple背景来实现一些View点击效果. 该水波纹效果有两种:一种是有界的(点击后类似于一个矩形向四周 ...

  7. iOS 自定义任意形状加载进度条(水波纹进度条)

    1. 项目中要做类似下面的加载动画: 先给出安卓的实现方式 2.iOS的实现方式参考了下面两位的,感谢. 以任意底部图片为背景的加载动画 和 水波纹动画 最后附上自己的demo

  8. Android特效专辑(十)——点击水波纹效果实现,逻辑清晰实现简单

    Android特效专辑(十)--点击水波纹效果实现,逻辑清晰实现简单 这次做的东西呢,和上篇有点类似,就是用比较简单的逻辑思路去实现一些比较好玩的特效,最近也是比较忙,所以博客更新的速度还得看时间去推 ...

  9. WebGL——水波纹特效

    大家好,今天我ccentry要做一个水波纹特效,我们来看看水波纹特效的做法.首先我们来看一下水波纹特效的效果是怎么样的,请看下图. 我们要做的就是类似这种纹理特效,那么我们来看看是如何制作的吧.首先鲫 ...

随机推荐

  1. Android -传统蓝牙通信聊天

    概述 Android 传统蓝牙的使用,包括开关蓝牙.搜索设备.蓝牙连接.通信等. 详细 代码下载:http://www.demodashi.com/demo/10676.html 原文地址: Andr ...

  2. Maven中模块的聚合以及对jar包的继承

    我需要将之前的A B C三个模块聚合到一个一个工程中ABC项目中 pom.xml文件中应该这样配置 1.修改packaging里面的配置 <!-- 用于聚合这个项目的时候应该将packaging ...

  3. POJ - 3268 单源最短路

    题意:给定一些有向边,以及一个目的地,从某个点到达目的地,再从目的地回到那个点.共有n个点,问这n个点花费最大是多少? 思路:从目的地回去直接把目的地作为源点即可.那么从某个点到达目的地应该如何得到最 ...

  4. TCP/IP三次握手四次挥手

    本文通过图来梳理TCP-IP协议相关知识.TCP通信过程包括三个步骤:建立TCP连接通道,传输数据,断开TCP连接通道.如图所示,给出了TCP通信过程的示意图. TCP 三次握手四次挥手 主要包括三部 ...

  5. 重装Win10系统的非常简单的操作教程

    这是回到学校的第二天,准备搞一份实习证明,然而宿舍宽带停了,于是我来到了社团办公室,打开了其中一台电脑. 各位师弟师妹...你们也太厉害,把电脑折腾成这样...电脑装了各种各样的工具, Adobe P ...

  6. 用DirectDraw封装的位图动画类

    头文件 [cpp] view plaincopyprint? #pragma once #include <vector> using namespace std; #include &l ...

  7. C语言中的可变参数函数的浅析(以Arm 程序中的printf()函数实现为例) .

    我们在C语言编程中会遇到一些参数个数可变的函数,一般人对它的实现不理解.例如Printf(): Printf()函数是C语言中非常常用的一个典型的变参数函数,它 的原型为: int printf( c ...

  8. STM32——GPIO之从库函数到寄存器的前因后果

    例子为单片机的"Hello World"级的流水灯实验--虽然只有一个,其中并不是将完整的代码给出,只是给出关键部分来说明"如何调用ST公司的的库来完成对硬件的控制,以及 ...

  9. freemarker中的substring取子串

    freemarker中的substring取子串 1.substring取子串介绍 (1)表达式?substring(from,to) (2)当to为空时,默认的是字符串的长度 (3)from是第一个 ...

  10. hibernate学习(三) hibernate中的对象状态

    hibernate对象的状态分为三种:  游离状态,持久化状态,瞬时状态 下面一行代码区分: Configuration   cfg=new Configuration().configure(); ...