to obtain the density for dp to px conversions? Could density “config change”?
Copy code
val Int.dp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
val Int.px: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
r
Ryan
12/02/2020, 5:07 PM
You are better off passing in resources or context.
Ryan
12/02/2020, 5:07 PM
that way you ensure this is called in the appropriate place
Ryan
12/02/2020, 5:08 PM
also I would do this:
TypedValue.applyDimension
r
Ricardo C.
12/02/2020, 5:11 PM
Back to the old ways then. Just found out about
Resources.getSystem()
, it makes the extensions so clean…
r
Ryan
12/02/2020, 5:11 PM
realistically you are fine
m
Marco
12/03/2020, 2:32 PM
imho, if you use
Resources.getSystem()
inside a class the risk is that you can’t create unit test on it, because you are using a resource statically. This is my opinion in terms of testability.