Hey, Does anyone know a faster and more efficient...
# random
t
Hey, Does anyone know a faster and more efficient way (might be a library that's out there) to draw a grid in a custom view than using
drawLine()
? I have a pixel art editor (made with
Bitmap
and written 100% in Kotlin) and I want to give the user the option to enable grid lines, but using
drawLine()
is not performant at all, and I've been trying to find a better alternative for some time now. Not to mention it's also verbose and for larger canvases it is not performant. I've spoken to a couple of people who have created a pixel art editor for Windows and they say that most platforms have a custom grid-drawing implementation for
Bitmap
which is lightweight and easy to use, but I so far haven't found one in Android. I also haven't found any library on GitHub which assists in this. I understand this is a niche question - since most people haven't created a pixel art editor/have experience in this space - but if anyone has any info/leads please let me know; it's mostly an app for a school project (and will be on the Play Store in the future), and I want it to function perfectly 😄 Cheers 🙏🏻, thebluepandabear
d
Most efficient would probably be a surface shader, that takes a period and X, Y offsets to make pixels compute 'whether they're a gridline or not'. This implies a bit of a learning curve integrating shaders to Compose but I think some examples exist.
A CPU based way that might involve less learning and sits between
drawLine
and a shader for efficiency; might be to do the same computation while initialising a pixel data array, then set this to a background image in your workspace.
Not at a desktop machine to check API's right now but hopefully those two general ideas spark something 👌
👍🏻 1
t
Thank you man (I'm not using Compose btw)
d
Ah yeah, my bad... concepts can still apply to Android View.
Do you need to zoom the grid, if so, how smoothly?
t
The grid should only zoom if the user taps the 'Zoom in' button
and the grid should disappear if it's zoomed out to a point where it's hardly visible - if you know what I mean?
Update: Rohit Singh's answer to https://stackoverflow.com/questions/24842550/2d-array-grid-on-drawing-canvas/53578910 seems to be working fine