public void CreateVisuals(Compositor compositor) { layerVisual = compositor.CreateLayerVisual(); itemContainerVisual = compositor.CreateContainerVisual(); layerVisual.Children.InsertAtTop(itemContainerVisual); for (int i = 0; i < items.Count; ++i) { var item = items[i]; var itemVisual = item.CreateVisual(compositor); itemVisual.Size = new Vector2(300, 200); itemVisual.Offset = new Vector3((i % 2 - 1) * itemVisual.Size.X, (i / 2 - 1) * itemVisual.Size.Y, 0); itemContainerVisual.Children.InsertAtTop(itemVisual); } }
// j新建视觉层画阴影 private void InitComposition() { Compositor compositor = ElementCompositionPreview.GetElementVisual(PicGrid).Compositor; //Create LayerVisual LayerVisual layerVisual = compositor.CreateLayerVisual(); layerVisual.Size = new Vector2(900, 900); //Create SpriteVisuals to use as LayerVisual child SpriteVisual sv1 = compositor.CreateSpriteVisual(); sv1.Brush = compositor.CreateColorBrush(Windows.UI.Colors.Blue); sv1.Size = new Vector2(300, 300); sv1.Offset = new Vector3(200, 200, 0); SpriteVisual sv2 = compositor.CreateSpriteVisual(); sv2.Brush = compositor.CreateColorBrush(Colors.Red); sv2.Size = new Vector2(300, 300); sv2.Offset = new Vector3(400, 400, 0); //Add children to the LayerVisual layerVisual.Children.InsertAtTop(sv1); layerVisual.Children.InsertAtTop(sv2); //Create DropShadow DropShadow shadow = compositor.CreateDropShadow(); shadow.Color = Colors.DarkSlateGray; shadow.Offset = new Vector3(40, 40, 0); shadow.BlurRadius = 9; shadow.SourcePolicy = CompositionDropShadowSourcePolicy.InheritFromVisualContent; //Associate Shadow with LayerVisual layerVisual.Shadow = shadow; ElementCompositionPreview.SetElementChildVisual(PicGrid, layerVisual); }
/// <summary> /// Initialize all Composition related stuff here (Compositor, Animations etc) /// </summary> private void InitializeComposition() { var rootVisual = ElementCompositionPreview.GetElementVisual(this); // Compositor _compositor = rootVisual.Compositor; // Composition Generator _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); // Final Value Expressions var vector3Expr = _compositor.CreateFinalValueExpression <Vector3>(); var scalarExpr = _compositor.CreateFinalValueExpression <float>(); // Opacity Animation var opacityAnimation = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(DefaultOpacityAnimationDuration) .ForTarget(() => rootVisual.Opacity); opacityAnimation.InsertExpressionKeyFrame(1f, scalarExpr); // Scale Animation var scaleAnimation = _compositor.CreateKeyFrameAnimation <Vector3>() .HavingDuration(DefaultScaleAnimationDuration) .ForTarget(() => rootVisual.Scale); scaleAnimation.InsertExpressionKeyFrame(1f, vector3Expr); // ImplicitAnimation _implicitAnimationCollection = _compositor.CreateImplicitAnimationCollection(); _implicitAnimationCollection["Opacity"] = opacityAnimation.Animation; _implicitAnimationCollection["Scale"] = scaleAnimation.Animation; // Expand Animations _expandLeftInset = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(InsetAnimationDuration) .DelayBy(InsetAnimationDelayDuration); _expandLeftInset.InsertKeyFrame(1f, 0); _expandRightInset = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(InsetAnimationDuration) .DelayBy(InsetAnimationDelayDuration); _expandInsetClip = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(InsetClipAnimationDuration); _expandInsetClip.InsertKeyFrame(1f, 0); // Collapse Animations _collapseLeftInset = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(InsetAnimationDuration); _collapseRightInset = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(InsetAnimationDuration); _collapseInsetClip = _compositor.CreateKeyFrameAnimation <float>() .HavingDuration(InsetClipAnimationDuration); // Root Container _rootContainer = _compositor.CreateContainerVisual(); // Background Layer _bgLayer = _compositor.CreateLayerVisual(); _bgLayer.Size = _rootContainer.Size; _bgLayer.CenterPoint = new Vector3(_bgLayer.Size * 0.5f, 0); // Top Layer _topLayer = _compositor.CreateLayerVisual(); _topLayer.Size = _rootContainer.Size; _rootContainer.Children.InsertAtBottom(_bgLayer); _rootContainer.Children.InsertAtTop(_topLayer); // Add the rootContainer to the visual tree ElementCompositionPreview.SetElementChildVisual(this, _rootContainer); }
/// <summary> /// Initialize all Composition related stuff here (Compositor, Animations etc) /// </summary> private void InitializeComposition() { var rootVisual = ElementCompositionPreview.GetElementVisual(this); // Compositor _compositor = rootVisual.Compositor; // Composition Generator _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); // Final Value Expressions var vector3Expr = _compositor.CreateFinalValueExpression<Vector3>(); var scalarExpr = _compositor.CreateFinalValueExpression<float>(); // Opacity Animation var opacityAnimation = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(DefaultOpacityAnimationDuration) .ForTarget(() => rootVisual.Opacity); opacityAnimation.InsertExpressionKeyFrame(1f, scalarExpr); // Scale Animation var scaleAnimation = _compositor.CreateKeyFrameAnimation<Vector3>() .HavingDuration(DefaultScaleAnimationDuration) .ForTarget(() => rootVisual.Scale); scaleAnimation.InsertExpressionKeyFrame(1f, vector3Expr); // ImplicitAnimation _implicitAnimationCollection = _compositor.CreateImplicitAnimationCollection(); _implicitAnimationCollection["Opacity"] = opacityAnimation.Animation; _implicitAnimationCollection["Scale"] = scaleAnimation.Animation; // Expand Animations _expandLeftInset = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(InsetAnimationDuration) .DelayBy(InsetAnimationDelayDuration); _expandLeftInset.InsertKeyFrame(1f, 0); _expandRightInset = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(InsetAnimationDuration) .DelayBy(InsetAnimationDelayDuration); _expandInsetClip = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(InsetClipAnimationDuration); _expandInsetClip.InsertKeyFrame(1f, 0); // Collapse Animations _collapseLeftInset = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(InsetAnimationDuration); _collapseRightInset = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(InsetAnimationDuration); _collapseInsetClip = _compositor.CreateKeyFrameAnimation<float>() .HavingDuration(InsetClipAnimationDuration); // Root Container _rootContainer = _compositor.CreateContainerVisual(); // Background Layer _bgLayer = _compositor.CreateLayerVisual(); _bgLayer.Size = _rootContainer.Size; _bgLayer.CenterPoint = new Vector3(_bgLayer.Size * 0.5f, 0); // Top Layer _topLayer = _compositor.CreateLayerVisual(); _topLayer.Size = _rootContainer.Size; _rootContainer.Children.InsertAtBottom(_bgLayer); _rootContainer.Children.InsertAtTop(_topLayer); // Add the rootContainer to the visual tree ElementCompositionPreview.SetElementChildVisual(this, _rootContainer); }
public void CreateVisuals(Compositor compositor) { layerVisual = compositor.CreateLayerVisual(); itemContainerVisual = compositor.CreateContainerVisual(); layerVisual.Children.InsertAtTop(itemContainerVisual); for (int i = 0; i < items.Count; ++i) { var item = items[i]; var itemVisual = item.CreateVisual(compositor); itemVisual.Size = new Vector2(300, 200); itemVisual.Offset = new Vector3((i % 2 - 1) * itemVisual.Size.X, (i / 2 - 1) * itemVisual.Size.Y, 0); itemContainerVisual.Children.InsertAtTop(itemVisual); } }
/// <summary> /// Handles the Arrange pass during Layout /// </summary> /// <param name="finalSize">Final Size of the control</param> /// <returns>Total size occupied by all the Children</returns> protected override Size ArrangeOverride(Size finalSize) { if ((ItemsSource == null) || (!ItemsSource.Any())) { return(finalSize); } var padding = Padding; var paddingSize = padding.CollapseThickness(); var width = finalSize.Width; var height = finalSize.Height; var itemCount = ItemsSource.Count(); _availableWidth = (width - paddingSize.Width).Single(); _availableHeight = (height - paddingSize.Height).Single(); var gapCount = itemCount - 1; var totalGap = (gapCount * ItemGap).Single(); var availableNonGapWidth = _availableWidth - totalGap; var availableNonGapHeight = _availableHeight; _itemWidth = availableNonGapWidth / itemCount; _itemHeight = availableNonGapHeight; _bannerBounds = new Rect(padding.Left, padding.Top, _availableWidth, _availableHeight); // Check if any visual is currently selected. If yes then obtain its index. // The new visual at that index should be selected and added to the toplayer var selectedVisualIndex = -1; if (_selectedVisual != null) { foreach (var item in _fluidItems) { selectedVisualIndex++; if (ReferenceEquals(item.Key, _selectedVisual)) { break; } } } // Clear previous visuals _fluidItems.Clear(); _rootContainer.Children.RemoveAll(); _rootContainer.Size = finalSize.ToVector2(); // Background Layer _bgLayer = _compositor.CreateLayerVisual(); _bgLayer.Size = _rootContainer.Size; _rootContainer.Children.InsertAtBottom(_bgLayer); _bgLayer.CenterPoint = new Vector3(_bgLayer.Size * 0.5f, 0); // Top Layer _topLayer = _compositor.CreateLayerVisual(); _topLayer.Size = _rootContainer.Size; _rootContainer.Children.InsertAtTop(_topLayer); // Create Visuals for (var i = 0; i < itemCount; i++) { // Add a visual for the background var bgVisual = _compositor.CreateSpriteVisual(); bgVisual.Size = new Vector2(_availableWidth, _availableHeight); bgVisual.Brush = _compositor.CreateColorBrush(ItemBackground); // Create the visual for the content var contentVisual = _compositor.CreateSpriteVisual(); contentVisual.Offset = Vector3.Zero; contentVisual.Size = new Vector2(_availableWidth, _availableHeight); // Load image from the Uri contentVisual.Brush = _compositor.CreateSurfaceBrush(_images.ElementAt(i).Surface); // Create the container for the content var container = _compositor.CreateContainerVisual(); container.Offset = new Vector3((float)padding.Left, (float)padding.Top, 0); container.Size = new Vector2(_availableWidth, _availableHeight); // Calculate the Inset Clip var left = i * (_itemWidth + ItemGap.Single()); var right = _availableWidth - (left + _itemWidth); container.Properties.InsertScalar("LeftInset", left); container.Properties.InsertScalar("RightInset", right); // If the current value of 'i' same as the index of the previously selected visual // then the InsetClip value should be appropriately set container.Clip = (i == selectedVisualIndex) ? _compositor.CreateInsetClip(0, 0, 0, 0) : _compositor.CreateInsetClip(left, 0, right, 0); // Center Point container.CenterPoint = new Vector3(left + (_itemWidth / 2f), (float)padding.Top + (_itemHeight / 2f), 0); contentVisual.CenterPoint = container.CenterPoint; container.ImplicitAnimations = _implicitAnimationCollection; contentVisual.ImplicitAnimations = _implicitAnimationCollection; // Set the container content container.Children.InsertAtTop(contentVisual); container.Children.InsertAtBottom(bgVisual); // Is the current value of 'i' same as the index of the previously selected visual if (i == selectedVisualIndex) { // Add to the Top Layer _topLayer.Children.InsertAtTop(container); _selectedVisual = container; } else { // Add to Background Layer _bgLayer.Children.InsertAtTop(container); } // Add Visual to the fluid items _fluidItems[container] = new Rect(padding.Left + left, padding.Top, _itemWidth, _itemHeight); } // If there was a previously selected visual then Scale back // and fade the other visuals in the banner if (selectedVisualIndex > -1) { // foreach (var child in _bgLayer.Children) { child.Scale = new Vector3(ScaleDownFactor, ScaleDownFactor, 1); child.Opacity = TargetOpacity; } } // Update Animations' keyframes _expandRightInset.InsertKeyFrame(1f, _availableWidth - _itemWidth); _collapseInsetClip.InsertKeyFrame(1f, _availableWidth - _itemWidth); // Add the rootContainer to the visual tree ElementCompositionPreview.SetElementChildVisual(this, _rootContainer); return(base.ArrangeOverride(finalSize)); }
/// <summary> /// Initializes the Composition elements /// </summary> private void InitComposition() { // Compositor _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; // CompositionGenerator _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); // Fade Out Animation _fadeOutAnimation = _compositor.CreateScalarKeyFrameAnimation(); _fadeOutAnimation.InsertKeyFrame(1f, 0); _fadeOutAnimation.Duration = TransitionDuration; // Fade In Animation _fadeInAnimation = _compositor.CreateScalarKeyFrameAnimation(); _fadeInAnimation.InsertKeyFrame(1f, 1); _fadeInAnimation.Duration = TransitionDuration; // Color Animation _colorAnimation = _compositor.CreateColorKeyFrameAnimation(); _colorAnimation.Duration = TransitionDuration; // Offset Animation _offsetAnimation = _compositor.CreateVector3KeyFrameAnimation(); _offsetAnimation.Target = "Offset"; _offsetAnimation.Duration = TransitionDuration; _offsetAnimation.InsertKeyFrame(1f, Vector3.Zero); // Alignment animations _alignXAnimation = _compositor.CreateScalarKeyFrameAnimation(); _alignXAnimation.Duration = AlignmentTransitionDuration; _alignYAnimation = _compositor.CreateScalarKeyFrameAnimation(); _alignYAnimation.Duration = AlignmentTransitionDuration; // ZoomIn Animation Group _scaleAnimation = _compositor.CreateVector3KeyFrameAnimation(); _scaleAnimation.Target = "Scale"; _scaleAnimation.InsertKeyFrame(1f, Vector3.One); _scaleAnimation.Duration = TransitionDuration; _zoomInAnimationGroup = _compositor.CreateAnimationGroup(); _zoomInAnimationGroup.Add(_scaleAnimation); _zoomInAnimationGroup.Add(_offsetAnimation); // Visuals _rootContainer = _compositor.CreateContainerVisual(); _frameLayer = _compositor.CreateLayerVisual(); _frameBackgroundVisual = _compositor.CreateSpriteVisual(); _frameContentVisual = _compositor.CreateSpriteVisual(); _placeholderContentVisual = _compositor.CreateSpriteVisual(); _placeholderBackgroundVisual = _compositor.CreateSpriteVisual(); _nextVisualContent = _compositor.CreateSpriteVisual(); _frameLayer.Children.InsertAtTop(_frameBackgroundVisual); _frameLayer.Children.InsertAtTop(_frameContentVisual); _frameLayer.Children.InsertAtTop(_placeholderBackgroundVisual); _frameLayer.Children.InsertAtTop(_placeholderContentVisual); _frameLayer.Children.InsertAtTop(_nextVisualContent); // Placeholder content _placeholderContentMask = _generator.CreateGeometrySurface(PlaceholderSize, GetPlaceHolderGeometry(), PlaceholderColor, PlaceholderBackground); _placeholderContentBrush = _compositor.CreateSurfaceBrush(_placeholderContentMask.Surface); _placeholderContentVisual.Brush = _placeholderContentBrush; // Placeholder background _placeholderBackgroundVisual.Brush = _compositor.CreateColorBrush(PlaceholderBackground); // By default placeholder visual will not be visible HidePlaceholder(); // Shadow visual _shadowVisual = _compositor.CreateSpriteVisual(); _rootContainer.Children.InsertAtBottom(_shadowVisual); _rootContainer.Children.InsertAtTop(_frameLayer); _frameBackgroundVisual.Brush = _compositor.CreateColorBrush(FrameBackground); // Create the effect to create the opacity mask var layerEffect = new CompositeEffect { // CanvasComposite.DestinationIn - Intersection of source and mask. // Equation: O = MA * S // where O - Output pixel, MA - Mask Alpha, S - Source pixel. Mode = CanvasComposite.DestinationIn, Sources = { new CompositionEffectSourceParameter("source"), new CompositionEffectSourceParameter("mask") } }; var layerEffectFactory = _compositor.CreateEffectFactory(layerEffect); _layerEffectBrush = layerEffectFactory.CreateBrush(); // The mask for the imageFrame _frameLayerMask = _generator.CreateMaskSurface(new Size(0, 0), null); _layerEffectBrush.SetSourceParameter("mask", _compositor.CreateSurfaceBrush(_frameLayerMask.Surface)); // Apply the mask effect to the frameLayer _frameLayer.Effect = _layerEffectBrush; ElementCompositionPreview.SetElementChildVisual(this, _rootContainer); }
/// <summary> /// Disposes the resources /// </summary> public void Dispose() { // Clean up resources _compositor = null; Source = null; DataContext = null; Foreground = null; Background = null; _scheduledObject = null; _currentObject = null; // Clean up Composition Objects _imageSurface?.Dispose(); _imageSurface = null; _nextImageSurface?.Dispose(); _nextImageSurface = null; _frameLayerMask?.Dispose(); _frameLayerMask = null; _placeholderContentMask?.Dispose(); _placeholderContentMask = null; _placeholderContentBrush?.Dispose(); _placeholderContentBrush = null; _nextSurfaceBrush?.Dispose(); _nextSurfaceBrush = null; _rootContainer?.Dispose(); _rootContainer = null; _shadowVisual?.Dispose(); _shadowVisual = null; _frameLayer?.Dispose(); _frameLayer = null; _frameBackgroundVisual?.Dispose(); _frameBackgroundVisual = null; _placeholderBackgroundVisual?.Dispose(); _placeholderBackgroundVisual = null; _placeholderContentVisual?.Dispose(); _placeholderContentVisual = null; _frameContentVisual?.Dispose(); _frameContentVisual = null; _nextVisualContent?.Dispose(); _nextVisualContent = null; _shadow?.Dispose(); _shadow = null; _layerEffectBrush?.Dispose(); _layerEffectBrush = null; _imageOptions = null; _zoomInAnimationGroup?.Dispose(); _zoomInAnimationGroup = null; _fadeOutAnimation?.Dispose(); _fadeOutAnimation = null; _fadeInAnimation?.Dispose(); _fadeInAnimation = null; _colorAnimation?.Dispose(); _colorAnimation = null; _alignXAnimation?.Dispose(); _alignXAnimation = null; _alignYAnimation?.Dispose(); _alignYAnimation = null; _offsetAnimation?.Dispose(); _offsetAnimation = null; _scaleAnimation?.Dispose(); _scaleAnimation = null; // Dispose the generator at the end to allow the // dependant composition objects to unsubscribe from // generator events _generator?.Dispose(); _generator = null; }