我正在嘗試做一個矩陣動畫,我在這裏同時縮放和轉置畫布。我發現的唯一方法是使用MatrixTransform和MatrixAnimationUsingKeyFrames。由於似乎沒有對內置矩陣進行插值(僅用於路徑/旋轉),似乎唯一的選擇是嘗試自行構建插值和DiscreteMatrixKeyFrame。使用MatrixTransform進行平滑動畫?
我做了這個基本實現,但它不完全平滑,我不知道這是最好的方式,以及如何處理framerate等任何人都有改進的建議?下面的代碼:
MatrixAnimationUsingKeyFrames anim = new MatrixAnimationUsingKeyFrames();
int duration = 1;
anim.KeyFrames = Interpolate(new Point(0, 0), centerPoint, 1, factor,100,duration);
this.matrixTransform.BeginAnimation(MatrixTransform.MatrixProperty, anim,HandoffBehavior.Compose);
public MatrixKeyFrameCollection Interpolate(Point startPoint, Point endPoint, double startScale, double endScale, double framerate,double duration)
{
MatrixKeyFrameCollection keyframes = new MatrixKeyFrameCollection();
double steps = duration * framerate;
double milliSeconds = 1000/framerate;
double timeCounter = 0;
double diffX = Math.Abs(startPoint.X- endPoint.X);
double xStep = diffX/steps;
double diffY = Math.Abs(startPoint.Y - endPoint.Y);
double yStep = diffY/steps;
double diffScale= Math.Abs(startScale- endScale);
double scaleStep = diffScale/steps;
if (endPoint.Y < startPoint.Y)
{
yStep = -yStep;
}
if (endPoint.X < startPoint.X)
{
xStep = -xStep;
}
if (endScale < startScale)
{
scaleStep = -scaleStep;
}
Point currentPoint = new Point();
double currentScale = startScale;
for (int i = 0; i < steps; i++)
{
keyframes.Add(new DiscreteMatrixKeyFrame(new Matrix(currentScale, 0, 0, currentScale, currentPoint.X, currentPoint.Y), KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(timeCounter))));
currentPoint.X += xStep;
currentPoint.Y += yStep;
currentScale += scaleStep;
timeCounter += milliSeconds;
}
keyframes.Add(new DiscreteMatrixKeyFrame(new Matrix(endScale, 0, 0, endScale, endPoint.X, endPoint.Y), KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))));
return keyframes;
}
做得很好!我爲這段代碼添加了緩動。在下面尋找我的位。 – bor 2013-04-05 06:55:52
會這樣可以給你打十幾次... – Jeff 2014-03-19 00:09:00