I would like to draw stroke around bitmap. getting left and right side point of bitmap and drawing line using path and points. got border around bitmap using below code and set it below imageview. but when i apply padding it want draw border with appropriate padding.
private suspend fun drawBorder(
src: Bitmap,
colorToReplace: Int,
): Bitmap? {
return kotlin.runCatching {
withContext(IO) {
val width = src.width
val height = src.height
val pixels = IntArray(width * height)
// get pixel array from source
src.getPixels(pixels, 0, width, 0, 0, width , height )
val bmOut = Bitmap.createBitmap(width, height, src.config)
val canvas = Canvas(bmOut)
val paint = Paint().apply {
isAntiAlias = true
isDither = true
color = colorCode
style = Paint.Style.STROKE
strokeWidth = radius
strokeJoin = Paint.Join.ROUND
strokeCap = Paint.Cap.ROUND
}
if (isDashed) {
paint.pathEffect = DashPathEffect(floatArrayOf(50f, radius * 2), 0f)
}
var moved = false
val path = Path()
var pixel = 0
// iteration through pixels
for (y in height - 1 downTo 0) {
for (x in width - 1 downTo 0) {
val index = y * width + x
pixel = pixels[index]
if (pixel != colorToReplace) {
if (!moved) {
moved = true
path.moveTo(x.toFloat(), y.toFloat())
}
path.lineTo(if(isPadded) x.toFloat() + 20 else x.toFloat(), y.toFloat())
break
}
} //x
} //y
for (y in 0 until height) {
for (x in 0 until width) {
val index = y * width + x
pixel = pixels[index]
if (pixel != colorToReplace) {
path.lineTo(if(isPadded) x.toFloat() - 20 else x.toFloat(), if(isPadded) y.toFloat() - 20 else y.toFloat())
break
}
} //x
} //y
canvas.drawPath(path, paint)
bmOut
}
}.getOrNull()
}
Now i would like to draw dashed with padding like below image.
If anyone have idea please let me know. Thanks.
