RedMadRobot/gears-android

core-ktx: Canvas.withClipOut

Closed this issue · 0 comments

Draft:

import android.graphics.Canvas
import android.graphics.Path
import android.graphics.Region
import android.os.Build

/**
 * Wrap the specified [block] in calls to [Canvas.save]/[Canvas.clipOutPath]
 * and [Canvas.restoreToCount].
 */
public inline fun Canvas.withClipOut(
    clipPath: Path,
    block: Canvas.() -> Unit,
) {

    val checkpoint = save()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        clipOutPath(clipPath)
    } else {
        @Suppress("DEPRECATION")
        clipPath(clipPath, Region.Op.XOR)
    }

    try {
        block()
    } finally {
        restoreToCount(checkpoint)
    }
}

Also the same method for Rect and RectF

Originally posted by @osipxd in #18 (comment)