1. 转化成 Bitmap
fun View.toBitmap(
backgroundColor:Int? = null,
isIncludePadding:Boolean = true
):Bitmap?{
val w= width - if (isIncludePadding) 0 else (paddingLeft + paddingRight)
val h= height - if (isIncludePadding) 0 else (paddingTop + paddingBottom)
if (w<= 0 || h<= 0) {
return null
}
val bmp= Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
val c= Canvas(bmp)
/** 如果不设置canvas画布为白色,则生成透明*/
if (backgroundColor != null) {
c.drawColor(backgroundColor)
}
val l= if (isIncludePadding) 0 else paddingLeft
val t= if (isIncludePadding) 0 else paddingTop
layout(l, t, w+ l, h+ t)
draw(c)
return bmp
}
网友评论