题
我目前正在实施一个在WPF称为SelectionBorder类。它是从形状类派生的。
它基本上看起来像这样:
public class SelectionBorder : Shape
{
public Point StartPoint {get; set;}
public PointCollection Points {get; set;}
public double StrokeLength {get; set;}
protected override Geometry DefiningGeometry{
get{
//Magic!
}
}
}
在StartPoint和积分特性确定的边界的角部。边界是一个典型的描边线边框(一个黑行程,一个看不见的行程这样的: - - - - )
这是我现在的问题是,由于角点是自由选择的这是很常见的中风(意为黑色和无形的笔触)的数量甚至没有(其实甚至不是一个整数),因此第一笔划的外观比别人(图中可见)长。这也许似乎没有什么大问题,但后来我想动画边框,使得招一圈又一圈的内容。当这样做动画的静态视图微小的瑕疵变得清晰可见,在我看来是非常令人不安的。
替代文字http://img14.imageshack.us/img14/2874/selectionborder .PNG
的问题是,我试图确定该StrokeLength得到尽可能接近原始StrokeLength尽可能并创建一个偶数数量的冲程。但是我碰到的问题是,WPF(显然)无法显示一个双小数StrokeLength的整个精度,并且因此所得到的笔划数是不均匀的再次
是否有此问题的任何解决方法吗?你可能有另一种解决我的问题?
提前感谢!
编辑:我重新和健身今天稍微休息一下后,审查代码,毕竟这只是在非常大的StrokeLengths发生。我打算使用的2 StrokeLengths其中的小动画跳做事情远远低于我本来以为。
解决方案 2
我只发现了一种方法,使得它比较容易的方式来创建这样一个动画SelectionBorder。
代替通过通过动画我刚动画由Shape类本身提供的StrokeDashOffset属性移动的自取AnimationPoint和设置StrokeDashArray定义StrokeLength创建动画的。
它看起来像这样在XAML:
<namespace:SelectionBorder StrokeDashArray="2" AnimationDuration="0:0:1" Stroke="Black" />
在类看起来像这样:
public class SelectionBorder : Shape
{
private DoubleAnimation m_Animation;
private bool m_AnimationStarted;
public SelectionBorder()
{
IsVisibleChanged += OnIsVisibleChanged;
}
protected void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (Visibility == Visibility.Visible)
{
StartAnimation();
}
else
{
StopAnimation();
}
}
public void StartAnimation()
{
if (m_AnimationStarted)
return;
if (m_Animation == null)
{
m_Animation = CreateAnimation();
}
BeginAnimation(StrokeDashOffsetProperty, m_Animation);
m_AnimationStarted = true;
}
protected virtual DoubleAnimation CreateAnimation()
{
DoubleAnimation animation = new DoubleAnimation();
animation.From = 0;
if (StrokeDashArray.Count == 0)
animation.To = 4;
else
animation.To = StrokeDashArray.First() * 2;
animation.Duration = AnimationDuration;
animation.RepeatBehavior = RepeatBehavior.Forever;
return animation;
}
public void StopAnimation()
{
if (m_AnimationStarted)
{
BeginAnimation(StrokeDashOffsetProperty, null);
m_AnimationStarted = false;
}
}
#region Dependency Properties
public Duration AnimationDuration
{
get { return (Duration)GetValue(AnimationDurationProperty); }
set { SetValue(AnimationDurationProperty, value); }
}
public static readonly DependencyProperty AnimationDurationProperty =
DependencyProperty.Register("AnimationDuration", typeof(Duration), typeof(SelectionBorder), new UIPropertyMetadata(new Duration(TimeSpan.FromSeconds(0.5))));
#endregion Dependency Properties
protected override Geometry DefiningGeometry
{
get
{
double width = (double.IsNaN(Width)) ? ((Panel)Parent).ActualWidth : Width;
double height = (double.IsNaN(Height)) ? ((Panel)Parent).ActualHeight : Height;
RectangleGeometry geometry = new RectangleGeometry(new Rect(0, 0, width, height));
return geometry;
}
}
}
其他提示
您可以在这方面作出多个角落“非匹配”。例如,而不是一个点是动画破折号的“源”和“目的地”,你可以挑选2分。之一将是“源”,破折号出现行军远离它在2个方向,而另一点是“目的地”,其中短划线会聚并消失。
GIMP,例如,动画选择虚线以这种方式系和似乎挑最接近一个点到左下为“源”和“目的地”最接近于右上的点。
您可以想出一些其他的方案,也是如此。
请记住,虽然它可能看起来令人不安的你,大多数用户不会在意。