SaveBackgroundViewToImageUtil把后台的布局保存成图片

package com.maka.app.util

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.maka.app.R
import com.maka.app.util.system.ScreenUtil


object SaveBackgroundViewToImageUtil {

    private var mBitmapDoneListener: BitmapDoneListener? = null

    /**
     * 计算view的大小
     */
    fun onSave(url: String?, title: String, activity: Activity, listener: BitmapDoneListener) {
        this.mBitmapDoneListener = listener
        //将布局转化成view对象
        val viewBitmap: View = LayoutInflater.from(activity).inflate(R.layout.layout_save_image_by_h5, null, false)

        val width = ScreenUtil.getWidthPixels()
        val height = ScreenUtil.getHeightPixels()

        //然后View和其内部的子View都具有了实际大小,也就是完成了布局,相当与添加到了界面上。接着就可以创建位图并在上面绘制了:
        layoutView(viewBitmap, width, height, url, title, activity)
    }

    /**
     * 填充布局内容
     */
    fun layoutView(viewBitmap: View, width: Int, height: Int, url: String?, title: String?, context: Context?) {
        // 整个View的大小 参数是左上角 和右下角的坐标
        val measuredWidth: Int = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY)
        val measuredHeight: Int = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
        viewBitmap.measure(measuredWidth, measuredHeight)
        viewBitmap.layout(0, 0, viewBitmap.getMeasuredWidth(), viewBitmap.getMeasuredHeight())

        val tv: TextView = viewBitmap.findViewById(R.id.tv_title)
        tv.setText(title)
        val imageView: ImageView = viewBitmap.findViewById(R.id.iv_qrCode)

        //注意加载网络图片时一定要用SimpleTarget回调
        Glide.with(context!!).asBitmap().load(url).into(object : SimpleTarget<Bitmap?>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap?>?) {
                imageView.setImageBitmap(resource)
                viewSaveToImage(viewBitmap)
            }
        })
    }

    /**
     * 把view转成图片
     *
     * @param view
     */
    private fun viewSaveToImage(view: View): Bitmap? {
        // 把一个View转换成图片
        var cachebmp = viewToBitmap(view);
        if (mBitmapDoneListener != null) {
            mBitmapDoneListener?.bitmapDone(cachebmp);
        }
        return cachebmp;
    }


    /**
     * view转bitmap
     */
    private fun viewToBitmap(v: View): Bitmap? {
        val w = v.width
        val h = v.height
        if (w <= 0 || h <= 0) {
            Log.e("xx", "viewToBitmap")
            return null;
        }

        val bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
        val c = Canvas(bmp)
        c.drawColor(Color.WHITE)
        /** 如果不设置canvas画布为白色,则生成透明  */
        v.layout(0, 0, w, h)
        v.draw(c)
        return bmp
    }

    interface BitmapDoneListener {
        fun bitmapDone(bitmap: Bitmap?)
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值