kotlin 如何让合成渲染我的自定义形状?

qpgpyjmq  于 2023-02-24  发布在  Kotlin
关注(0)|答案(1)|浏览(133)

我有这样的Composable,它绘制了一些文本,后面有一个自定义形状:

@Composable
fun TextWithIcon(text: String, icon: Shape) {
    Box(Modifier.size(48.dp)
        .border(1.dp, MaterialTheme.colors.secondaryVariant)
        .background(MaterialTheme.colors.background)
        .padding(4.dp)
        .border(2.dp, MaterialTheme.colors.secondaryVariant, icon)
        .padding(4.dp), contentAlignment = Alignment.Center) {
        Text(text, color = MaterialTheme.colors.onBackground)
    }
}

每当我调用它时,Compose都会在内部抛出一个Exception,它甚至没有提到我的代码:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Failed to Image::makeFromBitmap Bitmap(_ptr=0x249bafa64c0)
    at org.jetbrains.skia.Image$Companion.makeFromBitmap(Image.kt:115)
    at androidx.compose.ui.graphics.SkiaBackedCanvas.drawImageRect-cI72Soc(SkiaBackedCanvas.skiko.kt:210)
    at androidx.compose.ui.graphics.SkiaBackedCanvas.drawImageRect-HPBpro0(SkiaBackedCanvas.skiko.kt:185)
    at androidx.compose.ui.graphics.drawscope.CanvasDrawScope.drawImage-AZ2fEMs(CanvasDrawScope.kt:263)
    at androidx.compose.ui.node.LayoutNodeDrawScope.drawImage-AZ2fEMs(LayoutNodeDrawScope.kt)
    at androidx.compose.ui.graphics.drawscope.DrawScope.drawImage-AZ2fEMs$default(DrawScope.kt:510)
    at androidx.compose.foundation.BorderKt$drawGenericBorder$3.invoke(Border.kt:318)
    at androidx.compose.foundation.BorderKt$drawGenericBorder$3.invoke(Border.kt:315)
    at androidx.compose.ui.draw.DrawContentCacheModifier.draw(DrawModifier.kt:218)
    at androidx.compose.ui.node.DrawEntity.draw(DrawEntity.kt:98)
    at androidx.compose.ui.node.LayoutNodeWrapper.drawContainedDrawModifiers(LayoutNodeWrapper.kt:320)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:310)
    at androidx.compose.ui.node.LayoutNode.draw$ui(LayoutNode.kt:839)
    at androidx.compose.ui.node.InnerPlaceable.performDraw(InnerPlaceable.kt:90)
    at androidx.compose.ui.node.LayoutNodeWrapper.drawContainedDrawModifiers(LayoutNodeWrapper.kt:318)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:310)
    at androidx.compose.ui.node.ModifiedLayoutNode.performDraw(ModifiedLayoutNode.kt:139)
    at androidx.compose.ui.node.LayoutNodeDrawScope.drawContent(LayoutNodeDrawScope.kt:48)
    at androidx.compose.foundation.Background.draw(Background.kt:107)
    at androidx.compose.ui.node.DrawEntity.draw(DrawEntity.kt:98)
    at androidx.compose.ui.node.LayoutNodeWrapper.drawContainedDrawModifiers(LayoutNodeWrapper.kt:320)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:310)
    at androidx.compose.ui.node.LayoutNode.draw$ui(LayoutNode.kt:839)
    at androidx.compose.ui.node.InnerPlaceable.performDraw(InnerPlaceable.kt:90)
    at androidx.compose.ui.node.LayoutNodeWrapper.drawContainedDrawModifiers(LayoutNodeWrapper.kt:318)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:310)
    at androidx.compose.ui.node.LayoutNode.draw$ui(LayoutNode.kt:839)
    at androidx.compose.ui.node.InnerPlaceable.performDraw(InnerPlaceable.kt:90)
    at androidx.compose.ui.node.LayoutNodeWrapper.drawContainedDrawModifiers(LayoutNodeWrapper.kt:318)
    at androidx.compose.ui.node.LayoutNodeWrapper.access$drawContainedDrawModifiers(LayoutNodeWrapper.kt:60)
    at androidx.compose.ui.node.LayoutNodeWrapper$invoke$1.invoke(LayoutNodeWrapper.kt:340)
    at androidx.compose.ui.node.LayoutNodeWrapper$invoke$1.invoke(LayoutNodeWrapper.kt:339)
    at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2117)
    at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:113)
    at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui(OwnerSnapshotObserver.kt:78)
    at androidx.compose.ui.node.LayoutNodeWrapper.invoke(LayoutNodeWrapper.kt:339)
    at androidx.compose.ui.node.LayoutNodeWrapper$invokeOnCanvasInstance$1.invoke(LayoutNodeWrapper.kt:161)
    at androidx.compose.ui.node.LayoutNodeWrapper$invokeOnCanvasInstance$1.invoke(LayoutNodeWrapper.kt:161)
    at androidx.compose.ui.platform.SkiaLayer.performDrawLayer(SkiaLayer.skiko.kt:264)
    at androidx.compose.ui.platform.SkiaLayer.drawLayer(SkiaLayer.skiko.kt:225)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:305)
    at androidx.compose.ui.node.LayoutNode.draw$ui(LayoutNode.kt:839)
    at androidx.compose.ui.node.InnerPlaceable.performDraw(InnerPlaceable.kt:90)
    at androidx.compose.ui.node.LayoutNodeWrapper.drawContainedDrawModifiers(LayoutNodeWrapper.kt:318)
    at androidx.compose.ui.node.LayoutNodeWrapper.draw(LayoutNodeWrapper.kt:310)
    at androidx.compose.ui.node.LayoutNode.draw$ui(LayoutNode.kt:839)
    at androidx.compose.ui.platform.SkiaBasedOwner.draw(SkiaBasedOwner.skiko.kt:347)
    at androidx.compose.ui.ComposeScene.render(ComposeScene.skiko.kt:419)
    at androidx.compose.ui.awt.ComposeLayer$1$onRender$1.invoke(ComposeLayer.desktop.kt:316)
    at androidx.compose.ui.awt.ComposeLayer$1$onRender$1.invoke(ComposeLayer.desktop.kt:315)
    at androidx.compose.ui.awt.ComposeLayer.catchExceptions(ComposeLayer.desktop.kt:107)
    at androidx.compose.ui.awt.ComposeLayer.access$catchExceptions(ComposeLayer.desktop.kt:85)
    at androidx.compose.ui.awt.ComposeLayer$1.onRender(ComposeLayer.desktop.kt:315)
    at org.jetbrains.skiko.SkiaLayer.update$skiko(SkiaLayer.awt.kt:510)
    at org.jetbrains.skiko.redrawer.AWTRedrawer.update(AWTRedrawer.kt:54)
    at org.jetbrains.skiko.redrawer.Direct3DRedrawer$frameDispatcher$1.invokeSuspend(Direct3DRedrawer.kt:40)
    at org.jetbrains.skiko.redrawer.Direct3DRedrawer$frameDispatcher$1.invoke(Direct3DRedrawer.kt)
    at org.jetbrains.skiko.redrawer.Direct3DRedrawer$frameDispatcher$1.invoke(Direct3DRedrawer.kt)
    at org.jetbrains.skiko.FrameDispatcher$job$1.invokeSuspend(FrameDispatcher.kt:33)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@119c1b04, SwingDispatcher@56da22a8]

这种情况发生在每个形状上,甚至包括一个根本没有绘制任何内容的形状:

TextWithIcon("Hi", GenericShape { _, _ -> })

奇怪的是,我确信我看到了这段代码的工作原理,并画出了形状,但我不确定我在那之后做了什么改变,这破坏了它。
Modifier中删除除(第二个)边框之外的所有内容没有帮助,仍然会崩溃。同样删除Box内容,然后修复崩溃,大概是因为Compose知道元素是空的,甚至不会尝试渲染它...
我怎么才能让它工作?

n6lpvg4x

n6lpvg4x1#

在合成中,形状不允许为null、零或空。当您尝试绘制空形状时,通常会调用错误java.lang.IllegalArgumentException: width and height must be > 0,表示不支持GenericShape { _, _ -> }
但在您的情况下,错误是线程“AWT-EventQueue-0”中的异常,该线程通常在错误初始化时调用,此线程用于处理用户事件,如单击
应该将GenericShape更改为占用宽度和高度的内容

相关问题