楕円を波のアニメーションで塗りつぶす

Shubham Sahu:

私はWindows Phone 8.1 SilverlightアプリとUWPの両方で楕円を作成し、アニメーションの波でそれを埋めたかったので、この目的のために、私はこの解決策に従います

しかし、それはWPF用なので、「ビジュアルブラシ」などのコントロールを使用できません。

これに似た波で楕円を塗りたくなりました(画像の50%は無視します)-

ここに画像の説明を入力してください

そして、これが私のエリプスです

<Ellipse Name="WaveEllipse" Grid.Column="1" Grid.Row="0" VerticalAlignment="Top"
         Stroke="{StaticResource PhoneAccentBrush}"
         StrokeThickness="4"
         Width="225"
         Height="225">
</Ellipse>

視覚ブラシの代替はありますか?主にWindows Phone 8.1 Silverlightで実装したかったのですが、WPプラットフォームで利用できない場合はUWPに切り替えます

ジャスティンXL:

コードを与える前に、このアニメーションGIFを見て、このアニメーションがどのように作成されるかを理解してください。

ここに画像の説明を入力してください

Make sense, right? All we need to do is to create a shape like this, animate its offset X(endlessly) and Y(water level), and finally just clip it with an ellipse.

So first you will need to use Adobe Illustrator or similar tools to create this shape. In AI, there's a Zig Zag effect(see screenshot below) that's perfectly for this. You just need to make sure the starting point is at the same position as the ending one, so when you repeat the animation, it will feel like it's never ending.

ここに画像の説明を入力してください

What's currently missing in UWP is the ability to clip a UIElement with a non-rectangular shape, so here we have to export this as a png (otherwise we would export it as a svg and use Path to display it).

Also for the same reason, the clipping part requires a lot of work. Like in Jet Chopper's answer, that's tons of code to just get a surfaceBrush! Not to mention that you will also need to manually handle device lost and app lifecycle.

Thankfully, in Creators Update(i.e. 15063), there's a new API called LoadedImageSurface that creates a CompositionSurfaceBrush by an image uri with a couple of lines' code. In my code example below, you will see that I use this, which means, if you want to support older versions of Windows 10, you will need to replace it with what's in Jet's answer.

Code

The idea is to create a UserControl called WaveProgressControl which encapsulates all the animation logic and exposes a dependency property called Percent that controls the water level.

WaveProgressControlコントロール- XAML

<UserControl x:Class="WaveProgressControlRepo.WaveProgressControl"
             Height="160"
             Width="160">

    <Grid x:Name="Root">
        <Ellipse x:Name="ClippedImageContainer"
                 Fill="White"
                 Margin="6" />
        <Ellipse x:Name="CircleBorder"
                 Stroke="#FF0289CD"
                 StrokeThickness="3" />
        <TextBlock Foreground="#FF0289CD"
                   FontSize="36"
                   FontWeight="SemiBold"
                   TextAlignment="Right"
                   VerticalAlignment="Center"
                   Width="83"
                   Margin="0,0,12,0">
            <Run Text="{x:Bind Percent, Mode=OneWay}" />
            <Run Text="%"
                 FontSize="22" />
        </TextBlock>
    </Grid>
</UserControl>

WaveProgressControlコントロール-コードビハインド

private readonly Compositor _compositor;
private readonly CompositionPropertySet _percentPropertySet;

public WaveProgressControl()
{
    InitializeComponent();

    _compositor = Window.Current.Compositor;

    _percentPropertySet = _compositor.CreatePropertySet();
    _percentPropertySet.InsertScalar("Value", 0.0f);

    Loaded += OnLoaded;
}

public double Percent
{
    get => (double)GetValue(PercentProperty);
    set => SetValue(PercentProperty, value);
}
public static readonly DependencyProperty PercentProperty =
    DependencyProperty.Register("Percent", typeof(double), typeof(WaveProgressControl),
        new PropertyMetadata(0.0d, (s, e) =>
        {
            var self = (WaveProgressControl)s;
            var propertySet = self._percentPropertySet;
            propertySet.InsertScalar("Value", Convert.ToSingle(e.NewValue) / 100);
        }));

private void OnLoaded(object sender, RoutedEventArgs e)
{
    CompositionSurfaceBrush imageSurfaceBrush;

    SetupClippedWaveImage();
    SetupEndlessWaveAnimationOnXAxis();
    SetupExpressionAnimationOnYAxisBasedOnPercentValue();

    void SetupClippedWaveImage()
    {
        // Note LoadedImageSurface is only available in 15063 onward.
        var imageSurface = LoadedImageSurface.StartLoadFromUri(new Uri(BaseUri, "/Assets/wave.png"));
        imageSurfaceBrush = _compositor.CreateSurfaceBrush(imageSurface);
        imageSurfaceBrush.Stretch = CompositionStretch.None;
        imageSurfaceBrush.Offset = new Vector2(120, 248);

        var maskBrush = _compositor.CreateMaskBrush();
        var maskSurfaceBrush = ClippedImageContainer.GetAlphaMask(); // CompositionSurfaceBrush
        maskBrush.Mask = maskSurfaceBrush;
        maskBrush.Source = imageSurfaceBrush;

        var imageVisual = _compositor.CreateSpriteVisual();
        imageVisual.RelativeSizeAdjustment = Vector2.One;
        ElementCompositionPreview.SetElementChildVisual(ClippedImageContainer, imageVisual);

        imageVisual.Brush = maskBrush;
    }

    void SetupEndlessWaveAnimationOnXAxis()
    {
        var waveOffsetXAnimation = _compositor.CreateScalarKeyFrameAnimation();
        waveOffsetXAnimation.InsertKeyFrame(1.0f, -80.0f, _compositor.CreateLinearEasingFunction());
        waveOffsetXAnimation.Duration = TimeSpan.FromSeconds(1);
        waveOffsetXAnimation.IterationBehavior = AnimationIterationBehavior.Forever;
        imageSurfaceBrush.StartAnimation("Offset.X", waveOffsetXAnimation);
    }

    void SetupExpressionAnimationOnYAxisBasedOnPercentValue()
    {
        var waveOffsetYExpressionAnimation = _compositor.CreateExpressionAnimation("Lerp(248.0f, 120.0f, Percent.Value)");
        waveOffsetYExpressionAnimation.SetReferenceParameter("Percent", _percentPropertySet);
        imageSurfaceBrush.StartAnimation("Offset.Y", waveOffsetYExpressionAnimation);
    }
}

MainPage

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <local:WaveProgressControl x:Name="WaveProgressControl" />

    <Slider Grid.Row="1"
            Margin="24"
            Value="{x:Bind WaveProgressControl.Percent, Mode=TwoWay}" />
</Grid>

私はすべてをこのサンプルプロジェクトに入れました。以下はライブデモです。楽しい!:)

ここに画像の説明を入力してください

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

divを円形のカラーアニメーションで塗りつぶす

分類Dev

Androidでアニメーションを使用して中心から円の背景色をゆっくり塗りつぶす方法

分類Dev

Androidで「塗りつぶし」アニメーションを作成する方法

分類Dev

iOS-UIBezierPathのアニメーションを下から塗りつぶす

分類Dev

コンソールアプリケーションで文字で作られた塗りつぶされた楕円を「描画」する

分類Dev

円をグラデーションで塗りつぶす

分類Dev

UIViewの一部をアニメーションで色で塗りつぶします

分類Dev

雲または任意のフォームを色で塗りつぶします(アニメーション)

分類Dev

UIImageViewを他の色のアニメーションで塗りつぶします

分類Dev

.trimを使用する代わりに、円をアニメーション化して塗りつぶすにはどうすればよいですか?

分類Dev

Swift での SpriteKit カラー塗りつぶしアニメーション

分類Dev

円がテキストで塗りつぶされたjqueryキャンバスアニメーション

分類Dev

カラーSVGパスをアニメーションで塗りつぶします

分類Dev

divの背景色の塗りつぶしをアニメーション化する方法

分類Dev

CSSを使用してSVGパス要素の塗りつぶしをアニメーション化する

分類Dev

jqueryを使用してSVGの塗りつぶしをアニメーション化しますか?

分類Dev

アニメーション効果の塗りつぶしSVGを描画する方法は?

分類Dev

ホバー中にテキストカラーアニメーションをアニメーションカラーで塗りつぶします

分類Dev

円をグラデーションで塗りつぶす方法は?

分類Dev

マウスオーバー/マウスアウト(SMIL)でインラインSVGの塗りつぶし属性をアニメーション化する方法

分類Dev

svgパス塗りつぶしアニメーション

分類Dev

スライダーハンドラーを使用して楕円を円弧で塗りつぶす

分類Dev

CSS塗りつぶしアニメーションが空白で途切れる

分類Dev

svg塗りつぶしパスアニメーションの問題

分類Dev

アニメーションによる Svg パスの塗りつぶし

分類Dev

塗りつぶされた楕円をPysideでペイントする

分類Dev

JavaScriptを使用したSVG要素の塗りつぶしの不透明度のアニメーション

分類Dev

ボタン全体を塗りつぶすグラデーション付きのアニメーションを作成する方法

分類Dev

svgアニメーションが完了した後に色を塗りつぶしたいのですが、実行されません

Related 関連記事

  1. 1

    divを円形のカラーアニメーションで塗りつぶす

  2. 2

    Androidでアニメーションを使用して中心から円の背景色をゆっくり塗りつぶす方法

  3. 3

    Androidで「塗りつぶし」アニメーションを作成する方法

  4. 4

    iOS-UIBezierPathのアニメーションを下から塗りつぶす

  5. 5

    コンソールアプリケーションで文字で作られた塗りつぶされた楕円を「描画」する

  6. 6

    円をグラデーションで塗りつぶす

  7. 7

    UIViewの一部をアニメーションで色で塗りつぶします

  8. 8

    雲または任意のフォームを色で塗りつぶします(アニメーション)

  9. 9

    UIImageViewを他の色のアニメーションで塗りつぶします

  10. 10

    .trimを使用する代わりに、円をアニメーション化して塗りつぶすにはどうすればよいですか?

  11. 11

    Swift での SpriteKit カラー塗りつぶしアニメーション

  12. 12

    円がテキストで塗りつぶされたjqueryキャンバスアニメーション

  13. 13

    カラーSVGパスをアニメーションで塗りつぶします

  14. 14

    divの背景色の塗りつぶしをアニメーション化する方法

  15. 15

    CSSを使用してSVGパス要素の塗りつぶしをアニメーション化する

  16. 16

    jqueryを使用してSVGの塗りつぶしをアニメーション化しますか?

  17. 17

    アニメーション効果の塗りつぶしSVGを描画する方法は?

  18. 18

    ホバー中にテキストカラーアニメーションをアニメーションカラーで塗りつぶします

  19. 19

    円をグラデーションで塗りつぶす方法は?

  20. 20

    マウスオーバー/マウスアウト(SMIL)でインラインSVGの塗りつぶし属性をアニメーション化する方法

  21. 21

    svgパス塗りつぶしアニメーション

  22. 22

    スライダーハンドラーを使用して楕円を円弧で塗りつぶす

  23. 23

    CSS塗りつぶしアニメーションが空白で途切れる

  24. 24

    svg塗りつぶしパスアニメーションの問題

  25. 25

    アニメーションによる Svg パスの塗りつぶし

  26. 26

    塗りつぶされた楕円をPysideでペイントする

  27. 27

    JavaScriptを使用したSVG要素の塗りつぶしの不透明度のアニメーション

  28. 28

    ボタン全体を塗りつぶすグラデーション付きのアニメーションを作成する方法

  29. 29

    svgアニメーションが完了した後に色を塗りつぶしたいのですが、実行されません

ホットタグ

アーカイブ