1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

mediaQueryのViewingDistanceでわかる距離

1
Posted at

Jetpack Compose 1.11.x で新たに登場した mediaQueryViewingDistance というユーザーとデバイス画面の間の一般的な距離を取得することができるようになっています。
この "ユーザーとデバイス画面の間の一般的な距離" とは何なのか気になったのでその調べたメモです。

ViewingDistance でわかる距離

ViewingDistance 自体は近接センサーでユーザとデバイスの距離がわかるというものではなく、デバイスの種類でおおよその近さが定義されているものになります。
具体的には以下の表のように、デバイスからのおおよその距離で定数が用意されています。

Property デバイス例
Near(近い) スマートフォン、タブレット、ノートパソコン、デスクトップモニターなど
Medium(中距離) 自動車搭載機器やドックモードのタブレット
Far(遠い) TV

ViewingDistance の使い道

ユーザーとデバイス画面の間の一般的な距離を元にフォントサイズの調整を行ったりするのが主なユースケースになります。

val fontSize = when {
    mediaQuery { viewingDistance == UiMediaScope.ViewingDistance.Far } -> 20.sp
    mediaQuery { viewingDistance == UiMediaScope.ViewingDistance.Medium } -> 18.sp
    else -> 16.sp
}

ViewingDistance の内部実装

下記リンク先が実装内部になります。
PackageManager から TV や Automotive かを判定したり、タブレットのドックモードかを Intent から判定して、ViewingDistance を決定しています。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?