IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android将不显示的layout保存成图片,不需要setDrawingCacheEnabled -> 正文阅读

[移动开发]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,保存的图片为空白。

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-08-06 09:57:05  更:2021-08-06 09:57:44 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 13:19:59-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码