1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

How to create Bitmap form inflating FrameLayout (.. or other layout)

Last updated at Posted at 2020-10-21

I got into a bit of trouble when I tried to create a dynamic layout and generate a Bitmap without displaying it on the screen, so I'll leave this as a note.


    fun createLayoutBitmap() {
        val frameLayout = createMeasuredFrameLayout()
        val bitmap = createBitmap(frameLayout)
        // save bitmap or set Views ...
    }

    fun createMeasuredFrameLayout(): FrameLayout {
        // inflate or new your layout
        return FrameLayout(context).apply {
            setBackgroundColor(Color.YELLOW)
            val tv = TextView(context).apply {
                text = "test"
            }
            // apply pixel size to TextView
            addView(tv, Rect(0, 0, 100, 200).toLayoutParams())
            // important: you need call measure() after build layout. do not add after measure(). my mistaken  point.
            measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
    }

    fun createBitmap(view: View): Bitmap {
        // apply drawing bitmap size in pixel.
        val rect = Rect(0, 0, 500, 500)
        return Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888).also {
            view.layout(rect.left, rect.top, rect.width(), rect.height())
            view.draw(Canvas(it))
        }
    }
    // rect is intuitive.
    fun Rect.toLayoutParams(): ViewGroup.LayoutParams {
        return FrameLayout.LayoutParams(width(), height()).apply {
            leftMargin = left
            topMargin = top
        }
    }

this is a very easy sample code.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?