How to enlarge an Ellipse in WPF? - wpf

How i can enlarge an Ellipse in WPF, preffering using Margins, just like i did in my wrong code :D ? how i can do that, you will understand from the "wrong code"
<DoubleAnimation Duration="00:00:01" Storyboard.TargetProperty="Margin" From="789,-189,762,899" To="23,-189,-4,-637"/>
http://i.stack.imgur.com/Ryn31.png

Use ThicknessAnimation instead of DoubleAnimation :)

I tried enlarging an Ellipse by altering Margin values but did not see any visual change.
I see this question already has been answered, in any case, here is an example of how it is possible to animate the height and Width of an Ellipse in WPF using markup in XAML:
<Ellipse Height="100" Width="100" Fill="Blue" Stroke="Black">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation RepeatBehavior="Forever" Storyboard.TargetProperty="Width" From="100" To="300" Duration="00:00:02" />
<DoubleAnimation RepeatBehavior="Forever" Storyboard.TargetProperty="Height" From="100" To="300" Duration="00:00:02" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>

Related

WPF ellipse path

I am trying to move an image in wpf.
The image should follow an ellipse as a path.
When the animation is complete, it should start again immediately.
(for example a planet makes a revolution)
If I am right i should use DoubleAnimation.
The question is what should I write to the DoubleAnimation tag.
Here is the code I wrote so far:
<Image Source="t:\\moon.jpg" Width="50" Height="50">
<Image.Triggers>
<EventTrigger>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation>
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>
I think you should use PathAnimation not DoubleAnimation.
Example From MSDN:
<EventTrigger RoutedEvent="Button.Loaded">
<BeginStoryboard>
<Storyboard>
<MatrixAnimationUsingPath
Storyboard.TargetName="ButtonMatrixTransform"
Storyboard.TargetProperty="Matrix"
DoesRotateWithTangent="True"
Duration="0:0:5"
RepeatBehavior="Forever" >
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry
Figures="F1 M 147.5,0.5C 228.686,0.5 294.5,24.4528 294.5,54C 294.5,83.5472 228.686,107.5 147.5,107.5C 66.3141,107.5 0.5,83.5472 0.5,54C 0.5,24.4528 66.3142,0.5 147.5,0.5 Z"
PresentationOptions:Freeze="True" />
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
You can learn about Path Markup Syntax here

Textbox Text-Color Animation

Is it possible to animate just the part of the value of textbox/label. For example, txt1.text = "This is a Sample";. then I want to animate the word "Sample" to change its color/opacity etc. If you get what i mean. If it is possible please demonstrate the code Thanks!
I think you can't do it with a TextBox as its text has no formatting options. Not sure if the Label allows it, but the TextBlock might help you:
<TextBlock>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="scb01"
Storyboard.TargetProperty="Color"
From="White"
To="Black"
Duration="0:0:0.5"
AutoReverse="True"
RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
One <Run>
<Run.Foreground>
<SolidColorBrush x:Name="scb01"
Color="Red" />
</Run.Foreground>
Two</Run> Three
</TextBlock>

How to zoom in on a image when Image.MouseEnter fires?

I would like to zoom in on a Image when Image.MouseEnter fires and then zoom out when Image.MouseLeave fires.
I thought of creating a Trigger, but no luck.
This is what i tried so far:
<Image Name="logo" Source="{Binding Path=ImagePath}"
Width="50" Height="50">
<Image.RenderTransform>
<ScaleTransform x:Name="TransRotate" />
</Image.RenderTransform>
<Image.Triggers>
<EventTrigger RoutedEvent="Image.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="TransRotate"
Storyboard.TargetProperty="ScaleX"
From="0" To="100"
BeginTime="0:0:0"
Duration="0:0:10"
AutoReverse="False"/>
<DoubleAnimation Storyboard.TargetName="TransRotate"
Storyboard.TargetProperty="ScaleY"
From="0" To="100"
BeginTime="0:0:0"
Duration="0:0:10"
AutoReverse="False"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>
Is this the correct way, or is there a better way?
I would remove the From property, this makes the animation jump, apart from that you just seem to be lacking the reverse animations on MouseLeave. Also to center the zoom you can set the RenderTransformOrigin of the Image to 0.5,0.5.
Instead of using those two events i usually prefer a trigger on IsMouseOver with Enter and ExitActions.
If you want to retain the space the image takes you can place it in a container with fixed size and set ClipToBounds to true.

WPF Animation - Why won't my image spin?

I've created a simple user control in WPF and added an image to it:
<Image x:Name="logo" Source="/View/Images/Logo.png" Width="100" Height="100">
<Image.RenderTransform>
<RotateTransform Angle="0" CenterX="50" CenterY="50" />
</Image.RenderTransform>
</Image>
I want that image to spin constantly and to fade in and out. This is to be used as a busy indicator. So I created this animation storyboard:
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation From="0" To="360" Duration="00:00:05" Storyboard.TargetName="logo" Storyboard.TargetProperty="(RotateTransform.Angle)" />
<DoubleAnimation From="0.1" To="1" AutoReverse="True" Duration="00:00:02" Storyboard.TargetName="logo" Storyboard.TargetProperty="Opacity" />
</Storyboard>
However, when I view a window with the usercontrol in it, the fading works but the spiinning doesn't.
Your StoryBoard.TargetProperty is wrong, it should be:
Storyboard.TargetProperty="(Image.RenderTransform).(RotateTransform.Angle)"

Why is my WPF Path Animation flickering?

Why does the following animation flicker and act goofy on MouseLeave? If it can be repro-ed, I'll post a screencast.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas>
<Path Fill="Blue" Margin="15,15,15,15">
<Path.Data>
<!-- Describes an ellipse. -->
<EllipseGeometry x:Name="MyAnimatedEllipseGeometry" Center="200,100" RadiusX="15" RadiusY="15" />
</Path.Data>
<Path.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseEnter">
<BeginStoryboard>
<Storyboard Duration="0:0:.5">
<DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry"
Storyboard.TargetProperty="RadiusX"
From="15" To="100" />
<DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry"
Storyboard.TargetProperty="RadiusY"
From="15" To="100" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave">
<BeginStoryboard>
<Storyboard Duration="0:0:.5">
<DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry"
Storyboard.TargetProperty="RadiusX"
From="100" To="15" />
<DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry"
Storyboard.TargetProperty="RadiusY"
From="100" To="15" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Path.Triggers>
</Path>
</Canvas>
</Page>
The reason is that you specify a From on your DoubleAnimations.
If the radius is anything less than 100 when MouseLeave happens, the From property will make it jump up to 100 and likely cause a MouseEnter. Then, you have two competing animations and the mouse events go crazy as the ellipse radius flickers underneath the cursor.
The solution is to just omit the From properties, this will cause the animation to start from wherever the current radius is:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1">
<Canvas>
<Path Margin="15,15,15,15" Fill="Blue">
<Path.Data>
<!-- Describes an ellipse. -->
<EllipseGeometry x:Name="MyAnimatedEllipseGeometry" Center="200,100" RadiusX="15" RadiusY="15"/>
</Path.Data>
<Path.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry" Duration="0:0:.5" Storyboard.TargetProperty="RadiusX" To="100"/>
<DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry" Duration="0:0:.5" Storyboard.TargetProperty="RadiusY" To="100"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry" Duration="0:0:.5" Storyboard.TargetProperty="RadiusX" To="15"/>
<DoubleAnimation Storyboard.TargetName="MyAnimatedEllipseGeometry" Duration="0:0:.5" Storyboard.TargetProperty="RadiusY" To="15"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Path.Triggers>
</Path>
</Canvas>
</Page>
On an unrelated note, when you set Storyboard.Duration, it won't speed up your child animations, it will just end the Storyboard prematurely. You really want to set Duration on your child DoubleAnimations - I've modified the XAML above to do this.
There's always the usual culprits of hardware. But I would suspect that it may have something to do with the time being taken to perform the animation. Try increasing the length of time the animation runs, or adjusting the size of the values you are moving between.
half a second is not much time to transition from 100 to 15 and vice versa, so you may just be running into not having a smooth enough transition, resulting in a choppy animation. Remember that time is key and that your target should be at least 30 fps for a smooth animation.

Resources