Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CropCircleWithBorderTransformation #12

Open
WeiLianYang opened this issue Sep 25, 2023 · 1 comment
Open

CropCircleWithBorderTransformation #12

WeiLianYang opened this issue Sep 25, 2023 · 1 comment

Comments

@WeiLianYang
Copy link

WeiLianYang commented Sep 25, 2023

CropCircleWithBorderTransformation when calculating the border there is a bug.
If the width and height of the picture are not equal, then the width of the outer border is set to be wider than the actual value.

bugs

Code :

        val url = "https://pic.52112.com/JPG-180507/180507_295/hILItGsZAG_small.jpg"
        viewBinding.ivImage.load(url) {
            transformations(CropCircleWithBorderTransformation(10.dp, "#00ff00".toColorInt()))
        }
@WeiLianYang
Copy link
Author

fix:

class CropCircleWithBorder constructor(
    private val borderSize: Int,
    private val borderColor: Int
) : Transformer() {

    override fun transform(
        source: Bitmap,
        destination: Bitmap
    ): Bitmap {

        CropCircle().transform(source, destination)

        destination.density = source.density
        destination.setHasAlpha(true)

        val minSize = minOf(source.width, source.height)
        val maxSize = maxOf(source.width, source.height)
        val strokeSize = borderSize * 1f * minSize / maxSize
        val radius = minSize / 2f

        val paint = Paint().apply {
            color = borderColor
            style = Paint.Style.STROKE
            strokeWidth = strokeSize
            isAntiAlias = true
            isFilterBitmap = true
        }
        val canvas = Canvas(destination)
        canvas.drawCircle(radius, radius, radius - strokeSize / 2f, paint)
        canvas.setBitmap(null)

        return destination
    }

    override fun key(): String = "$id(borderSize=$borderSize, borderColor=$borderColor)"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant