美文网首页
View 的一些扩展方法

View 的一些扩展方法

作者: 案玉璃青 | 来源:发表于2020-03-26 11:07 被阅读0次

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

}

相关文章

网友评论

      本文标题:View 的一些扩展方法

      本文链接:https://www.haomeiwen.com/subject/tjgtbctx.html