我正在try 使用this minus extension function of Path在Jetpack编写代码.

但我收到了一个未解决的参考错误.

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

推荐答案

您可以使用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,
            col或 = Col或.Black,
            alpha = 1F,
        )
    }
}

var newPath= Path()

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

Android相关问答推荐

Android深度链接配置中的URL片段匹配'

图像在Android Studio中显示,但不在设备中显示

泛型类型lambda函数参数作为函数参数

尽管我们不再使用GCM SDK,但应用程序已被标记为使用GCM SDK

Jetpack Compose中尺寸不断增加的动画

如何解决Gradle构建错误:java.lang.NoSuchMethodError

Jetpack Compose:根据盒子中的最大视图固定宽度和高度

没有打开历史记录的活动意向 Select 器完成调用活动

如何在卡片视图右侧添加箭头

FFmpeg Android 错误

如何在C++中使用JNI_GetCreatedJavaVMs调用Java代码

如何让这个三角形指示器在 android jetpack compose 中旋转和移动?

了解 Compose 声明性逻辑

Jetpack Compose UI - 在 AlertDialog 中单击时按钮宽度会发生变化

在事件中使用 Context/Toast 时不需要的重组 - Jetpack Compose

viewModel 的可变值状态不起作用

如何在 MAUI 项目中包含每个平台的现有 C++ 库?

0dp 大小的可组合文件是否可以组合?

WearOS - 有没有办法从心电图传感器收集原始数据?

如何满足设备内框架的无效 Wear OS 屏幕截图Wear OS 表盘策略违规?