首页 > 解决方案 > How to remove white border/background from Bitmap in Kotlin?

问题描述

i have this function to draw text on image so the user can share it it depends on the content

public fun drawTextToBitmap_soul(
context: Context,
gResId: Int,
textSize: Int = 78,
text1: String,
text2: String,
text3: String,
text4: String
): Bitmap {
val resources = context.resources
val scale = resources.displayMetrics.density
var bitmap = BitmapFactory.decodeResource(resources, gResId)

var bitmapConfig = bitmap.config;

if (bitmapConfig == null) {
    bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888
}

bitmap = bitmap.copy(bitmapConfig, true)

val canvas = Canvas(bitmap)

val paint = Paint(Paint.ANTI_ALIAS_FLAG)
paint.color = Color.rgb(255, 255, 255)

paint.textSize = (textSize * scale).roundToInt().toFloat()

val fontFace = ResourcesCompat.getFont(context, R.font.poppins)
paint.typeface = Typeface.create(fontFace, Typeface.NORMAL)

paint.setShadowLayer(1f, 0f, 1f, Color.WHITE)

val bounds = Rect()

paint.getTextBounds(text1, 0, text1.length, bounds)
paint.textAlign = Paint.Align.LEFT
var x = (bitmap.width - bounds.width()) / 20.0
var y = (bitmap.height + bounds.height()) / 4.0
canvas.drawText(text1, x.toFloat(), y.toFloat(), paint)



val paints = TextPaint(Paint.ANTI_ALIAS_FLAG)

paints.color = Color.rgb(255, 255, 255)

paints.textSize = (textSize * scale).roundToInt().toFloat()

paints.setShadowLayer(1f, 0f, 1f, Color.WHITE)


val textWidth = canvas.width - (46 * scale).toInt()


val textLayout = StaticLayout(
    text2, paints, textWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false
)

val textLayout2 = StaticLayout(
    text3, paints, textWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false
)

val textLayout3 = StaticLayout(
    text4, paints, textWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false
)


var textHeight = textLayout.height


x = ((bitmap.width - textWidth) / 2).toFloat().toDouble()
y = ((bitmap.height - textHeight) / 2.5).toFloat().toInt().toDouble()


canvas.save()
canvas.translate(x.toFloat(), y.toFloat())
textLayout.draw(canvas)

textHeight = textLayout2.height
x = ((bitmap.width - textWidth) / 2).toFloat().toDouble()
y = ((bitmap.height - textHeight) / 3.5).toFloat().toInt().toDouble()
canvas.translate(x.toFloat(), y.toFloat())
textLayout2.draw(canvas)
textHeight = textLayout3.height
x = ((bitmap.width - textWidth) / 2).toFloat().toDouble()
y = ((bitmap.height - textHeight) / 5).toFloat().toInt().toDouble()
canvas.translate(x.toFloat(), y.toFloat())
textLayout3.draw(canvas)
canvas.restore()

return bitmap
}

and this is the results:

enter image description here

how can i remove the white border/background around the image?

i tried doing it from the xml but it didn't work and it didn't affect anything, is it from the function? or do i need to add anything to the function?

标签: javaandroidkotlinbitmapborder

解决方案


The white border/background was from the dialog itself not the bitmap

dialog!!.window?.setBackgroundDrawableResource(R.drawable.share_mysoul);

so i had to change it from here in the dialog


推荐阅读