简体   繁体   中英

Canvas crop image Android

I use canvas to create a circle. Now I want to export that circle to a png file, removing everything other than the circle and taking what is in the circle. Note I have 2 frames on top of each other.

例子

You can do that using Android PorterDuffMode Here is the code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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