两个bitmap对象拼成一个返回的两种实现方式。记录

1.XML布局文件,拼接。 再将xml转换成bitmap返回

/**
 * 生成分享自定义主题的bitmap图片
 * content 对应一个自定义主题bitmap的路径
 */
fun createShareBitmap(context: Context, screenShot: String): Bitmap {
    val themeBitmap = BitmapFactory.decodeFile(screenShot)
    val inflater = LayoutInflater.from(context)
    val inviteView = inflater.inflate(R.layout.custom_share_item, null)
    val img = inviteView.findViewById(R.id.custom_theme_img) as ImageView
    val  draw = BitmapDrawable( themeBitmap)
    img.image = draw
    inviteView.isDrawingCacheEnabled = true
    inviteView.measure(
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
    inviteView.layout(0, 0,
            inviteView.measuredWidth,
            inviteView.measuredHeight)

    inviteView.buildDrawingCache()
    val cacheBitmap = inviteView.drawingCache
    return Bitmap.createBitmap(cacheBitmap)
}

使用:

//                val bitmap = createShareBitmap(this@CustomThemeSavedActivity,screenShot)//返回bitmap
//                val bitmap =  BitmapDrawable( bitmap)//bitmap转drawable

  2. 画布操作

    /**
     * frameBitmap 外部框图片
     * themeBitmap 自定义主题图片 (由一个路径而来)
     */
    fun mergeShareBitmap(frameBitmap :Bitmap,themeBitmap :Bitmap) :Bitmap{
        //以其中一张图片的大小作为画布的大小,或者也可以自己自定义
        val bitmap = Bitmap.createBitmap(frameBitmap.width, frameBitmap
                .height, frameBitmap.config)
        //生成画布
        val canvas = Canvas(bitmap)
        //传入的themeBitmap的大小是不固定的,所以我要将传来的themeBitmap调整
//        val w = (frameBitmap.width)*0.65 //内部图片的宽为百分之65
//        val h = (frameBitmap.height )*0.45 //内部图片的高为百分之45
        val w = frameBitmap.width
        val h = frameBitmap.height
        val m = Matrix()
        //确定secondBitmap大小比例
        m.setScale((w / themeBitmap.width).toFloat(), (h / themeBitmap.height).toFloat())
        val paint = Paint()
        paint.alpha = 150
        canvas.drawBitmap(frameBitmap, 0F, 0F, null)
        canvas.drawBitmap(themeBitmap, m, paint)
        return bitmap
    }

猜你喜欢

转载自blog.csdn.net/qq_38679144/article/details/82420685