LinearGradientBrush spin - visual-studio

I have a LinearGradientBrush and am trying to apply the RotateTransform offset to it. With this code, it rotates and keeps still at the angle I give it:
<LinearGradientBrush.Transform>
<RotateTransform CenterX="60" CenterY="30" Angle="45" />
</LinearGradientBrush.Transform>
Is there a way to make the "Angle" dynamic so the brush will spin at 180 degrees?

You can run a DoubleAnimation on the Angle property of the RotateTransform, like in the example below, where the LinearGradientBrush fills a Rectangle, and the the animation is started when the Rectangle is loaded, and then runs forever:
<Rectangle Width="300" Height="300">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0,1">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="Black" Offset="1"/>
<LinearGradientBrush.Transform>
<RotateTransform CenterX="150" CenterY="150"/>
</LinearGradientBrush.Transform>
</LinearGradientBrush>
</Rectangle.Fill>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Fill.Transform.Angle"
To="360" Duration="0:0:5" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>

Related

UWP - LinearGradientBrush turns transparents UIelements on foreground

I have following XAML:
<StackPanel Orientation="Vertical">
<Canvas Width="100" Height="100">
<Border Width="100" Height="100">
<Border.Background>
<LinearGradientBrush MappingMode="Absolute" StartPoint="-10,10" EndPoint="10,0" SpreadMethod="Repeat">
<GradientStop Color="Blue" Offset="4"/>
<GradientStop Color="Red"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Polyline Points="0,0 10,10 50,10 90,60" Stroke="Black" StrokeThickness="4"/>
</Canvas>
<Canvas Width="100" Height="100">
<Border Width="100" Height="100">
<Border.Background>
<LinearGradientBrush MappingMode="Absolute" StartPoint="-10,10" EndPoint="10,0"
SpreadMethod="Pad">
<GradientStop Color="Blue" Offset="4"/>
<GradientStop Color="Red"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Polyline Points="0,0 10,10 50,10 90,60" Stroke="Black" StrokeThickness="4"/>
</Canvas>
</StackPanel>
and it looks like:
When I put SpreadMethod="Repeat" on a LinearGradientBrush, it makes all UIelements over the brush, half-transparent. Is this a bug?, how can I fix this?
(It is more weird, if I give the polyline a fill color, it's stroke gets hard black)
Both target and min versions of the project are Windows 10 Fall Creators Update (10.0; build 16299). IDE is VS2017.

How to make Ellipse animation (Windows Phone 8 - c#)

I have an ellipse, and I want to make an animation to show this ellipse.
The animation should do something like this gif:
http://media.giphy.com/media/qiutE2wCo1YXe/giphy.gif
But instead of the ellipse disappear, it starts from zero and ends full.
Does anybody can help me with this?
You can make it by splitting the ellipse in half and animate two pieces of it separately. Here's my solution that implements the idea.
First of all, you have to define shapes to animate. I use Canvas as a container.
<Canvas Width="100"
Height="100">
<Canvas.Clip>
<EllipseGeometry RadiusX="50"
RadiusY="50"
Center="50, 50" />
</Canvas.Clip>
<Canvas>
<Canvas.Clip>
<RectangleGeometry Rect="0, 0, 50, 100" />
</Canvas.Clip>
<Rectangle x:Name="HalfLeft"
Width="50"
Height="100"
Fill="Red"
Canvas.Left="0"
Canvas.Top="0">
<Rectangle.RenderTransform>
<RotateTransform Angle="-180"
CenterX="50"
CenterY="50"/>
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
<Canvas>
<Canvas.Clip>
<RectangleGeometry Rect="50, 0, 50, 100" />
</Canvas.Clip>
<Rectangle x:Name="HalfRight"
Width="50"
Height="100"
Fill="Red"
Canvas.Left="50"
Canvas.Top="0">
<Rectangle.RenderTransform>
<RotateTransform Angle="-180"
CenterX="0"
CenterY="50"/>
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
</Canvas>
You can modify Angle value to see how it works. I set -180 degrees, so they are invisible, but the angle will be animated to 0.
Then you need two animations in one storyboard. I have added it to main page resources.
<phone:PhoneApplicationPage.Resources>
<Storyboard x:Name="FillAnimation">
<DoubleAnimation Storyboard.TargetName="HalfLeft"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
From="-180"
To="0"
BeginTime="00:00:01"
Duration="00:00:01"/>
<DoubleAnimation Storyboard.TargetName="HalfRight"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
From="-180"
To="0"
Duration="00:00:01"/>
</Storyboard>
</phone:PhoneApplicationPage.Resources>
And that's it. The last thing you have to do is to run the animation from code.
FillAnimation.Begin();
Screenshot:

How do I rotate an image and resize it at the same time

I am developing a Windows Phone 8 game application and I want to rotate the image and at the same time I want to re-size the image from 100 to 0 then again from 0 to 100. Till now what I have done is
<Storyboard x:Name="Rotatetransition" >
<DoubleAnimation Storyboard.TargetName="TransRotate"
Storyboard.TargetProperty="ScaleX"
From="1" To="0"
BeginTime="0:0:0"
Duration="0:0:0.5"
AutoReverse="True"/>
<DoubleAnimation Storyboard.TargetName="TransRotate"
Storyboard.TargetProperty="ScaleY"
From="1" To="0"
BeginTime="0:0:0"
Duration="0:0:0.5"
AutoReverse="True"/>
</Storyboard>
So this animation is resizing my image to 0 from 100. Now the same image I want to rotate so that it will look good and I can load new image for a new game.
For rotaing the image I have code
<Storyboard x:Name="Rotatetransition2" >
<DoubleAnimation Storyboard.TargetName="AnimatedRotateTransform"
Storyboard.TargetProperty="Angle"
By="10"
To="720"
Duration="0:0:0.2"
FillBehavior="Stop" />
</Storyboard>
But the problem is in the Image control I can use only one child <Image.RenderTransform>
<Image x:Name="PreviewImage" Height="480" Width="480" Opacity="1" RenderTransformOrigin="0.5,0.5" >
<Image.RenderTransform>
<ScaleTransform x:Name="TransRotate" />
</Image.RenderTransform>
<!--<Image.RenderTransform>
<RotateTransform x:Name="AnimatedRotateTransform" Angle="0" />
</Image.RenderTransform>-->
</Image>
In the code behind when the game completes I will call
Rotatetransition.Begin();
Circletransition.Begin();
RotatetransitionRev.Begin(); // Again resize the new Loaded image from 0 to 100
Circletransitionrev.Begin(); // Roate the image again from 0 to 720
StartThegame();
Can anyone suggest how do I achieve this?
and the new game will start.
Try using a TransformGroup:
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform />
<RotateTransform />
</TransformGroup>
</Image.RenderTransform>

Image Animation: Make Image move left and then back

I have the following which works for animating the opacity of the image, but what I'd really like to do is have the image move back in forth say 100 pixels to the right then a 100 pixels to the left. But I haven't been able to achieve this effect.
<Image Source="MyImage.jpg" Width="2000" Height="800" x:Name="MyAnimatedImageGeometry">
<Image.Triggers>
<EventTrigger RoutedEvent="Image.Loaded">
<BeginStoryboard>
<Storyboard Storyboard.TargetName="MyAnimatedImageGeometry" Storyboard.TargetProperty="Opacity">
<DoubleAnimation To="0" AutoReverse="True" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
Here's an image:
<Image x:Name="image" Source="/filename.png" RenderTransformOrigin="0.5,0.5" >
<Image.RenderTransform>
<CompositeTransform TranslateX="0"/>
</Image.RenderTransform>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<eim:ControlStoryboardAction Storyboard="{StaticResource moveImage}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
The initialization of the rendor transform is the interesting bit. The trigger is just there to demonstrate starting the storyboard. You wouldn't do this in a real app as tapping the item to move it in this way gets messed up when you tap it while it's moving. (Obviously this would be easy to control in code.)
Here's the other interesting bit, the related storyboard:
<Storyboard x:Name="moveImage" AutoReverse="True" RepeatBehavior="1x">
<DoubleAnimation Duration="0:0:1"
To="-100"
Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)"
Storyboard.TargetName="image"
<DoubleAnimation.EasingFunction>
<CubicEase EasingMode="EaseIn"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
This will move the image 100 pixels to the left (over a period of one second) and then back again.
I've added an easing function to make it more interesting.

Silverlight ProgressBar: Set IndeterminateGradientFill dynamically?

I'm styling a ProgressBar in SL4 and have it nearly done except for the indeterminate animation. This is to be a typed style, and the width of the ProgressBar won't be a fixed value. I'm trying to get set the "To" value in the storyboard, but I'm not having much success...
<DoubleAnimation Duration="00:00:.5" Storyboard.TargetProperty="(Shape.Fill).(LinearGradientBrush.Transform).(TransformGroup.Children)[0].X" Storyboard.TargetName="IndeterminateGradientFill" From="0" To="{Binding ElementName=ProgressBarRootGrid, Path=ActualWidth}"/>
Any suggestions?
I would recommend setting the Shape's HorizontalAlignment to Stretch and then animate the RenderTransforms Scale X property.
This should get your started:
<UserControl x:Class="SilverlightTestImages.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<ControlTemplate x:Key="ProgressBarControlTemplate1" TargetType="ProgressBar">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Determinate"/>
<VisualState x:Name="Indeterminate">
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation Duration="0:0:1" To="1" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Storyboard.TargetName="rectangle" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border BorderBrush="Black" BorderThickness="1" CornerRadius="2">
<Rectangle x:Name="rectangle" Fill="#FF1B6C0B" RadiusX="1" RadiusY="1" RenderTransformOrigin="0,0.5">
<Rectangle.RenderTransform>
<CompositeTransform ScaleX="0"/>
</Rectangle.RenderTransform>
</Rectangle>
</Border>
</Grid>
</ControlTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ProgressBar Height="10" VerticalAlignment="Bottom" Template="{StaticResource ProgressBarControlTemplate1}" IsIndeterminate="True"/>
</Grid>
</UserControl>
Update
To accomplish something similar to VS2010 progress bar is a little bit tougher due to some ways the translation animation is setup. Jeff Wilcox from the Silverlight Test team has a nice control that can be fitted to work for your needs. Go to http://www.jeff.wilcox.name/2010/08/performanceprogressbar/ and under 'Get the code' download the RelativeAnimatingContentControl.cs. Then use he following animation, this should be close to what you are looking for
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Microsoft_Phone_Controls_Unsupported="clr-namespace:Microsoft.Phone.Controls.Unsupported"
x:Class="SilverlightTestImages.MainPage"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<ControlTemplate x:Key="ProgressBarControlTemplate1" TargetType="ProgressBar">
<Microsoft_Phone_Controls_Unsupported:RelativeAnimatingContentControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" >
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Determinate"/>
<VisualState x:Name="Indeterminate">
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation Duration="0:0:1" To="100.1" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="Background" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border BorderBrush="#FFBCBCBC" BorderThickness="1" CornerRadius="2">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFF5F5F5"/>
<GradientStop Color="#FFE5E5E5" Offset="0.49"/>
<GradientStop Color="#FFB4B4B4" Offset="0.5"/>
<GradientStop Color="#FFC8C8C8" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Border BorderBrush="#B2FFFFFF" BorderThickness="1" CornerRadius="2"/>
<Rectangle x:Name="Background" RadiusX="1" RadiusY="1" RenderTransformOrigin="0,0.5" Width="100" HorizontalAlignment="Left">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#0041DE0C"/>
<GradientStop Color="#9941DE0C" Offset="0.4"/>
<GradientStop Color="#9941DE0C" Offset="0.6"/>
<GradientStop Color="#0041DE0C" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
<Rectangle.RenderTransform>
<CompositeTransform TranslateX="-20.1"/>
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
</Border>
</Microsoft_Phone_Controls_Unsupported:RelativeAnimatingContentControl>
</ControlTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ProgressBar Height="20" Width="200" VerticalAlignment="Center" HorizontalAlignment="Center" Template="{StaticResource ProgressBarControlTemplate1}" IsIndeterminate="True" />
</Grid>
</UserControl>

Resources