package com.base.locationsharewhite.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.LayoutInflater
import android.view.View
import com.google.android.gms.maps.model.BitmapDescriptor
import com.google.android.gms.maps.model.BitmapDescriptorFactory


object BitmapUtils {

    private fun createBitmapFromView(view: View): Bitmap {
        view.measure(
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        )
        view.layout(0, 0, view.measuredWidth, view.measuredHeight)

        // 创建一个和布局大小一样的 Bitmap
        val bitmap = Bitmap.createBitmap(view.measuredWidth, view.measuredHeight, Bitmap.Config.ARGB_8888)
        // 使用 Canvas 将布局绘制到 Bitmap 上
        val canvas = Canvas(bitmap)
        view.draw(canvas)
        return bitmap
    }

    // 方法：将 XML 布局资源 ID 转换为 BitmapDescriptor
    fun getBitmapDescriptorFromLayoutResId(view: View): BitmapDescriptor {
        val bitmap = createBitmapFromView(view)
        return BitmapDescriptorFactory.fromBitmap(bitmap)
    }
}