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

Posted 汤米粥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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?)
    }
}

以上是关于SaveBackgroundViewToImageUtil把后台的布局保存成图片的主要内容,如果未能解决你的问题,请参考以下文章