LoginSignup
4
1

More than 1 year has passed since last update.

JetpackComposeでDpからPxを簡単に計算する拡張関数

Posted at

毎回LocalDensityを使用して取得するのは面倒なので拡張関数にする

@Composable
fun Hoge(){
  val size = 10.dp
  val sizePx = with(LocalDensity.current) { size.toPx() }
}

以下のような拡張関数を作成する

@Composable
fun Dp.toPx(): Float = with(LocalDensity.current) { this@toPx.toPx() }

使い方

@Composable
fun Hoge(){
  val size = 10.dp
    val sizePx = size.toPx()
}
4
1
1

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
4
1