I'm working on an android library to display an image as a fixed background image. To do this, I'm dynamically ajusting the position of the image every 10ms based on the locationOnScreen. I understand that it's an aweful solution, but I'm here to improve this :)
The issue with this is that there is a glitch when the parent scrollable view is scrolling too fast and the image jump while the other view are moving. (click on the gif for the full demo)
As it's a library, I don't want to add complexity when integrating the lib, meaning no scroll listener, theme or no window override etc.
Solution tried:
- changing the loop delay
- using window background is not possible for a library
- no access to activity theme or similar
handler
override fun run() {
fixedBackgroundImageLayout.getLocationOnScreen(locationOnScreen)
fixedBackgroundImagePlugin.update(locationOnScreen)
handler.postDelayed(this, 10)
}
FixedBackgroundImagePlugin#update
override fun update(locationOnScreen: IntArray) {
if (backgroundImageFrameLayout == null) {
return
}
yPosition = parentLocationOnScreen[1] - locationOnScreen[1]
backgroundImageFrameLayout.top = 2 * yPosition - lastYPosition - 10
lastYPosition = yPosition
}
the backgroundImageFrameLayout has the image as a background image.
I've also setup a sample repository to help you dig in if wanted.
I'm open to any advice/lead