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)