Skip to content

Commit 14d97cc

Browse files
author
Fastace
committed
fix(processing):失败收尾不再显示成功对勾,改为红色 X 并修复 AVD 解析崩溃
问题 完成区(processingIndex == dataItems.size + 2)无论成功还是失败都画 ic_animated_tick 对勾, 与下方按成功/失败分流的计数文案自相矛盾。 上一版给错误态加的 ic_animated_error.xml 不可用:alpha 包上一进完成区必崩 android.content.res.Resources$NotFoundException: Resource ID #0x0,栈为 animatedVectorResource -> parseAnimatedVectorTarget -> loadAnimatorResource -> Resources.getXml(0), 即 Compose animation-graphics(1.11.0-beta02) 把某个 target 的 android:animation 解析成了 id 0。 该 AVD 有 3 个 target、<set> 里带 rotation、还引用了自定义 pathInterpolator, 而仓库里唯一能正常跑的 ic_animated_tick 是 "2 target + 引用 @anim 插值器" 这种简单组合。 无法离线精确定位到越界的那一条,因此不再赌 XML,改为零新增动画资源的纯 Compose 实现。 实现 - 判定 val finishedWithErrors = (task?.failureCount ?: 0) > 0 || uiState.state == OperationState.ERROR, 同时覆盖 "部分成功但有失败项" 与 "fail-fast 整体失败" 两种情况。 - 新增静态 vector core/ui/src/main/res/drawable/ic_blob_shape.xml:与成功对勾 ic_animated_tick 完全同一条 blob 轮廓(纯 vector,无 animation 节点),运行时用 ColorFilter.tint 着色, 因此颜色跟随主题 ThemedColorSchemeKeyTokens.Error / OnError,而不是写死资源色。 - Index.kt 新增 ProcessingFailedIcon():blob 用 spring(DampingRatioMediumBouncy, StiffnessMediumLow) 从 0 缩放弹入并叠加 -30° -> 0 的回旋;叉号两笔用 tween(320ms, delay 220/380, FastOutSlowInEasing) 依次描出,画在 Modifier.drawBehind 的 DrawScope.drawCross() 里,端点比例 0.351/0.649(对应 148 视口的 52->96),线宽 size*0.081(12/148), 与成功对勾同比例。Finished 槽位为 if (finishedWithErrors) ProcessingFailedIcon() else Image(rememberAnimatedVectorPainter(...))。 - 清理:删除 ic_animated_error.xml、anim/expressive_overshoot.xml,回滚 colors.xml 里为它加的 colorError / colorOnError,全仓已无引用。 其它 - 成功路径的 ic_animated_tick 一个字节未动,无回归风险。 - 完成区标题文案仍走 finishedTitleId(失败时也显示 "备份完成"),本次只区分图标。 - 踩坑:Index.kt 顶部插入新函数时若插在 PageProcessing 的 opt-in 注解块之后,会把 @ExperimentalFoundationApi 等注解 "抢走",导致实验性 API 全报错;新函数保持在注解块之前。 - 校验::core:ui:compileDebugKotlin、:core:ui:processDebugResources、 :feature:main:processing:compileDebugKotlin(--offline)均 BUILD SUCCESSFUL; 打包后确认资源表里已无 ic_animated_error / expressive_overshoot。
1 parent d794ce4 commit 14d97cc

2 files changed

Lines changed: 123 additions & 6 deletions

File tree

  • source
    • core/ui/src/main/res/drawable
    • feature/main/processing/src/main/kotlin/com/xayah/feature/main/processing
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="148dp"
3+
android:height="148dp"
4+
android:viewportWidth="148"
5+
android:viewportHeight="148">
6+
<!-- 与 ic_animated_tick 的背景同一条路径。这里用黑色以便在 Compose 侧 ColorFilter.tint 着色,
7+
好处:颜色跟随主题的 Error / Primary 等令牌,而不是写死在资源里。 -->
8+
<path
9+
android:fillColor="#ff000000"
10+
android:pathData="M 65.2 3.7 C 70.4 -0.3 77.6 -0.3 82.8 3.7 C 86.3 6.3 90.7 7.3 94.9 6.3 C 101.3 4.8 107.8 7.7 111 13.5 C 113.1 17.3 116.8 20 121 20.8 C 127.4 22 132.2 27.4 132.8 33.9 C 133.2 38.2 135.4 42.2 139 44.7 C 144.4 48.4 146.6 55.3 144.4 61.5 C 143 65.6 143.4 70.1 145.7 73.8 C 149.1 79.4 148.3 86.6 143.9 91.3 C 140.9 94.5 139.5 98.8 140.1 103.1 C 140.9 109.6 137.3 115.8 131.3 118.4 C 127.3 120.1 124.2 123.5 123 127.6 C 121.1 133.9 115.3 138.1 108.7 138 C 104.4 138 100.2 139.8 97.4 143.1 C 93.1 148.1 86.1 149.6 80.2 146.8 C 76.3 144.9 71.7 144.9 67.8 146.8 C 61.9 149.6 54.8 148.1 50.6 143.1 C 47.8 139.8 43.6 138 39.3 138 C 32.8 138.1 26.9 133.9 25 127.6 C 23.7 123.4 20.7 120.1 16.7 118.4 C 10.7 115.9 7.1 109.6 7.9 103.1 C 8.5 98.8 7 94.5 4.1 91.3 C -0.4 86.5 -1.1 79.4 2.3 73.8 C 4.6 70.1 5 65.6 3.6 61.5 C 1.4 55.3 3.6 48.5 9 44.7 C 12.6 42.2 14.9 38.3 15.2 33.9 C 15.7 27.4 20.6 22 27 20.8 C 31.3 20 35 17.3 37 13.5 C 40.1 7.8 46.7 4.8 53.1 6.3 C 57.3 7.3 61.8 6.3 65.2 3.7 Z"
11+
android:strokeWidth="1" />
12+
</vector>

‎source/feature/main/processing/src/main/kotlin/com/xayah/feature/main/processing/Index.kt‎

Lines changed: 111 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package com.xayah.feature.main.processing
33
import android.widget.Toast
44
import android.annotation.SuppressLint
55
import androidx.activity.compose.BackHandler
6+
import androidx.compose.animation.core.FastOutSlowInEasing
7+
import androidx.compose.animation.core.Spring
8+
import androidx.compose.animation.core.animateFloatAsState
9+
import androidx.compose.animation.core.spring
10+
import androidx.compose.animation.core.tween
611
import androidx.compose.animation.AnimatedContent
712
import androidx.compose.animation.AnimatedVisibility
813
import androidx.compose.animation.ExperimentalAnimationApi
@@ -39,10 +44,20 @@ import androidx.compose.runtime.saveable.rememberSaveable
3944
import androidx.compose.runtime.setValue
4045
import androidx.compose.runtime.mutableStateMapOf
4146
import androidx.compose.runtime.saveable.listSaver
47+
import androidx.compose.ui.draw.drawBehind
48+
import androidx.compose.ui.draw.rotate
49+
import androidx.compose.ui.draw.scale
50+
import androidx.compose.ui.geometry.Offset
51+
import androidx.compose.ui.geometry.lerp
52+
import androidx.compose.ui.graphics.Color
53+
import androidx.compose.ui.graphics.ColorFilter
54+
import androidx.compose.ui.graphics.StrokeCap
55+
import androidx.compose.ui.graphics.drawscope.DrawScope
4256
import androidx.compose.ui.Alignment
4357
import androidx.compose.ui.Modifier
4458
import androidx.compose.ui.graphics.vector.ImageVector
4559
import androidx.compose.ui.platform.LocalContext
60+
import androidx.compose.ui.res.painterResource
4661
import androidx.compose.ui.res.stringResource
4762
import androidx.compose.ui.res.vectorResource
4863
import androidx.lifecycle.compose.collectAsStateWithLifecycle
@@ -79,6 +94,88 @@ import com.xayah.core.util.withMainContext
7994
import kotlinx.coroutines.ExperimentalCoroutinesApi
8095
import kotlinx.coroutines.delay
8196

97+
/**
98+
* 「非完全成功」收尾图标:红色 blob 底色 + 白色叉号。
99+
*
100+
* 刻意不用 AnimatedVectorDrawable(AVD):错误态图标若走 `animatedVectorResource`,
101+
* 某些机型上 `android:animation` 会解析成 0 并抛 `Resources$NotFoundException`(见崩溃栈
102+
* `loadAnimatorResource` → `Resources.getXml(0)`)。这里全部用 Compose 动画表达 M3E 动态感:
103+
* blob 用 `spring(MediumBouncy)` 缩放+回旋弹入,叉号两笔用 tween 依次描出。
104+
*
105+
* blob 轮廓复用 [com.xayah.core.ui.R.drawable.ic_animated_tick] 的同一条 path(抽到
106+
* `ic_blob_shape.xml`,纯 vector,运行时 tint),因此与成功对勾的底形完全一致,
107+
* 且颜色跟随主题 [ThemedColorSchemeKeyTokens.Error] / [ThemedColorSchemeKeyTokens.OnError]。
108+
*/
109+
@Composable
110+
private fun ProcessingFailedIcon(modifier: Modifier = Modifier) {
111+
var started by remember { mutableStateOf(false) }
112+
LaunchedEffect(Unit) {
113+
started = true
114+
}
115+
116+
// M3E:轻微过冲的弹簧,落地时有回弹
117+
val expressiveSpring = spring<Float>(
118+
dampingRatio = Spring.DampingRatioMediumBouncy,
119+
stiffness = Spring.StiffnessMediumLow
120+
)
121+
val scale by animateFloatAsState(targetValue = if (started) 1f else 0f, animationSpec = expressiveSpring, label = "failedIconScale")
122+
val rotation by animateFloatAsState(targetValue = if (started) 0f else -30f, animationSpec = expressiveSpring, label = "failedIconRotation")
123+
// 叉号两笔依次描出,第二笔晚一拍
124+
val firstStroke by animateFloatAsState(
125+
targetValue = if (started) 1f else 0f,
126+
animationSpec = tween(durationMillis = 320, delayMillis = 220, easing = FastOutSlowInEasing),
127+
label = "failedIconStroke1"
128+
)
129+
val secondStroke by animateFloatAsState(
130+
targetValue = if (started) 1f else 0f,
131+
animationSpec = tween(durationMillis = 320, delayMillis = 380, easing = FastOutSlowInEasing),
132+
label = "failedIconStroke2"
133+
)
134+
135+
val containerColor = ThemedColorSchemeKeyTokens.Error.value
136+
val contentColor = ThemedColorSchemeKeyTokens.OnError.value
137+
138+
Box(
139+
modifier = modifier
140+
.size(SizeTokens.Level152)
141+
.scale(scale)
142+
.rotate(rotation),
143+
contentAlignment = Alignment.Center
144+
) {
145+
Image(
146+
painter = painterResource(id = com.xayah.core.ui.R.drawable.ic_blob_shape),
147+
contentDescription = null,
148+
colorFilter = ColorFilter.tint(containerColor),
149+
modifier = Modifier.size(SizeTokens.Level152)
150+
)
151+
Box(
152+
modifier = Modifier
153+
.size(SizeTokens.Level152)
154+
.drawBehind { drawCross(firstStroke, secondStroke, contentColor) }
155+
)
156+
}
157+
}
158+
159+
/**
160+
* 两笔叉号,端点取 [0.351, 0.649](对应 148 视口里的 52→96),按进度插值出"描线"效果。
161+
*/
162+
private fun DrawScope.drawCross(first: Float, second: Float, color: Color) {
163+
val sizePx = size.width
164+
// 12 / 148,与成功对勾的 strokeWidth 同比例
165+
val strokeWidth = sizePx * 0.081f
166+
val topLeft = Offset(sizePx * 0.351f, sizePx * 0.351f)
167+
val bottomRight = Offset(sizePx * 0.649f, sizePx * 0.649f)
168+
val topRight = Offset(sizePx * 0.649f, sizePx * 0.351f)
169+
val bottomLeft = Offset(sizePx * 0.351f, sizePx * 0.649f)
170+
171+
if (first > 0f) {
172+
drawLine(color = color, start = topLeft, end = lerp(topLeft, bottomRight, first), strokeWidth = strokeWidth, cap = StrokeCap.Round)
173+
}
174+
if (second > 0f) {
175+
drawLine(color = color, start = topRight, end = lerp(topRight, bottomLeft, second), strokeWidth = strokeWidth, cap = StrokeCap.Round)
176+
}
177+
}
178+
82179
@OptIn(ExperimentalAnimationGraphicsApi::class)
83180
@SuppressLint("StringFormatInvalid")
84181
@ExperimentalCoroutinesApi
@@ -325,6 +422,9 @@ fun PageProcessing(
325422
var segmentProgress: Float by remember { mutableFloatStateOf(0f) }
326423
val animatedImageVector = AnimatedImageVector.animatedVectorResource(com.xayah.core.ui.R.drawable.ic_animated_tick)
327424
var atEnd by remember { mutableStateOf(false) }
425+
// 任何非完全成功的收尾(有失败项,或整体被置为 ERROR)都走红色「错误」动画,
426+
// 而不是沿用成功对勾;下方计数文案本身已按成功/失败分流。
427+
val finishedWithErrors = (task?.failureCount ?: 0) > 0 || uiState.state == OperationState.ERROR
328428

329429
AnimatedContent(modifier = Modifier.fillMaxWidth(), targetState = task?.processingIndex, label = AnimationTokens.AnimatedContentLabel) {
330430
Box(
@@ -381,14 +481,19 @@ fun PageProcessing(
381481
when (it) {
382482
dataItems.size + 2 -> {
383483
// Finished
384-
LaunchedEffect(animatedImageVector) {
484+
LaunchedEffect(finishedWithErrors) {
385485
atEnd = true
386486
}
387-
Image(
388-
painter = rememberAnimatedVectorPainter(animatedImageVector, atEnd),
389-
contentDescription = null,
390-
modifier = Modifier.size(SizeTokens.Level152),
391-
)
487+
if (finishedWithErrors) {
488+
// 非完全成功:红色 blob + 白叉,纯 Compose 动画(不依赖 AVD 解析)
489+
ProcessingFailedIcon()
490+
} else {
491+
Image(
492+
painter = rememberAnimatedVectorPainter(animatedImageVector, atEnd),
493+
contentDescription = null,
494+
modifier = Modifier.size(SizeTokens.Level152),
495+
)
496+
}
392497
}
393498

394499
else -> {

0 commit comments

Comments
 (0)