How to draw dashed border with padding around bitmap android?

Viewed 138

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.

required image

If anyone have idea please let me know. Thanks.

1 Answers

this is a function I used for the same, it could help you

private Bitmap addWhiteBorder(Bitmap bmp, int borderSize) {
Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2, bmp.getHeight() + borderSize * 2, bmp.getConfig());
Canvas canvas = new Canvas(bmpWithBorder);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bmp, borderSize, borderSize, null);
return bmpWithBorder;
}

Basically, it creates a new Bitmap adding 2 * border size to each dimension, and then paints the original Bitmap over it, offsetting it with border size.

You make your bitmap bigger than the one you are adding to it and then fill the canvas with the background you want. If you need to add other effects you can look into the canvas options for clipping the rect and adding rounded corners and such.

Related