如何制作如下图所示的钢琴音符:
它应该有文本并且可点击。
这是我尝试过的:
PianoNotes.kt
const val NUM_NOTES = 7
const val DIVIDER_WIDTH = 5
@Composable
fun PianoNotes(modifier: Modifier = Modifier) {
val config = LocalConfiguration.current
val screenWidth = config.screenWidthDp.dp
val screenHeight = config.screenHeightDp.dp
Row {
repeat(NUM_NOTES) { ithNotes -> // 0 indexed
// NUM_NOTES - 1 because we don't use divider on last notes
val dividerOffset = (DIVIDER_WIDTH * NUM_NOTES - 1).dp
val singleNotesWidth = (screenWidth - dividerOffset) / NUM_NOTES
NaturalNotes(
size = DpSize(width = singleNotesWidth, height = screenHeight),
ithNotes = ithNotes,
)
// we don't need divider on the last notes
if (ithNotes != NUM_NOTES - 1) {
VerticalDivider(
modifier = modifier.fillMaxHeight(),
thickness = DIVIDER_WIDTH.dp,
color = Color.Black
)
}
// sharp notes doesn't exist on the third divider
// we don't even have divider on 6th note
if (ithNotes != 2 && ithNotes != 6) {
SharpNotes(
size = DpSize(60.dp, 2 * screenHeight / 3),
ithNotes = ithNotes,
)
}
}
}
}
NaturalNotes.kt
@Composable
fun NaturalNotes(modifier: Modifier = Modifier, size: DpSize, ithNotes: Int) {
Box(
modifier = modifier
.background(Color.White)
.size(size.width, size.height)
.clickable {
Log.e(
"TAG",
PianoConstants.getNaturalNoteFromNumber(ithNotes + 1)
)
},
contentAlignment = Alignment.BottomCenter
) {
Text(
text = PianoConstants.getNaturalNoteFromNumber(ithNotes + 1),
fontSize = 36.sp,
fontWeight = FontWeight.ExtraBold,
modifier = modifier.padding(bottom = 20.dp)
)
}
}
SharpNotes.kt
@Composable
fun SharpNotes(modifier: Modifier = Modifier, size: DpSize, ithNotes: Int) {
Box(
modifier = modifier
.background(Color.Black)
.size(size.width, size.height)
.clickable {
Log.e(
"TAG",
PianoConstants.getSharpNoteFromNumber(ithNotes + 1)
)
},
contentAlignment = Alignment.BottomCenter
) {
Text(
text = PianoConstants.getSharpNoteFromNumber(ithNotes + 1),
fontSize = 36.sp,
fontWeight = FontWeight.ExtraBold,
color = Color.White,
modifier = modifier.padding(bottom = 20.dp)
)
}
}
这导致:
尖锐的音符会占用空间并且不会与分隔线重叠。我尝试过的其他方法是将VerticalDivider
和包装SharpNotes
在一个盒子中并使用偏移量,但它仍然留下一个空白空间。
PianoConstants
只是一个类,它为我提供了要在笔记中显示的文本
对于这种情况,您需要自定义布局。我整理了一份,根据您的需要进行修改。
主要可组合项
布局
我改变了你的
NaturalNotes
一点SharpNotes
,字体大小仍然需要调整。