Using a bitmap inside a new Android Project

Hello Everyone.
I know its kinda of a newbie question but i’m having a few difficulties around here.

I’m tying to draw a bitmap inside a view and write a text on it.
Right now, my code goes as follows:

package com.example.desenhanaview

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    fun View.fromBitmap(): Bitmap {
        val spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        this.measure(spec, spec)
        this.layout(0, 0, this.measuredWidth, this.measuredHeight)
        val b = Bitmap.createBitmap(this.measuredWidth, this.measuredHeight,
            Bitmap.Config.ARGB_8888)
        val mPaintSquare: Paint
        mPaintSquare = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaintSquare.setColor(Color.RED)
        mPaintSquare.textSize = 40f
        val c = Canvas(b)
        c.drawText("TextoTeste",10f,45f,mPaintSquare);
        c.translate((-this.scrollX).toFloat(), (-this.scrollY).toFloat())
        this.draw(c)
        return b
    }
}

}

However, when previewing i see nothing but the default Hello World on the app screen.
Is this Bitmap supposed to be inside XML layout file also ?

If so, how do i declare it ?

Any input is appreciated.

Thanks (and again sorry for the newbie question)

Not really related to kotlin at all but here’s some Android resources on a custom view in xml and custom drawing:
Creating a View Class
Custom Drawing

Well, you’re not calling the fromBitmap function anywhere, so it’s never going to be run.

And after creating the bitmap, you need to do something with it for it to show up on the screen.

I’m not sure what’s in your layout, since you didn’t post the code from activity_main.xml but the first thing you would need to do to show a Bitmap on the screen is to have a view in your layout that can display it.

For that, you’d want to put an <ImageView> in the layout, and then call setImageBitmap on it to set the bitmap after you create it.

However, even then you’ll run into some problems…

Measuring a view in onCreate is always going to give you a size of zero because the view hasn’t been laid out yet. You can get around this by using ViewTreeObserver to listen for layout to finish, and THEN create and set the bitmap…

However, if you explain what you’re trying to accomplish, there may be less complicated ways to do it. I suspect that probably a custom View subclass where you override the onDraw method might accomplish the same thing without having to worry about layout timing or measurement.

Or you might be able to construct what you want just using layout directly.

One of the drawbacks of working with Bitmap objects on Android is that they are rendered using the CPU, then uploaded to the GPU for display. This makes them quite slow if you need to recreate them during an animation or in response to (say) the user rotating the screen. They also eat up memory (and on today’s high resolution screens, that can be a lot of memory). If you can build what you want just in the layout XML, or in onDraw in a custom view, it will be rendered using the GPU, which means a smaller memory footprint (in most cases) and better performance.

Hope that’s helpful!

None of this is really Kotlin-related though…