Last active
March 27, 2019 09:32
-
-
Save shiveshmehta09/c130de21f40656ed1eb3ed78f93246a4 to your computer and use it in GitHub Desktop.
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
// for api level 28 | |
fun getScreenShotFromView(view: View, activity: Activity, callback: (Bitmap) -> Unit) { | |
activity.window?.let { window -> | |
val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888) | |
val locationOfViewInWindow = IntArray(2) | |
view.getLocationInWindow(locationOfViewInWindow) | |
try { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
PixelCopy.request( | |
window, | |
Rect( | |
locationOfViewInWindow[0], | |
locationOfViewInWindow[1], | |
locationOfViewInWindow[0] + view.width, | |
locationOfViewInWindow[1] + view.height | |
), bitmap, { copyResult -> | |
if (copyResult == PixelCopy.SUCCESS) { | |
callback(bitmap) } | |
else { | |
} | |
// possible to handle other result codes ... | |
}, | |
Handler() | |
) | |
} | |
} catch (e: IllegalArgumentException) { | |
// PixelCopy may throw IllegalArgumentException, make sure to handle it | |
e.printStackTrace() | |
} | |
} | |
} | |
//deprecated version | |
/* Method which will return Bitmap after taking screenshot. We have to pass the view which we want to take screenshot. */ | |
fun getScreenShot(view: View): Bitmap { | |
val screenView = view.rootView | |
screenView.isDrawingCacheEnabled = true | |
val bitmap = Bitmap.createBitmap(screenView.drawingCache) | |
screenView.isDrawingCacheEnabled = false | |
return bitmap | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment