Android将不显示的layout保存成图片,不需要setDrawingCacheEnabled
Posted 汤米粥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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,保存的图片为空白。
以上是关于Android将不显示的layout保存成图片,不需要setDrawingCacheEnabled的主要内容,如果未能解决你的问题,请参考以下文章
android 存在数据库中的动态图片,如何读取出来,显示在ImageView中
安卓ImageView的src路径drawable在哪?图片显示不出来