Windows Phone 8.1 Path按鈕製做分享

前言前端

  常常在博客園看你們的文章,因爲本人文筆太爛,沒寫過博客和小夥伴們分享過,昨天看到安卓同事作的那個按鈕比較不錯,就想着也作個。如今把過程寫出來,請你們不吝賜教(第一次發博客不會排版。。。)你們湊合看吧app

開始動畫

  通常要作動畫以前,習慣用blend先作個動畫,由於blend作動畫很快,能夠方便的看到動畫的效果,待動畫調試差很少的時,用vs打開 (一個展開的動畫和一個收回的動畫,其實設置下Storyboard的AutoReverse屬性能夠自動執行收回的動畫可是過程沒辦法控制,只能寫兩個動畫分開)     this

                      

     根據需求,若有動畫比較靈活,那就要把生成的xaml代碼轉化爲C#代碼了。(用blend的好處就是調試動畫很方便轉化爲C#代碼的時候也比較很簡單),其中用到了四個動畫X軸的平移,Y軸的平移,中心點的旋轉,屬性值的改變。spa

     代碼用到了關鍵幀動畫,因此看起來比較長,其實能夠不用關鍵幀動畫,直接DoubleAnimation就行3d

     這個動畫主要控制五個按鈕的旋轉,從-360到0之間的旋轉動畫調試

        Storyboard expand = new Storyboard();
            for (int i = 0; i < 5; i++)
            {
                //旋轉動畫
                DoubleAnimationUsingKeyFrames rotation = new DoubleAnimationUsingKeyFrames();
                Storyboard.SetTargetProperty(rotation, "(UIElement.RenderTransform).(CompositeTransform.Rotation)");
                //只有動畫在xaml中才能夠使用這個方法
               // Storyboard.SetTargetName(rotation, "image" + i);

                Storyboard.SetTarget(rotation, this.FindName("image"+i) as Image);
                EasingDoubleKeyFrame startAngle = new EasingDoubleKeyFrame();
                startAngle.Value = -360;
                startAngle.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0));
                rotation.KeyFrames.Add(startAngle);
                if (i==2)
                {
                    rotation.BeginTime = TimeSpan.FromMilliseconds(50);
                }
                else if (i==3)
                {
                    rotation.BeginTime = TimeSpan.FromMilliseconds(100);
                }
                else if (i==4)
                {
                    rotation.BeginTime = TimeSpan.FromMilliseconds(150);
                }
                EasingDoubleKeyFrame endAngle = new EasingDoubleKeyFrame();
                endAngle.Value = 0;
                endAngle.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(500));
                rotation.KeyFrames.Add(endAngle);
                expand.Children.Add(rotation);
            }

 

  rotation.BeginTime = TimeSpan.FromMilliseconds(50);code

這個是控制按鈕出來時候的順序,上面的按鈕先執行動畫。orm

 Y軸平移的動畫,每一個按鈕平移的距離不一樣因此只能用if else判斷了。注意blog

//只有動畫在xaml中才能夠使用這個方法 

// Storyboard.SetTargetName(rotation, "image" + i);

 Storyboard.SetTarget(rotation, this.FindName("image"+i) as Image);//代碼生成的動畫用這個方法

   for (int i = 1; i < 5; i++)
            {
                //Y軸平移 
                DoubleAnimationUsingKeyFrames ytransalte = new DoubleAnimationUsingKeyFrames();
                Storyboard.SetTargetProperty(ytransalte, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)");
                Storyboard.SetTarget(ytransalte, this.FindName("image" + i) as Image);
                //起始點
                EasingDoubleKeyFrame ystartPoint = new EasingDoubleKeyFrame();
                ystartPoint.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0));
                if (i == 1)
                {
                    ystartPoint.Value = 180;
                }
                else if (i == 2)
                {
                    ytransalte.BeginTime = TimeSpan.FromMilliseconds(50);
                    ystartPoint.Value = 140;
                }
                else if (i == 3)
                {
                    ytransalte.BeginTime = TimeSpan.FromMilliseconds(100);
                    ystartPoint.Value = 82;
                }
                else if (i == 4)
                {
                    ytransalte.BeginTime = TimeSpan.FromMilliseconds(150);
                    ystartPoint.Value = 10;
                }
                ystartPoint.EasingFunction = new PowerEase() { EasingMode = 0 };
               //終點
                EasingDoubleKeyFrame yendPoint = new EasingDoubleKeyFrame();
                yendPoint.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(500));
                yendPoint.Value = 0;
                yendPoint.EasingFunction = new PowerEase() { EasingMode = 0 };

 

 X軸的動畫大同小異,就不寫了,下面是Image,是否顯示屬性值的改變(按鈕還未出來的時候,是在頁面隱藏的,收回按鈕後,又隱藏)

 

    //改變屬性值的動畫
                ObjectAnimationUsingKeyFrames visi = new ObjectAnimationUsingKeyFrames();
                Storyboard.SetTargetProperty(visi, "(UIElement.Visibility)");
                Storyboard.SetTarget(visi, this.FindName("image" + i) as Image);

                DiscreteObjectKeyFrame startObj = new DiscreteObjectKeyFrame();
                startObj.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0));
                startObj.Value = Visibility.Visible;

                DiscreteObjectKeyFrame endObj = new DiscreteObjectKeyFrame();
                endObj.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(500));
                endObj.Value = Visibility.Visible;

                if (i == 2)
                {
                    visi.BeginTime = TimeSpan.FromMilliseconds(50);
                  
                }
                else if (i == 3)
                {
                    visi.BeginTime = TimeSpan.FromMilliseconds(100);
                }

                else if (i == 4)
                {
                    visi.BeginTime = TimeSpan.FromMilliseconds(150);

                }
                visi.KeyFrames.Add(startObj);
                visi.KeyFrames.Add(endObj);
                expand.Children.Add(visi);

 

上面是按鈕展開的動畫,還要寫收回的動畫,改下展開動畫的代碼就行了。

效果

前端代碼

<Grid>
        <Canvas HorizontalAlignment="Left" Height="228" Margin="0,402,0,0" VerticalAlignment="Top"  Width="234">
            <Image x:Name="image0" Height="50" Width="50" Canvas.Top="180"  RenderTransformOrigin="0.5,0.5" Source="ms-appx:///Assets/Image/button1.png" Tapped="image0_Tapped">
                <Image.RenderTransform>
                    <CompositeTransform/>
                </Image.RenderTransform>
            </Image>
            <Image Visibility="Collapsed" x:Name="image1" Height="50" Width="50" Canvas.Left="45" Source="ms-appx:///Assets/Image/button2.png" RenderTransformOrigin="0.5,0.5">
                <Image.RenderTransform>
                    <CompositeTransform/>
                </Image.RenderTransform>
            </Image>
            <Image Visibility="Collapsed" x:Name="image2" Height="50"  Width="50" Canvas.Top="40" Canvas.Left="110" Source="ms-appx:///Assets/Image/button3.png" RenderTransformOrigin="0.5,0.5">
                <Image.RenderTransform>
                    <CompositeTransform/>
                </Image.RenderTransform>
            </Image>
            <Image Visibility="Collapsed" x:Name="image3" Height="50"  Width="50" Canvas.Top="98" Canvas.Left="150" Source="ms-appx:///Assets/Image/button4.png" RenderTransformOrigin="0.5,0.5">
                <Image.RenderTransform>
                    <CompositeTransform/>
                </Image.RenderTransform>
            </Image>
            <Image Visibility="Collapsed" x:Name="image4" Height="50"  Width="50" Canvas.Top="170" Canvas.Left="175" Source="ms-appx:///Assets/Image/button5.png" RenderTransformOrigin="0.5,0.5" >
                <Image.RenderTransform>
                    <CompositeTransform/>
                </Image.RenderTransform>
            </Image>
        </Canvas>

    </Grid>
相關文章
相關標籤/搜索