Skip to main content

Microsoft Silverlight

Answered Question Scalling Silverlight RSS Feed

(0)

hihoboy
hihoboy

Member

Member

24 points

49 Posts

Scalling Silverlight

hello Everybody some can offers me a help pls ! I need to get an animation for scaling but c# code not XAML I mean programaticlly

Ahmad.Saad

Junior SW Developer

prujohn
prujohn

Contributor

Contributor

3579 points

704 Posts

Answered Question

Re: Scalling Silverlight

I wrote a little animation helper a while back that could be useful to you (looking at it now, it could use some polishing, but should get you started).  It does have some base elements in Xaml, but the actual control of the animation is done in C#.  I use it for doing simple on-the-fly scaling and some color shifting.

The Xaml:

<UserControl x:Class="Helpers.animation"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <UserControl.Resources>
        <Storyboard x:Name="animZoom">
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                <SplineDoubleKeyFrame x:Name="kf_animZoomOut_startXScale" KeyTime="00:00:00" Value="1"/>
                <SplineDoubleKeyFrame x:Name="kf_animZoomOut_endXScale" KeyTime="00:00:00.300" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"  Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
                <SplineDoubleKeyFrame x:Name="kf_animZoomOut_startYScale" KeyTime="00:00:00" Value="1"/>
                <SplineDoubleKeyFrame x:Name="kf_animZoomOut_endYScale" KeyTime="00:00:00.300" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Name="animColorShift">
            <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                <SplineColorKeyFrame x:Name="kf_animColorShift_startColor" KeyTime="00:00:00" Value="#FFFFFFFF"/>
                <SplineColorKeyFrame x:Name="kf_animColorShift_endColor" KeyTime="00:00:01" Value="#FFD41717"/>
            </ColorAnimationUsingKeyFrames>
        </Storyboard>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">

    </Grid>
</UserControl>

 The code-behind:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Helpers
{
    sealed public partial class animation : UserControl
    {
        private bool _zoomRelativeLock = false;
        private bool _zoomAbsoluteLock = false;
        private bool _colorShiftLock = false;

        public event EventHandler ColorShiftAnimationComplete;
        public event EventHandler ZoomComplete;
        public void OnZoomComplete(EventArgs e)
        {
            if (ZoomComplete != null)
                ZoomComplete(this, e);
        }
        public void OnColorShiftAnimationComplete(EventArgs e)
        {
            if (ColorShiftAnimationComplete != null)
            {
                ColorShiftAnimationComplete(this, e);
            }
        }
        
        public animation()
        {
            InitializeComponent();
        }

        public void ZoomRelative(UIElement c, double relativeScale)
        {
            if (!_zoomRelativeLock)
            {
                _zoomRelativeLock = true;
                Initialize(animZoom, c);
                kf_animZoomOut_endXScale.Value = kf_animZoomOut_startXScale.Value * relativeScale;
                kf_animZoomOut_endYScale.Value = kf_animZoomOut_startYScale.Value * relativeScale;
                animZoom.Completed += new EventHandler(animZoom_Completed);
                Storyboard.SetTarget(animZoom, c);
                animZoom.Begin();
            }
        }

        void animZoom_Completed(object sender, EventArgs e)
        {

            animZoom.Completed -= new EventHandler(animZoom_Completed);
            kf_animZoomOut_startXScale.Value = kf_animZoomOut_endXScale.Value;
            kf_animZoomOut_startYScale.Value = kf_animZoomOut_endYScale.Value;
            _zoomRelativeLock = false;
            _zoomAbsoluteLock = false;
            OnZoomComplete(new EventArgs());
        }

        public void ZoomAbsolute(UIElement c, double absoluteScale)
        {
            if (!_zoomAbsoluteLock)
            {
                _zoomAbsoluteLock = true;
                Initialize(animZoom, c);
                kf_animZoomOut_endXScale.Value = kf_animZoomOut_endYScale.Value = absoluteScale;
                animZoom.Completed += new EventHandler(animZoom_Completed);
                Storyboard.SetTarget(animZoom, c);
                animZoom.Begin();
            }
        }

        public void ColorShift(Panel c, Color nextColor, int DurationInMilliseconds)
        {
            if (!_colorShiftLock)
            {
                if (animColorShift.GetCurrentState() != ClockState.Stopped)
                    animColorShift.Stop();
                _colorShiftLock = true;
                animColorShift.Completed += new EventHandler(animColorShift_Completed);
                Storyboard.SetTarget(animColorShift, c);
                kf_animColorShift_startColor.Value = ((SolidColorBrush)c.Background).Color;
                kf_animColorShift_endColor.Value = nextColor;
                kf_animColorShift_endColor.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, DurationInMilliseconds));
                animColorShift.Begin();
            }
        }
        
        void animColorShift_Completed(object sender, EventArgs e)
        {
            animColorShift.Stop();
            animColorShift.Completed -= new EventHandler(animColorShift_Completed);
            _colorShiftLock = false;
            OnColorShiftAnimationComplete(new EventArgs());
        }

        private void Initialize(Storyboard sb, UIElement c)
        {
            if (sb.GetCurrentState() != ClockState.Stopped)
                sb.Stop();
            TransformGroup tg = new TransformGroup();
            tg.Children.Add(new ScaleTransform());
            tg.Children.Add(new SkewTransform());
            tg.Children.Add(new RotateTransform());
            tg.Children.Add(new TranslateTransform());
            c.RenderTransform = tg;
            c.RenderTransformOrigin = new Point(.5, .5);
        }
    }
}
 

hihoboy
hihoboy

Member

Member

24 points

49 Posts

Re: Re: Scalling Silverlight

really thx for ur help I apporsiate that but I wanna know how to use ur code coz when I creat new project and get ur code pasted there I just can see empty page 2nd I wanna know how can I change the value scalling during runtime I mean in c# code not Xmal thx man in advance

Ahmad.Saad

Junior SW Developer

hihoboy
hihoboy

Member

Member

24 points

49 Posts

Re: Re: Scalling Silverlight

yeah evantually I could run use ur code its perfect there is no thing I can say abiut this valuable help thx Perfect man

Ahmad.Saad

Junior SW Developer

hihoboy
hihoboy

Member

Member

24 points

49 Posts

Re: Scalling Silverlight

is there any way to have a smooth Zooming for Canvas like a one in DeepZoom

Ahmad.Saad

Junior SW Developer
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities