Image Animation: Make Image move left and then back - windows-phone-7

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.

Related

LinearGradientBrush spin

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>

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>

Storyboard can't be executed twice

I tried to use an animation in my app and I'm using only two pages to test the animation.
When the app starts for the first time, I want to animate my application title with a sliding effect. The title should come from outside the page.
I used the following code:
<StackPanel x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle"
Text="{Binding LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"
Style="{StaticResource PhoneTextNormalStyle}" RenderTransformOrigin="0.5,0.5" >
<TextBlock.RenderTransform>
<CompositeTransform x:Name="ApplicationTitleTransIn" TranslateX="-200"/>
</TextBlock.RenderTransform>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard BeginTime="00:00:0.5">
<DoubleAnimation Duration="00:00:0.7"
Storyboard.TargetName="ApplicationTitleTransIn"
Storyboard.TargetProperty="TranslateX"
From="-200" To="0">
<DoubleAnimation.EasingFunction>
<BackEase EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</StackPanel>
And it works pretty well.
When I click a button, my application title should move to the right side of my page and then the second page should be displayed.
I created the following storyboard:
<phone:PhoneApplicationPage.Resources>
<Storyboard x:Name="ApplicationTitleTransOut">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="ApplicationTitle">
<EasingDoubleKeyFrame KeyTime="0" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase EasingMode="EaseIn"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="600">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase EasingMode="EaseIn"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</phone:PhoneApplicationPage.Resources>
In the code behind, the storyboard is executed as follow:
ApplicationTitleTransOut.Completed += delegate
{
ApplicationTitleTransOut.Stop();
var vm = DataContext as MainViewModel;
if (vm != null)
{
vm.OpenPageCommand.Execute(listBox.SelectedItem as TileItem);
}
};
ApplicationTitleTransOut.Begin();
The text will move to the right side of my page and then I'll navigate to the second page.
Until now everything works.
But when I press the back button (on the phone) I've an exception.
ExceptionObject = {System.InvalidOperationException: Cannot resolve TargetName ApplicationTitleTransIn.}
Did I miss something? Is this the right way to achieve this animation?
Thank you for your help.
In your situation, I would structure the XAML layout differently. First of all, it does not seem that you need CompositeTransform, but just TranslateTransform. In this case, use this snippet for RenderTransform:
<TextBlock.RenderTransform>
<TranslateTransform x:Name="ApplicationTitleTransIn" X="-200"/>
</TextBlock.RenderTransform>
Now, when you are binding the DoubleAnimation to it, use relative property conventions:
<DoubleAnimation Duration="00:00:0.7"
Storyboard.TargetName="ApplicationTitle"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"
From="-200" To="0">
Same applies to your DoubleAnimationUsingKeyFrames:
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)" Storyboard.TargetName="ApplicationTitle">
Works like a charm.

Why does the ball in this animation look chopped when it changes position?

Here is a link to the animation: http://www.microdivision.com/blog/Strange-Silverlight-Behavior
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="BouncingBall.MainPage"
Width="640" Height="480">
<UserControl.Resources>
<Storyboard x:Name="Bounce" RepeatBehavior="Forever" AutoReverse="True">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="Ball">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:1" Value="-144" KeySpline="0,1,1,1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<i:Interaction.Triggers>
<i:EventTrigger>
<ei:ControlStoryboardAction Storyboard="{StaticResource Bounce}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Canvas x:Name="LayoutRoot" Background="#FFCADFFF">
<Ellipse x:Name="Ball" Stroke="Black" Width="100" Height="100" Fill="#FFF31D1D" Canvas.Top="190" Canvas.Left="0">
<Ellipse.RenderTransform>
<CompositeTransform/>
</Ellipse.RenderTransform>
</Ellipse>
</Canvas>
</UserControl>
As answered by SharpGIS at http://forums.silverlight.net/forums/t/223659.aspx enabling hardware acceleration solved the problem.
For HTML modify the Silverlight control to include the following param:
<param name="EnableGPUAcceleration" value="true" />
I think this may be the result of how Silverlight is adapting during playback. On my machine the ball appears unchopped most of the time despite the fact that I have an older machine with hardly a good CPU/GPU. Silverlight uses only CPU for rendering (except for 3D perspective operations when told to do so) and its time-based animation allows it to cut frames in order to adapt to the host client. It is known for choppiness as a result. Silverlight 5 promises GPU support.
Any reason you are using a spline to create the bounce instead of this?
<Storyboard x:Name="Bounce" RepeatBehavior="Forever" AutoReverse="True">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="Ball">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="-144">
<EasingDoubleKeyFrame.EasingFunction>
<BounceEase EasingMode="EaseIn" Bounciness="0" Bounces="1"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>

Resources