简体   繁体   English

画布裁剪图像 Android

[英]Canvas crop image Android

I use canvas to create a circle.我使用canvas来创建一个圆圈。 Now I want to export that circle to a png file, removing everything other than the circle and taking what is in the circle.现在我想将该圆圈导出到一个 png 文件,删除圆圈以外的所有内容并获取圆圈中的内容。 Note I have 2 frames on top of each other.注意我有 2 个框架在彼此之上。

例子

You can do that using Android PorterDuffMode Here is the code您可以使用 Android PorterDuffMode 来做到这一点 这是代码

private fun crop(bitmapImage: Bitmap): Bitmap {
    val bitmap = Bitmap.createBitmap(
            bitmapImage.width,
            bitmapImage.height,
            Bitmap.Config.ARGB_8888
    )
    val canvas = Canvas(bitmap)
    val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    canvas.drawCircle(100.0f, 100.0f, 50.0f, paint)
    paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)
    canvas.drawBitmap(bitmapImage, 0.0f, 0.0f, paint)
    return bitmap
}

you can change the circle pivot and radius to achieve your result, If you pass the bitmap of the image, it will return the bitmap and you can save to file您可以更改圆的枢轴和半径以实现您的结果,如果您传递图像的位图,它将返回位图,您可以保存到文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM