タイル アニメーション

タイルは次のような複数の方法でアニメーション化できます。

スイープ遷移を表示する

ある値から別の値へのスムーズなスイープを表示するには、次のコード スニペットに示すように、要素のトゥイーン アニメーションを有効にします。

private var startValue = 15f
private var endValue = 105f
private val animationDurationInMillis = 2000L // 2 seconds

override fun onTileRequest(requestParams: RequestBuilders.TileRequest): ListenableFuture<Tile> {
    val circularProgressIndicator =
        CircularProgressIndicator.Builder()
            .setProgress(
                FloatProp.Builder(/* static value */ 0.25f)
                    .setDynamicValue(
                        // Or you can use some other dynamic object, for example
                        // from the platform and then at the end of expression
                        // add animate().
                        DynamicFloat.animate(
                            startValue,
                            endValue,
                            AnimationSpec.Builder()
                                .setAnimationParameters(
                                    AnimationParameters.Builder()
                                        .setDurationMillis(animationDurationInMillis)
                                        .build()
                                )
                                .build(),
                        )
                    )
                    .build()
            )
            .build()

    return Futures.immediateFuture(
        Tile.Builder()
            .setResourcesVersion(RESOURCES_VERSION)
            .setTileTimeline(Timeline.fromLayoutElement(circularProgressIndicator))
            .build()
    )
}

円弧の向きを設定する

タイルに円弧が含まれている場合、ユーザーが選択した言語のデフォルトのテキスト方向で円弧線やテキストが常に伸びるようにすることはおすすめしません。円弧の伸び方向を指定するには、ArcDirection API を使用します。

public override fun onTileRequest(
    requestParams: RequestBuilders.TileRequest
): ListenableFuture<Tile> {
    return Futures.immediateFuture(
        Tile.Builder()
            .setResourcesVersion(RESOURCES_VERSION)
            .setTileTimeline(
                Timeline.fromLayoutElement(
                    EdgeContentLayout.Builder(deviceParameters)
                        .setResponsiveContentInsetEnabled(true)
                        .setEdgeContent(
                            Arc.Builder()
                                // Arc should always grow clockwise.
                                .setArcDirection(LayoutElementBuilders.ARC_DIRECTION_CLOCKWISE)
                                .addContent(
                                    ArcLine.Builder()
                                        // Set color, length, thickness, and more.
                                        // Arc should always grow clockwise.
                                        .setArcDirection(
                                            LayoutElementBuilders.ARC_DIRECTION_CLOCKWISE
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .build()
                )
            )
            .build()
    )
}

スムーズなフェードまたはスライドを表示する

明確にタイルでの要素の表示と非表示を表したり、タイルの値の段階的な変化を自然に示したりするには、タイル アニメーションでフェード効果とスライド効果を使用します。

タイル レイアウトに値が変更された要素が含まれている場合、タイルは要素の終了アニメーションを表示した後で、レイアウトを更新して要素の開始アニメーションを表示します。

フェード遷移

次のコード スニペットは、DefaultContentTransitions のヘルパー メソッドを使用して、フェードインとフェードアウトの遷移を実行する方法を示しています。カスタムの FadeInTransition オブジェクトと FadeOutTransition オブジェクトを定義するには、遷移セッター メソッドで setFadeIn()setFadeOut() をそれぞれ呼び出します。

public override fun onTileRequest(
    requestParams: RequestBuilders.TileRequest
): ListenableFuture<Tile> {
    // Assumes that you've defined a custom helper method called
    // getTileTextToShow().
    val tileText = getTileTextToShow()
    return Futures.immediateFuture(
        Tile.Builder()
            .setResourcesVersion(RESOURCES_VERSION)
            .setTileTimeline(
                Timeline.fromLayoutElement(
                    Text.Builder(this, tileText)
                        .setModifiers(
                            Modifiers.Builder()
                                .setContentUpdateAnimation(
                                    AnimatedVisibility.Builder()
                                        .setEnterTransition(DefaultContentTransitions.fadeIn())
                                        .setExitTransition(DefaultContentTransitions.fadeOut())
                                        .build()
                                )
                                .build()
                        )
                        .build()
                )
            )
            .build()
    )
}

スライド遷移

このコード スニペットは DefaultContentTransitions のヘルパー メソッドを使用して、スライドインとスライドアウトの遷移を実行する方法を示しています。カスタムの SlideInTransition オブジェクトと SlideOutTransition オブジェクトを定義する場合は、遷移セッター メソッドで setSlideIn()setSlideOut() をそれぞれ呼び出します。

public override fun onTileRequest(
    requestParams: RequestBuilders.TileRequest
): ListenableFuture<Tile> {
    // Assumes that you've defined a custom helper method called
    // getTileTextToShow().
    val tileText = getTileTextToShow()
    return Futures.immediateFuture(
        Tile.Builder()
            .setResourcesVersion(RESOURCES_VERSION)
            .setTileTimeline(
                Timeline.fromLayoutElement(
                    Text.Builder(this, tileText)
                        .setModifiers(
                            Modifiers.Builder()
                                .setContentUpdateAnimation(
                                    AnimatedVisibility.Builder()
                                        .setEnterTransition(
                                            DefaultContentTransitions.slideIn(
                                                ModifiersBuilders.SLIDE_DIRECTION_LEFT_TO_RIGHT
                                            )
                                        )
                                        .setExitTransition(
                                            DefaultContentTransitions.slideOut(
                                                ModifiersBuilders.SLIDE_DIRECTION_LEFT_TO_RIGHT
                                            )
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .build()
                )
            )
            .build()
    )
}

変換を表示する

タイルの特定の要素や領域に注意を向けさせるには、回転、スケーリング、移動など、複数の種類の変換を適用できます。

変換に関連付けられた多くの浮動小数点値は、動的式を受け付けます。これにより、これらの変換をアニメーション化できます。

回転

カスタマイズ可能なピボット ポイントを中心に時計回りに回転するには、次のようなコードを使用します。

return Futures.immediateFuture(
    Tile.Builder()
        .setResourcesVersion(RESOURCES_VERSION)
        .setTileTimeline(
            Timeline.fromLayoutElement(
                Text.Builder(this, someTileText)
                    .setModifiers(
                        Modifiers.Builder()
                            .setTransformation(
                                ModifiersBuilders.Transformation.Builder()
                                    // Set the pivot point 50 dp from the left edge
                                    // and 100 dp from the top edge of the screen.
                                    .setPivotX(dp(50f))
                                    .setPivotY(dp(100f))
                                    // Rotate the element 45 degrees clockwise.
                                    .setRotation(degrees(45f))
                                    .build()
                            )
                            .build()
                    )
                    .build()
            )
        )
        .build()
)

スケーリング

要素を水平方向と垂直方向のスケーリング ファクタで拡大または縮小するには、次のようなコードを使用します。

return Futures.immediateFuture(
    Tile.Builder()
        .setResourcesVersion(RESOURCES_VERSION)
        .setTileTimeline(
            Timeline.fromLayoutElement(
                Text.Builder(this, someTileText)
                    .setModifiers(
                        Modifiers.Builder()
                            .setTransformation(
                                ModifiersBuilders.Transformation.Builder()
                                    // Set the pivot point 50 dp from the left edge
                                    // and 100 dp from the top edge of the screen.
                                    .setPivotX(dp(50f))
                                    .setPivotY(dp(100f))
                                    // Shrink the element by a scale factor
                                    // of 0.5 horizontally and 0.75 vertically.
                                    .setScaleX(FloatProp.Builder(0.5f).build())
                                    .setScaleY(
                                        FloatProp.Builder(0.75f).build()
                                    )
                                    .build()
                            )
                            .build()
                    )
                    .build()
            )
        )
        .build()
)

幾何学的変換

要素を画面上で水平方向または垂直方向に特定の数の密度ピクセル(dp)移動するには、次のようなコードを使用します。

return Futures.immediateFuture(
    Tile.Builder()
        .setResourcesVersion(RESOURCES_VERSION)
        .setTileTimeline(
            Timeline.fromLayoutElement(
                Text.Builder(this, someTileText)
                    .setModifiers(
                        Modifiers.Builder()
                            .setTransformation(
                                ModifiersBuilders.Transformation.Builder()
                                    // Translate (move) the element 60 dp to the right
                                    // and 80 dp down.
                                    .setTranslationX(dp(60f))
                                    .setTranslationY(dp(80f))
                                    .build()
                            )
                            .build()
                    )
                    .build()
            )
        )
        .build()
)

アニメーションの途中で重要な情報を表示しない

次のような状況ではアニメーションが無効になります。

  • システムのタイル レンダリングにより、すべてのタイルのアニメーションが無効になることがあります。
  • タイルは一度に 4 つの要素のみアニメーション化できます。4 つを超える要素を同時にアニメーション化しようとすると、一部のアニメーションのみ表示されます。

アニメーションが無効になっている場合、要素は静的となりアニメーションの終了値を表示します。そのため、重要な情報を表示する場合は、アニメーションの長さなどの動作に依存しないようにします。