Skip to content

Instantly share code, notes, and snippets.

@ruthwikkk
Created October 7, 2023 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruthwikkk/e9d9cab3b1b131a6997ca7c32fea59d2 to your computer and use it in GitHub Desktop.
Save ruthwikkk/e9d9cab3b1b131a6997ca7c32fea59d2 to your computer and use it in GitHub Desktop.
Code for finding relative position and translate the draw pointer
fun getBackgroundAndDrawBehind(parent: ViewGroup) {
//Arrays to store the co-ordinates
val rootLocation = IntArray(2)
val viewLocation = IntArray(2)
parent.getLocationOnScreen(rootLocation) //get the parent co-ordinates
this.getLocationOnScreen(viewLocation) //get view co-ordinates
//Calculate relative co-ordinates
val left: Int = viewLocation[0] - rootLocation[0]
val top: Int = viewLocation[1] - rootLocation[1]
canvas?.save()
canvas?.translate(-left.toFloat(), -top.toFloat()) //translates the initial position
canvas?.let {
parent.draw(it)
}
canvas?.restore()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment