Android将不显示的layout保存成图片,不需要setDrawingCacheEnabled

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
 


object SaveBackgroundViewToImageUtil {

    private var mBitmapDoneListener: BitmapDoneListener? = null

    /**
     * view转bitmap
     *
     * @param v View
     * @return Bitmap
     */
    private fun viewConversionBitmap(v: View): Bitmap {
        val w = v.width
        val h = v.height
        Log.e("SaveImage", "width: $w height: $h")
        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
    }

    fun createBitmap3(activity: Activity, listener: BitmapDoneListener): Bitmap? {

        mBitmapDoneListener = listener
        //将布局转化成view对象
        val v: View = LayoutInflater.from(activity).inflate(R.layout.layout_save_image_by_h5, null, false)

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

        //测量使得view指定大小
        val measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY)
        val measuredHeight = View.MeasureSpec.makeMeasureSpec(height, 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)

        if (mBitmapDoneListener != null) {
            mBitmapDoneListener?.bitmapDone(bmp);
        }
        return bmp
    }


    /**
     * 计算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?)
    }
}

参考:https://www.cnblogs.com/taixiang/p/9575195.html

网上大都说先layout后,再setDrawingCacheEnabled,可我那样实现保存的图片为空白。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值