将 ListView 转换为位图

问题描述 投票:0回答:1

我想将 ListView 转换为位图图像。 以下代码工作正常,除非列表比屏幕长。我设法使画布达到所需的大小,但它仍然只显示适合屏幕的元素,其他区域只是白色:

fun getBitmapFromView(view: ListView): Bitmap {
    val listAdapter = view.adapter
    var totalHeight = listView.paddingTop + listView.paddingBottom
    val desiredWidth = MeasureSpec.makeMeasureSpec(listView.width, MeasureSpec.AT_MOST)
    for (i in 0 until listAdapter.getCount()) {
        val listItem: View = listAdapter.getView(i, null, listView)
        if (listItem != null) {
            listItem.layoutParams =
                RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
                )
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED)
            totalHeight += listItem.measuredHeight
        }
    }

    val bitmap  = Bitmap.createBitmap(view.width, totalHeight, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(bitmap )
    val bgDrawable = view.background
    if (bgDrawable != null) bgDrawable.draw(canvas) else canvas.drawColor(Color.WHITE)
    view.draw(canvas)
    return bitmap
}

如何修复它以包含整个列表?

android kotlin listview bitmap
1个回答
0
投票

我设法通过调整 Kotlin 中 ListView 的 Take a Screenshot of RecyclerView in FULL length 中的代码来使其工作。感谢您的帮助。

fun getBitmapFromView(listView: ListView): Bitmap? {
    val adapter: ListAdapter = listView.adapter
    var bigBitmap: Bitmap? = null
    if (adapter != null) {
        val size = adapter.count
        var height = 0
        val paint = Paint()
        var iHeight = 0
        val maxMemory = (Runtime.getRuntime().maxMemory() / 1024).toInt()

        // Use 1/8th of the available memory for this memory cache.
        val cacheSize = maxMemory / 8
        val bitmapCache = LruCache<String, Bitmap>(cacheSize)

        for (i in 0 until size) {
            val itemView = adapter.getView(i, null, listView) as View
            itemView.measure(
                View.MeasureSpec.makeMeasureSpec(listView.width, View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
            )
            itemView.layout(0, 0, itemView.measuredWidth, itemView.measuredHeight)
            itemView.isDrawingCacheEnabled = true
            itemView.buildDrawingCache()
            val drawingCache = itemView.drawingCache

            if (drawingCache != null) {
                bitmapCache.put(i.toString(), drawingCache)
            }
            height += itemView.measuredHeight
        }

        bigBitmap = Bitmap.createBitmap(listView.width, height, Bitmap.Config.ARGB_8888)
        val bigCanvas = Canvas(bigBitmap)
        bigCanvas.drawColor(Color.WHITE)

        for (i in 0 until size) {
            val bitmap = bitmapCache.get(i.toString())
            bigCanvas.drawBitmap(bitmap, 0f, iHeight.toFloat(), paint)
            iHeight += bitmap?.height ?: 0
            bitmap?.recycle()
        }
    }
    return bigBitmap
}
© www.soinside.com 2019 - 2024. All rights reserved.