Home > other >  Unable to find minus() of Path in Jetpack Compose
Unable to find minus() of Path in Jetpack Compose

Time:08-19

I am trying to use this minus extension function of Path in Jetpack Compose code.

But I am getting an unresolved reference error.

Code

@Composable
fun PathDiff() {
    Canvas(
        modifier = Modifier
            .fillMaxSize(),
    ) {
        val rect1 = Path().also {
            it.addRect(Rect(Offset.Zero, Offset(100.0F, 100.0F)))
            it
        }
        val rect2 = Path().also {
            it.addRect(Rect(Offset(50.0F, 50.0F), Offset(150.0F, 150.0F)))
            it
        }

        drawPath(
            path = rect1 - rec2,
            color = Black,
            alpha = 1F,
        )
    }
}

Dependency version

jetpackComposeVersion = "1.3.0-alpha02"
implementation "androidx.compose.ui:ui:$rootProject.jetpackComposeVersion"

Error

e: /Users/abhimanyu/Documents/projects/stackoverflow-answers/codebase/android/app/src/main/java/com/makeappssimple/abhimanyu/stackoverflowanswers/android/MainActivity.kt: (375, 26): Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public inline operator fun BigDecimal.minus(other: BigDecimal): BigDecimal defined in kotlin
public inline operator fun BigInteger.minus(other: BigInteger): BigInteger defined in kotlin
public operator fun <T> Iterable<TypeVariable(T)>.minus(element: TypeVariable(T)): List<TypeVariable(T)> defined in kotlin.collections
public operator fun <T> Iterable<TypeVariable(T)>.minus(elements: Array<out TypeVariable(T)>): List<TypeVariable(T)> defined in kotlin.collections
public operator fun <T> Iterable<TypeVariable(T)>.minus(elements: Iterable<TypeVariable(T)>): List<TypeVariable(T)> defined in kotlin.collections
public operator fun <T> Iterable<TypeVariable(T)>.minus(elements: Sequence<TypeVariable(T)>): List<TypeVariable(T)> defined in kotlin.collections
public operator fun <K, V> Map<out TypeVariable(K), TypeVariable(V)>.minus(key: TypeVariable(K)): Map<TypeVariable(K), TypeVariable(V)> defined in kotlin.collections
public operator fun <K, V> Map<out TypeVariable(K), TypeVariable(V)>.minus(keys: Array<out TypeVariable(K)>): Map<TypeVariable(K), TypeVariable(V)> defined in kotlin.collections
public operator fun <K, V> Map<out TypeVariable(K), TypeVariable(V)>.minus(keys: Iterable<TypeVariable(K)>): Map<TypeVariable(K), TypeVariable(V)> defined in kotlin.collections
public operator fun <K, V> Map<out TypeVariable(K), TypeVariable(V)>.minus(keys: Sequence<TypeVariable(K)>): Map<TypeVariable(K), TypeVariable(V)> defined in kotlin.collections
public operator fun <T> Set<TypeVariable(T)>.minus(element: TypeVariable(T)): Set<TypeVariable(T)> defined in kotlin.collections
public operator fun <T> Set<TypeVariable(T)>.minus(elements: Array<out TypeVariable(T)>): Set<TypeVariable(T)> defined in kotlin.collections
public operator fun <T> Set<TypeVariable(T)>.minus(elements: Iterable<TypeVariable(T)>): Set<TypeVariable(T)> defined in kotlin.collections
public operator fun <T> Set<TypeVariable(T)>.minus(elements: Sequence<TypeVariable(T)>): Set<TypeVariable(T)> defined in kotlin.collections
public operator fun <T> Sequence<TypeVariable(T)>.minus(element: TypeVariable(T)): Sequence<TypeVariable(T)> defined in kotlin.sequences
public operator fun <T> Sequence<TypeVariable(T)>.minus(elements: Array<out TypeVariable(T)>): Sequence<TypeVariable(T)> defined in kotlin.sequences
public operator fun <T> Sequence<TypeVariable(T)>.minus(elements: Iterable<TypeVariable(T)>): Sequence<TypeVariable(T)> defined in kotlin.sequences
public operator fun <T> Sequence<TypeVariable(T)>.minus(elements: Sequence<TypeVariable(T)>): Sequence<TypeVariable(T)> defined in kotlin.sequences

CodePudding user response:

You can do it with Path.combine(PathOperation.Difference, path1, path2)

@Composable
fun PathDiff() {
    Canvas(
        modifier = Modifier
            .fillMaxSize(),
    ) {
        val rect1 = Path().apply {
            addRect(Rect(Offset.Zero, Offset(100.0F, 100.0F)))

        }
        val rect2 = Path().apply {
            addRect(Rect(Offset(50.0F, 50.0F), Offset(150.0F, 150.0F)))

        }

       val newPath = Path.combine(PathOperation.Difference, path1 = rect1, path2 = rect2)

        drawPath(
            path = newPath,
            color = Color.Black,
            alpha = 1F,
        )
    }
}

or

var newPath= Path()

newPath.op(path1, path2, PathOperation.Difference)
  • Related