@ NandanKumarSingh's answer https://stackoverflow.com/a/16626832/116278 works, but I made some changes in the fab code (not in xml, because they will be overwritten in class methods)
fab.setTextBitmap("ANDROID", 100f, Color.WHITE) fab.scaleType = ImageView.ScaleType.CENTER fab.adjustViewBounds = false
Where setTextBitmap is an extension for the ImageView class with similar functionality, but it supports multi-channel text
fun ImageView.setTextBitmap(text: String, textSize: Float, textColor: Int) { val paint = Paint(Paint.ANTI_ALIAS_FLAG) paint.textSize = textSize paint.color = textColor paint.textAlign = Paint.Align.LEFT val lines = text.split("\n") var maxWidth = 0 for (line in lines) { val width = paint.measureText(line).toInt() if (width > maxWidth) { maxWidth = width } } val height = paint.descent() - paint.ascent() val bitmap = Bitmap.createBitmap(maxWidth, height.toInt() * lines.size, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) var y = - paint.ascent() for (line in lines) { canvas.drawText(line, 0f, y, paint) y += height } setImageBitmap(bitmap) }
V. Kalyuzhnyu Oct 26 '18 at 6:12 2018-10-26 06:12
source share