WPF中绘图

来源:互联网 发布:全自动猫厕所 知乎 编辑:程序博客网 时间:2024/06/05 19:20

WPF中绘图

XAML可以绘制矢量图

基本图形,派生自Shape类
Line—直线段,可以设置笔触(Stroke)
Rectangle—矩形,(Stroke,Fill)
Ellipse—椭圆,(Stroke,Fill)
Polygon—多边形,(Stroke,Fill)
Polyline—折线
Path—路径,各种线组成

直线

<Line X1="10" Y1="20" X2="260" Y2="20" Stroke="Red" StrokeThickness="10"/>


使用Path剪裁界面元素
UIElement的Clip属性
Clip属性的类型是Geometry 与 Path的Data属性一致。
如果窗体能被剪切,AllowsTransparency必须设置为True。WindowStyle属性必须为None。

UIElement 中有BitmapEffect(用CPU资源)和Effect(用GPU资源)两个属性为UI元素添加效果。

BitmapEffect:

BevelBitmapEffect—斜角效果
BitmapEffectGroup—复合效果
BlurBitmapEffect—模糊效果
DropShadowBitmapEffect—投影效果
EmbossBitmapEffect—浮雕效果
OuterGlowBitmapEffect—外发光效果
例:

<Button.BitmapEffect>    <DropShadowBitmapEffect Direction="-45" Opacity="0.75" ShadowDepth="7"/></Button.BitmapEffect>

Effect
UIElement 类有 Effect属性,它是Effect类型,Effect是抽象类
BlurEffect—模糊效果
DropShadowEffect—投影效果
ShaderEffect—着色器效果

官方滤镜包
wpffx.codeplex.com

变形
RenderTransform:呈现变形 UIElement
只是显示变形,其余的都不变
LayoutTransform:布局变形 FrameworkElement
显示和内容都变

这两个属性都是依赖属性类型时Transform
MatrixTransform 矩阵变形
RotateTransform 旋转变形
ScaleTransform坐标系变形
SkewTransform拉伸变形
TranslateTransform偏移变形
TransformGroup变形组

动画
例:
DoubleAnimationBase:
DoubleAnimation,
DoubleAnimationUsingKeyFrames,
DoubleAnimationUsingPath。

1.简单线性动画
变化时间 Duration
变化终点To
变化幅度By
变化起点From

例:

<Button.RenderTransform>    <Translate Transform x:Name="tt" X="0" Y="0"/></Button.RenderTransform>...CLick(... sender,... e){    DoubleAnimation daX = new DoubleAnimation();    DoubleAnimation daY = new DoubleAnimation();    Random r = new Random();    daX.From = 0D;    daY.From = 0D;    daX.To = r.NextDouble()*300;    daY.To = r.NextDouble()*300;    Duration duration = new Duration(TimeSpan.FromMilliseconds(300));    daX.Duration =  duration;    daX.Duration =  duration;this.tt.BeginAnimation(TranslateTransform.XProperty,daX);this.tt.BeginAnimation(TranslateTransform.XProperty,daX);}

注意:
daX.From = 0D每次都会从初始位置开始

2.高级动画属性
AccelerationRation 加速速率 0~1
DecelerationRation 减速速率 0~1
SpeedRatio 实际播放速度与正常速度之比
AutoReverse 反向播放
RepeatBehavior 重复次数
BeginTime 播放前等待
EasingFunction 缓冲式渐变
3.关键帧动画
DoubleAnimationUsingKeyFrames

<Button.RenderTransform>    <Translate Transform x:Name="tt" X="0" Y="0"/></Button.RenderTransform>...    DoubleAnimationUsingKeyFrames dakX= new DoubleAnimationUsingKeyFrames();    DoubleAnimationUsingKeyFrames dakY= new DoubleAnimationUsingKeyFrames();    Duration duration = new Duration(TimeSpan.FromMilliseconds(300));    daX.Duration =  duration;    daX.Duration =  duration;    ...    LinearDoubleKeyFrame x_lf_1 = new LinearDoubleKeyFrame();     LinearDoubleKeyFrame x_lf_2 = new LinearDoubleKeyFrame();     LinearDoubleKeyFrame x_lf_3 = new LinearDoubleKeyFrame();     x_lf_1 .KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(300));    x_lf_1 .Value = 200    x_lf_2 .KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(600));    x_lf_2 .Value = 0    x_lf_3 .KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(900));    x_lf_3 .Value = 200    dakX.Add(x_lf_1 );    dakX.Add(x_lf_2 );    dakX.Add(x_lf_3 );    ...

KeyTime.FromTimeSpan绝对时间
KeyTime.FromPercent相对时间

4.特殊的关键帧
DoubleAnimationUsingKeyFrames 的 KeyFrames属性是DoubleKeyFrameCollection,它可以接收DoubleKeyFrame元素。
DoubleKeyFrame:
LinearDoubleKeyFrame—线性变化关键帧
DiscreteDoubleKeyFrame—不连续变化关键帧
EasingDoubleKeyFrame—缓冲式变化关键帧
SplineDoubleKeyFrame—样条变化式关键帧,贝塞尔曲线
例:

DoubleAnimationUsingKeyFrames dakX= new DoubleAnimationUsingKeyFrames();Duration duration = new Duration(TimeSpan.FromMilliseconds(300));    daX.Duration =  duration;SplineDoubleKeyFrame skf = new SplineDoubleKeyFrame();kf.KeyTime = KeyTime.FromPercent(1);kf.Value = 400;KeySpline ks = new KeySpline();ks.ControlPoint1 = new Point(0,1);ks.ControlPoint2 = new Point(1,0);kf.KeySpline  = ks;dakX.KeyFrames.Add(kf);

5.路径动画
DoubleAnimationUsingPath

原创粉丝点击