我创建了一个自定义视图,以显示值的进度,即假期前几天。当它显示在屏幕上时,这完美呈现,但是当我尝试以编程方式拍摄该屏幕的屏幕快照时,自定义视图将不再呈现。
这是在屏幕上显示时的外观: https://i.stack.imgur.com/3xVbE.png
这是我截屏时的样子: https://i.stack.imgur.com/aMSfs.png
要获取屏幕截图,我将屏幕转换为位图,然后将其保存到手机的缓存中。我尝试了几种转换为位图的方法:
`val view = constraintLayout
view.isDrawingCacheEnabled = true
view.buildDrawingCache()
val bitmap = view.drawingCache`
`val view = constraintLayout
val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
view.draw(canvas)`
`val view = this.window.decorView
view.isDrawingCacheEnabled = true
view.buildDrawingCache()
val bitmap = view.drawingCache
val frame = Rect()
this.window.decorView.getWindowVisibleDisplayFrame(frame)
val statusBarHeight = frame.top
val width = this.windowManager.defaultDisplay.width
val height = this.windowManager.defaultDisplay.height
val bitmapScreenshot = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width, height - statusBarHeight)
view.destroyDrawingCache()`
这就是我保存位图的方式:
val cachePath = File(this.cacheDir, "images")
cachePath.mkdirs() // don't forget to make the directory
val stream = FileOutputStream(cachePath.path + "/image.png") // overwrites this image every time
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
stream.close()
我已经看过这个问题mimic screenshot functionality question,如果有人知道答案,那将非常有帮助,因为当我使用硬件按钮进行屏幕截图时,自定义视图可以正确显示。
所以我的问题是,为什么截取屏幕截图时不能显示我的自定义视图,我该如何纠正? 提前致谢!