Reviewing some code today where a BorderStroke was...
# compose-android
c
Reviewing some code today where a BorderStroke was set to 1.66dp Having a
dp
set to a
.66
value seems really weird. Is there any sort of documentation i can point to for this instead of just having a feeling that this is wrong? looks like we just copied the exact val from sketch.
k
Was probably tested on a device with fractional scaling, where the designer wanted the "exact" thickness in pixels.
c
Is your concern specifically about the .66? Or do you feel weird about any fractional value for dp? I sometimes use values like 1.5 dp. They're specifically common for things like dividers and borders.
c
I don't think I've ever used a fractional value in dp so I was wondering if it's just weird to do that.
y
On some devices 2.dp might be fractional. Density is a float also
Not sure if it's common
a
It's very common for density to not be an integer - any dpi that isn't a multiple of
160
is going to have a
Density
conversion value that isn't an integer. For Pixel 9 and before, only the Pixel 8 Pro, Pixel 9 Pro, and Pixel 9 Pro XL will have a default density value that's an integer at 480dpi (and you can change that value in settings, so you shouldn't ever assume a certain device equates to specific density)
c
got it. i guess i never use floating point dp values which was the cause of this. but seems like dp get converted to int anyway and they could be floating point. thank you all for the conversation
y
Where is the int conversion? Android canvas has a lot of float prams.
c
Sorry I slightly mispoke. I was assuming what Alex Vanyo said is that .dp are fundamentally converted to int or floating point since they have to be actual pixel values.
y
Yeah, I think it depends. For example for text you can use hinting to control it. https://halilibo.com/2024/why-text-gets-jittery-when-scaled-on-android.html
a
Layout calculations all get done in integer pixels, so any dp value gets rounded. I think the counter intuitive thing is that working in integer dps doesn't mean you're working in integer pixels - so in that way
1.66dp
vs
2.dp
could both could end up being a non-integer number of pixels that would need to get rounded for layout purposes
❤️ 1