Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
screenWidthDpちょっと 怖いかも、、??
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
takarabe-hamuyatti
December 02, 2023
0
470
screenWidthDpちょっと 怖いかも、、??
dpとかpxに関する話です。
takarabe-hamuyatti
December 02, 2023
Tweet
Share
More Decks by takarabe-hamuyatti
See All by takarabe-hamuyatti
Performance Classを触ってみよう
rabeyatti
0
360
Featured
See All Featured
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
140
From π to Pie charts
rasagy
0
120
Reality Check: Gamification 10 Years Later
codingconduct
0
2k
Skip the Path - Find Your Career Trail
mkilby
0
57
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
120
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
Designing for humans not robots
tammielis
254
26k
Done Done
chrislema
186
16k
sira's awesome portfolio website redesign presentation
elsirapls
0
150
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
190
My Coaching Mixtape
mlcsv
0
48
Transcript
screenWidthDpちょっと 怖いかも、、??
目次 • 自己紹介 • 結論 • 基礎知識 • screenWidthDpについて •
何が落とし穴? • screenWidthDpをもとにpxを作ると? • 改めて結論
自己紹介 財部彰太 twitter : rabe_hamuyatti 株式会社ZOZO 2023新卒 ZOZOTOWNのAndroidアプリを作って います。 旅行とか、服とか、音楽が好きです。
結論 画面幅をpxの単位でほしい時は、Configuration.screenWidthDpをpxに変換すると正 確な値が取れない。 BoxWithConstraintsScope.maxWidthにtoPxをかけたもの または Resources.getSystem().displayMetrics.width を使おう。
基礎知識 px → デジタル画像の最小単位。 dpi(dot per inch) → 1インチあたりのドット数。画面密度 dp(density-independent
pixels) → 密度非依存ピクセル。dpが端末の画面密度に応じてpxに変換され、ユーザーの目に 届く。
screenWidthDpについて dp単位で表示可能な画面領域の幅のこと。(WindowInsetを除いた値) 取得方法 • Resources.getSystem().configuration.screenWidthDp • LocalConfiguration.current.screenWidthDp 公式Document https://developer.android.com/reference/android/content/res/Configuration#screenWidthDp
何が落とし穴? Floatではなく、Intで保存されていること (BoxWithConstraintsScope.maxWidthはFloat) DPを求める式は 画面幅(px) * 160 / dpi →
割り切れなさそうじゃない、、?? 計算式 https://developer.android.com/training/multiscreen/screendensities?hl=ja#dips-pels screenWidthDpの実際のコード https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/content/res/Configurat
何が落とし穴? 実際、Pixel6では 横幅 → 1080px dpi → 420 1080 *
160 / 420 = 411.4285 → ScreenWidthDpはIntのため、端数が切り落とされ411で保存されている 公式Document https://developer.android.com/reference/android/content/res/Configuration#screenWidthDp
screenWidthDpをもとにpxを作ると? LocalConfiguration.current.screenWidthDp.dp.toPx() や Resources.getSystem().configuration.screenWidthDp * resources.displayMetrics.densityDpi / 160 → 1078.875
本来の1080とずれてしまっている!!!
改めて。 px単位で正確な画面横幅を知りたいときは Resources.getSystem().displayMetrics.widthPixels BoxWithConstraintsScope.maxWidthをpxに変換 を使いましょう。(他にもあるかも?)
おしまい!