Wpf 抽屜效果

在android開發中有抽屜效果,就是在頁面的邊上有一個按鈕,能夠經過點擊或者拖拽這個按鈕,讓頁面顯示。Wpf也能夠實現相同的效果。android

 

主要是經過一個DoubleAnimation和RectAnimation動畫實現動畫

<Grid HorizontalAlignment="Center" Width="90" Height="130" Background="Blue">
            <Image x:Name="Thumb" Source="high.png" Width="90" Height="125">
                <Image.RenderTransform>
                    <TranslateTransform x:Name="Translate"></TranslateTransform>
                </Image.RenderTransform>
                <Image.Clip>
                    <RectangleGeometry x:Name="ClipRect" Rect="0,0,90,125" ></RectangleGeometry>
                </Image.Clip>
            </Image>
        </Grid>

 

private bool _Expand = true;
        public bool Expand
        {
            get { return _Expand; }
            set
            {
                _Expand = value;

                Duration duration = new Duration(TimeSpan.FromSeconds(1));
                FillBehavior behavior = FillBehavior.HoldEnd;

                DoubleAnimation translateAnim = new DoubleAnimation();
                translateAnim.Duration = duration;
                translateAnim.FillBehavior = behavior;

                RectAnimation clipAnim = new RectAnimation();
                clipAnim.Duration = duration;
                clipAnim.FillBehavior = behavior;

                double delta = 80; //收縮的大小

                if (_Expand) // Expand
                {
                    translateAnim.From = -delta;
                    translateAnim.To = 0;

                    clipAnim.From = new Rect(delta, 0, Thumb.ActualWidth, Thumb.ActualHeight);
                    clipAnim.To = new Rect(0, 0, Thumb.ActualWidth, Thumb.ActualHeight);
                }
                else  //Shrink
                {
                    translateAnim.From = 0;
                    translateAnim.To = -delta;

                    clipAnim.From = new Rect(0, 0, Thumb.ActualWidth, Thumb.ActualHeight);
                    clipAnim.To = new Rect(delta, 0, Thumb.ActualWidth, Thumb.ActualHeight);
                }

                Translate.BeginAnimation(TranslateTransform.XProperty, translateAnim);
                ClipRect.BeginAnimation(RectangleGeometry.RectProperty, clipAnim);
            }
        }

 

Demo地址spa

http://pan.baidu.com/s/1gdxHhnXcode

相關文章
相關標籤/搜索