Skip to content

Instantly share code, notes, and snippets.

@zty5678
Last active March 26, 2024 15:48
Show Gist options
  • Save zty5678/e0d23580a110e34a7d0ad87ec00639de to your computer and use it in GitHub Desktop.
Save zty5678/e0d23580a110e34a7d0ad87ec00639de to your computer and use it in GitHub Desktop.
compose实现Duolingo按钮效果
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
@Preview
@Composable
fun WidgetButtonPreview() {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
DuolingoButton(
text = "年度报告测试",
normalColor = Color(0xff00B3FD),
pressedColor = Color(0xff009CDC),
cornerRadius = 16.dp,
textColor = Color.White,
fontSize = 16.sp,
offset = 6.dp,
onClick = {
print("按钮被点击了")
}
)
}
}
@Composable
fun DuolingoButton(
text: String,
normalColor: Color,
pressedColor: Color,
cornerRadius: Dp,
textColor: Color,
fontSize: TextUnit,
offset: Dp,
onClick: () -> Unit
) {
val interactionSource = remember { MutableInteractionSource() }
val pressed by interactionSource.collectIsPressedAsState()
Surface(
modifier = Modifier
.clickable(
onClick = onClick,
interactionSource = interactionSource,
indication = null
)
.drawWithCache {
onDrawBehind {
drawRoundRect(
color = pressedColor,
cornerRadius = CornerRadius(cornerRadius.toPx(), cornerRadius.toPx()),
size = Size(
size.width,
size.height
)
)
if (!pressed) {
drawRoundRect(
color = normalColor,
cornerRadius = CornerRadius(
cornerRadius.toPx(),
cornerRadius.toPx()
),
size = Size(
size.width,
size.height - offset.toPx()
)
)
}
}
},
color = Color.Transparent
) {
Text(
text = text,
modifier = Modifier
.padding(
start = 40.dp, end = 40.dp,
top = 16.dp, bottom = 16.dp
)
.offset(
x = 0.dp,
y = if (pressed) 0.dp else (-offset / 2)
),
color = textColor,
fontWeight = androidx.compose.ui.text.font.FontWeight.Bold,
fontSize = fontSize)
}
}
@zty5678
Copy link
Author

zty5678 commented Mar 26, 2024

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment