diff --git a/README.md b/README.md index fdb68af..a9a046f 100644 --- a/README.md +++ b/README.md @@ -88,3 +88,105 @@ BUG有很多,请大家多多指出. 提BUG给我哈. (现在都是我自己给 新增 10.9 单步调试工具栏 新增 10.10 计算表达式 +``` +import android.graphics.Paint +import androidx.compose.foundation.Canvas +import androidx.compose.foundation.layout.* +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.CornerRadius +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.geometry.Size +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.Path +import androidx.compose.ui.graphics.drawscope.drawIntoCanvas +import androidx.compose.ui.graphics.nativeCanvas +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp + +@Composable +fun RiskLevelIndicator( + productRiskLevel: Int = 3, + userRiskTolerance: Int = 4 +) { + val boxWidth = 60f + val boxHeight = 60f + val boxGap = 8f + + Column(horizontalAlignment = Alignment.CenterHorizontally) { + Text("Product risk level", fontSize = 14.sp) + Canvas( + modifier = Modifier + .fillMaxWidth() + .height(120.dp) + .padding(horizontal = 16.dp) + ) { + val totalBoxes = 6 + val startX = (size.width - (boxWidth * totalBoxes + boxGap * (totalBoxes - 1))) / 2f + val centerY = size.height / 2f + + for (i in 0 until totalBoxes) { + val x = startX + i * (boxWidth + boxGap) + val color = when (i) { + productRiskLevel -> Color(0xFF2B4A78) // 深蓝色 + userRiskTolerance -> Color(0xFF4CAF50) // 绿色 + else -> Color(0xFFD3D3D3) // 浅灰色 + } + + drawRoundRect( + color = color, + topLeft = Offset(x, centerY - boxHeight / 2), + size = Size(boxWidth, boxHeight), + cornerRadius = CornerRadius(8f, 8f) + ) + + // 绘制数字 + drawIntoCanvas { + it.nativeCanvas.apply { + val textPaint = Paint().apply { + color = android.graphics.Color.BLACK + textAlign = Paint.Align.CENTER + textSize = 36f + isAntiAlias = true + } + drawText( + "$i", + x + boxWidth / 2, + centerY + 12f, + textPaint + ) + } + } + } + + // 绘制上方箭头(产品风险) + val px = startX + productRiskLevel * (boxWidth + boxGap) + boxWidth / 2 + drawPath( + path = Path().apply { + moveTo(px, centerY - boxHeight / 2 - 10) + lineTo(px - 10, centerY - boxHeight / 2 - 30) + lineTo(px + 10, centerY - boxHeight / 2 - 30) + close() + }, + color = Color.Black + ) + + // 绘制下方箭头(用户容忍度) + val ux = startX + userRiskTolerance * (boxWidth + boxGap) + boxWidth / 2 + drawPath( + path = Path().apply { + moveTo(ux, centerY + boxHeight / 2 + 10) + lineTo(ux - 10, centerY + boxHeight / 2 + 30) + lineTo(ux + 10, centerY + boxHeight / 2 + 30) + close() + }, + color = Color.Black + ) + } + Text("Your risk tolerance", fontSize = 14.sp) + } +} + +```