package com.xty.common.util

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.view.View
import com.hjq.toast.ToastUtils
import java.io.FileOutputStream

object  ViewScreenshotUtils {

     fun viewSaveToImage(view: View,context: Context) {
        view.isDrawingCacheEnabled = true
        view.drawingCacheQuality = View.DRAWING_CACHE_QUALITY_HIGH
        view.drawingCacheBackgroundColor = Color.WHITE
        // 把一个View转换成图片
        val cachebmp = loadBitmapFromView(view)
        val fos: FileOutputStream
        var imagePath = ""
        try {
            /*var  savePath =
                FileUtils.getCacheFile(context) + File.separator + System.currentTimeMillis() + ".jpg"
            PictureUtils.saveBitmapToFile(savePath, cachebmp)*/
            SaveUtils.saveBitmapToAlbum(context,cachebmp)
            ToastUtils.show("保存成功")

        } catch (e: Exception) {
            e.printStackTrace()
        }
        view.destroyDrawingCache()
    }

     fun loadBitmapFromView(v: View): Bitmap {
        val w = v.width
        val h = v.height
        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
    }
}