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
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
350
Utilizing Notion as your number one productivity tool
mfonobong
3
220
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.9k
Color Theory Basics | Prateek | Gurzu
gurzu
0
200
Code Reviewing Like a Champion
maltzj
527
40k
Marketing to machines
jonoalderson
1
4.6k
GitHub's CSS Performance
jonrohan
1032
470k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.3k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
100
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
590
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に変換 を使いましょう。(他にもあるかも?)
おしまい!