1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| /**
* 注释:从不可见View中创建Bitmap
* 时间:2024/10/17 0017 16:01
* 作者:郭翰林
*/
fun createBitmapFromInvisibleView(v: View, dpWidth: Int, dpHeight: Int): Bitmap? {
//测量使得view指定大小
val measuredWidth =
View.MeasureSpec.makeMeasureSpec(
dpToPixels(v.context, dpWidth),
View.MeasureSpec.EXACTLY
)
val measuredHeight =
View.MeasureSpec.makeMeasureSpec(
dpToPixels(v.context, dpHeight),
View.MeasureSpec.EXACTLY
)
v.measure(measuredWidth, measuredHeight)
//调用layout方法布局后,可以得到view的尺寸大小
v.layout(0, 0, v.measuredWidth, v.measuredHeight)
val bmp = Bitmap.createBitmap(v.width, v.height, Bitmap.Config.ARGB_8888)
val c = Canvas(bmp)
c.drawColor(Color.WHITE)
v.draw(c)
return bmp
}
|