Created
October 7, 2023 12:11
-
-
Save ruthwikkk/e9d9cab3b1b131a6997ca7c32fea59d2 to your computer and use it in GitHub Desktop.
Code for finding relative position and translate the draw pointer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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