java - 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:
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?
解决方案
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
推荐阅读
- google-api - 如何在 CURLRequest CI4 上为 Google API 设置授权承载?
- rust - 如何关闭货物文档测试并为 Rust 中的特定模块编译
- powershell - Visual Studio 代码错误“/bin/sh: 1: powershell: not found”
- javascript - 错误类型错误:.filter 不是函数
- android - 在 Android 的底部均匀对齐所有屏幕尺寸的按钮
- c++ - 未定义模板错误的隐式实例化
- python - 如何在 python gdal 中按经度和纬度切割 dem?
- javascript - 用户单击“Enter”按钮时创建的列表项未划掉
- javascript - 下拉样式在反应类中不起作用
- c# - 单击按钮后将 DatagridRow 移动到最后一行