首先是新建一个类,取个名字,例如:EmphasisOnZoomEffect,并且集成DSKin的IEffects接口,并补齐接口所需要实现的属性方法。找到
public Bitmap DoEffect(out Point offset),这个方法,在这里面就可以写你任意的动画效果了,其实主要想想窗体就是一张图片,然后对这个图片做动画处理。
在窗体的构造函数中调用:Animation.Effect=new EmphasisOnZoomEffect(1.3);这里的1.3是我封装好的缩放属性系数
/// <summary>
/// 强调缩放
/// </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);
}
}