用C#写一个防安卓的属性动画
2021/12/7 20:19:09
本文主要是介绍用C#写一个防安卓的属性动画,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
用C#写一个防安卓的属性动画
- 定义独立动画接口
- 实现接口,创建值动画类
- 实现对象属性动画
- 应用
定义独立动画接口
public interface IAnimator { IAnimator Start(); IAnimator Stop(); IAnimator Reset(); IAnimator SetDuration(int times); IAnimator SetNext(IAnimator next); }
实现接口,创建值动画类
using Doraemon.Common.Utility; using System; using System.Collections.Generic; using System.Windows.Forms; namespace Doraemon.Common.Animation { public class ValueAnimator<T> : IAnimator { private T start = default(T); private T end = default(T); private int times = 1000; private T curValue; private long ticks = 0; private IAnimator next = null; private Timer timer; public T CurValue { get => curValue; set { if ((IComparable)curValue != (IComparable)value) { curValue = value; Update?.Invoke(this, curValue); } } } public ValueAnimator(T start, T end) { CreateBase(start, end); } private void CreateBase(T start, T end) { this.start = start; this.end = end; timer = new Timer(); timer.Enabled = false; timer.Interval = 10; timer.Tick += Timer_Tick; } public IAnimator Start() { timer.Start(); return this; } public IAnimator Stop() { timer.Stop(); return this; } public IAnimator Reset() { timer.Stop(); CurValue = default(T); return this; } public event Handler<ValueAnimator<T>, T> Update; public event Handler<ValueAnimator<T>, T> Done; private List<string> strs = new List<string>(); private void Timer_Tick(object sender, EventArgs e) { if (ticks == 0) ticks = SystemHelper.Ticks; double curRate = (SystemHelper.Ticks - ticks) / (times * 1000.0); double v1 = double.Parse(start.ToString()); double v2 = double.Parse(end.ToString()); bool reverse = (v2 - v1) < 0; double val = (v1 + (v2 - v1) * curRate); if ((!reverse && val >= v2) || (reverse && val <= v2)) { CurValue = end; Stop(); ticks = 0; Done?.Invoke(this, CurValue); if (next != null) next.Start(); } else CurValue = (T)Convert.ChangeType(val, typeof(T)); } public IAnimator SetNext(IAnimator ani) { next = ani; return this; } public IAnimator SetDuration(int times) { this.times = times; return this; } } }
实现对象属性动画
using System; using System.Reflection; namespace Doraemon.Common.Animation { public class ObjectAnimator : IAnimator { private object obj = null; private string propertyName = string.Empty; private IAnimator animator; private ObjectAnimator(object obj, string propertyName, int start, int end) { this.obj = obj; this.propertyName = propertyName; var ani = new ValueAnimator<int>(start, end); this.animator = ani; ani.Update += Ani_Update; } private ObjectAnimator(object obj, string propertyName, double start, double end) { this.obj = obj; this.propertyName = propertyName; var ani = new ValueAnimator<double>(start, end); this.animator = ani; ani.Update += Ani_Update1; } private void Ani_Update1(ValueAnimator<double> sender, double e) { SetValue<double>(e); } private void SetValue<T>(T e) { try { Type t = this.obj.GetType(); PropertyInfo pi = t.GetProperty(propertyName); pi.SetValue(this.obj, e); } catch { animator.Stop(); } } private void Ani_Update(ValueAnimator<int> sender, int e) { SetValue<int>(e); } public static ObjectAnimator OfInt(object obj, string propertyName, int start, int end) { return new ObjectAnimator(obj, propertyName, start, end); } public static ObjectAnimator OfDouble(object obj, string propertyName, double start, double end) { return new ObjectAnimator(obj, propertyName, start, end); } public IAnimator Start() { return animator.Start(); } public IAnimator Stop() { return animator.Stop(); } public IAnimator Reset() { return animator.Reset(); } public IAnimator SetDuration(int times) { return animator.SetDuration(times); } public IAnimator SetNext(IAnimator next) { return animator.SetNext(next); } } }
应用
在WinForm中调用
IAnimator ani1 = ObjectAnimator.OfDouble(this, "Opacity", 1, 0).SetDuration(200); IAnimator ani2 = ObjectAnimator.OfDouble(this, "Opacity", 0, 1).SetDuration(200); ani1.SetNext(ani2); ani2.SetNext(ani1); ani1.Start();
这篇关于用C#写一个防安卓的属性动画的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具
- 2024-01-24.NET集成IdGenerator生成分布式全局唯一ID
- 2024-01-23用CI/CD工具Vela部署Elasticsearch + C# 如何使用
- 2024-01-23.NET开源的简单、快速、强大的前后端分离后台权限管理系统