1
我嘗試在一個Grid控件中動畫多個(不同大小)的圖像,但只有最大的一個是動畫。目標屬性是「Offset.x」。UWP wincomposition:動畫多個圖像的偏移
var container = new Grid();
var compositor = ElementCompositionPreview.GetElementVisual(container).Compositor;
var containerVisual = compositor.CreateContainerVisual();
// ------
// CLOUDS
// ------
var cloudImage1 = CreateCloudImage(60);
var cloudVisual1 = ElementCompositionPreview.GetElementVisual(cloudImage1);
var cloudImage2 = CreateCloudImage(70);
var cloudVisual2 = ElementCompositionPreview.GetElementVisual(cloudImage2);
var cloudImage3 = CreateCloudImage(50);
var cloudVisual3 = ElementCompositionPreview.GetElementVisual(cloudImage3);
container.Children.Add(cloudImage1);
container.Children.Add(cloudImage2);
container.Children.Add(cloudImage3);
containerVisual.Children.InsertAtTop(cloudVisual1);
containerVisual.Children.InsertAtTop(cloudVisual2);
containerVisual.Children.InsertAtTop(cloudVisual3);
// ----------
// ANIMATIONS
// ----------
var offsetAnimation = CreateOffsetAnimation(compositor, 50, 6);
cloudVisual1.StartAnimation("Offset.x", offsetAnimation);
offsetAnimation.InsertKeyFrame(1f, -60);
cloudVisual2.StartAnimation("Offset.x", offsetAnimation); // doesn't work
ElementCompositionPreview.SetElementChildVisual(container, containerVisual);
return container;
助手方法:
static ScalarKeyFrameAnimation CreateOffsetAnimation(Compositor compositor, float endKeyFrame, double duration) {
var animation = compositor.CreateScalarKeyFrameAnimation();
animation.InsertKeyFrame(0f, 0);
animation.InsertKeyFrame(1f, endKeyFrame);
animation.IterationBehavior = AnimationIterationBehavior.Forever;
animation.Direction = AnimationDirection.Alternate;
animation.Duration = TimeSpan.FromSeconds(duration);
return animation;
}
static Image CreateCloudImage(double size) {
var cloudBitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/Icons/cloudy.png"));
var cloudImageControl = new Image() {
Source = cloudBitmapImage,
Height = size,
Width = size,
};
return cloudImageControl;
}
需要注意的是,如果我做的第二圖象最大的,它成爲該動畫的一個。
關於如何解決這個問題的任何想法?
在此先感謝。
這確實是由網格造成的。原因解釋[這裏](https://github.com/Microsoft/WindowsUIDevLabs/wiki/XAML-Composition-Interop-Behavior-Changes)。您可以使用邊框來包裝每個圖像而不使用畫布。在創作者更新中,您可以爲翻譯屬性設置動畫而不是偏移。從[這裏](https://github.com/JustinXinLiu/Continuity/blob/master/Continuity/Extensions/CompositionExtensions.Translation.cs)查看'StartTranslationAnimation'方法。 –
謝謝!我要去看看這:) –