作者: Blue_Pen
查看: 3130|回复: 5
打印 上一主题 下一主题

【窗体动画】BinGoo系列之《窗体动画-强调缩放特效》

[复制链接]
跳转到指定楼层
楼主
Blue_Pen 发表于 2019-7-18 09:33:32 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
查看: 3130|回复: 5
BinGoo系列之《窗体动画-强调缩放动画》

        对于大多数DSkin用户来说,可能很多人都只到DSkin其中的一个优势就是动画,可是具体怎么实现自己的动画特效还是一脸懵逼,这篇帖子主要是讲DSkin动画特效的封装,以及做好的强调缩放动画的代码。
        废话就这么多了,先看看效果:
        
        效果图里面像素问题是录制软件的问题,可以忽略。
        接下来讲讲具体实现方法(超级简单)
        首先是新建一个类,取个名字,例如:EmphasisOnZoomEffect,并且集成DSKin的IEffects接口,并补齐接口所需要实现的属性方法。找到
public Bitmap DoEffect(out Point offset),这个方法,在这里面就可以写你任意的动画效果了,其实主要想想窗体就是一张图片,然后对这个图片做动画处理。
        写好这个动画类以后,怎么使用?
        在窗体的构造函数中调用:Animation.Effect=new EmphasisOnZoomEffect(1.3);这里的1.3是我封装好的缩放属性系数
        具体实现的代码如下
        
[C#] 纯文本查看 复制代码
    [/align][align=left]    /// <summary>[/align]    /// 强调缩放
    /// </summary>
    public class EmphasisOnZoomEffect : IEffects
    {
        public EmphasisOnZoomEffect()
        {
        }
        public EmphasisOnZoomEffect(double zoonxishu)
        {
            ZoonXishu = zoonxishu;
        }
        public double ZoonXishu =1.2;

        public string Name
        {
            get { return "强调缩放特效"; }
        }

        int totalframes = 50;

        Bitmap bitmap = null;

        public bool CanDesc
        {
            get { return true; }
        }

        bool isAsc = true;

        public bool IsAsc
        {
            get { return isAsc; }
            set { isAsc = value; }
        }

        bool isFinal = true;

        public bool IsFinal
        {
            get { return isFinal; }
        }

        public void Reset()
        {
            zoon = 0;
            zoon2 = 0;
        }
        Bitmap original;

        public Bitmap Original
        {
            get { return original; }
            set { original = value; }
        }
        int zoon = 20;
        int zoon2 = 0;
        
        public Bitmap DoEffect(out Point offset)
        {
            offset = new Point();
            if (original != null)
            {
                if (bitmap == null || bitmap.Size != new Size((int)(original.Width * ZoonXishu), (int)(original.Height * ZoonXishu)))
                {
                    //缓存位图
                    bitmap = new Bitmap((int)(original.Width * ZoonXishu), (int)(original.Height * ZoonXishu));
                }
                offset=new Point(original.Width/2-bitmap.Width/2, original.Height / 2 - bitmap.Height / 2);
                float percent;
                if (isAsc)
                {
                    percent = (float)(1.0 * (zoon- zoon2) / (totalframes - 1));//动画百分比
                }
                else
                {
                    percent = 1.0f - (float)(1.0 * (zoon - zoon2) / (totalframes - 1));//动画百分比
                }
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.Clear(Color.Transparent);
                    Size size =new Size(original.Size.Width, original.Size.Height);
                    Size changedSize = new Size((int)(1.0 * size.Width * percent), (int)(1.0 * size.Height * percent));
                    RectangleF rf = new RectangleF((bitmap.Width - changedSize.Width) * 1.0f / 2,
                        (bitmap.Height - changedSize.Height) * 1.0f / 2, changedSize.Width, changedSize.Height);
                    g.DrawImage(original,rf);
                }

                if (isAsc)
                {
                    if (zoon < totalframes * ZoonXishu)
                    {
                        zoon += (int)((totalframes * ZoonXishu + 5 - zoon) / 5);
                        isFinal = false;
                    }
                    else
                    {
                        if (zoon2 < zoon - totalframes)
                        {
                            zoon2 += (zoon - totalframes + 10) / 10;
                            isFinal = false;
                        }
                        else
                        {
                            isFinal = true;
                        }
                    }
                }
                else
                {
                    if (zoon2 < totalframes * ZoonXishu - totalframes)
                    {
                        zoon2 += (int)(totalframes * ZoonXishu - totalframes + 13) / 13;
                        isFinal = false;
                    }
                    else
                    {
                        if (zoon < (totalframes + zoon2))
                        {
                            zoon += (totalframes + zoon2 + 10) /10;
                            isFinal = false;
                        }
                        else
                        {
                            isFinal = true;
                        }
                    }
                }


            }


            return bitmap;
        }
        /// <summary>
        /// 获取一个带有透明度的ImageAttributes
        /// </summary>
        /// <param name="opcity"></param>
        /// <returns></returns>
        public ImageAttributes GetAlphaImgAttr(int opcity)
        {
            if (opcity < 0 || opcity > 100)
            {
                throw new ArgumentOutOfRangeException("opcity 值为 0~100");
            }
            //颜色矩阵
            float[][] matrixItems =
            {
                new float[]{1,0,0,0,0},
                new float[]{0,1,0,0,0},
                new float[]{0,0,1,0,0},
                new float[]{0,0,0,(float)opcity / 100,0},
                new float[]{0,0,0,0,1}
            };
            ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
            ImageAttributes imageAtt = new ImageAttributes();
            imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            return imageAtt;
        }
        public void Dispose()
        {
            if (bitmap != null)
            {
                bitmap.Dispose();
                bitmap = null;
            }
            GC.SuppressFinalize(this);
        }
    }
        下载源码请狂点这里:

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播
回复 论坛版权

使用道具 举报

沙发
时间 发表于 2019-7-18 09:40:14 | 只看该作者
大B终于想起来他的论坛账号密码了
板凳
mqzzzz 发表于 2019-7-19 09:05:37 | 只看该作者
Dispose怎么调用呢
地板
wtf3505 发表于 2019-7-28 18:45:43 | 只看该作者
不错哦。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
回复

使用道具 举报

5#
86208012@qq.com 发表于 2019-8-16 15:40:39 | 只看该作者
没有注释我不大看的懂,哎,太菜
6#
myconfig519 发表于 2019-11-7 14:20:44 | 只看该作者
给用户了一个不同的体验。很不错的想法。谢谢
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-4-19 21:45, Processed in 0.672567 second(s), 31 queries , Gzip On.

快速回复 返回顶部 返回列表