Android Studio Jetpack Compose毛玻璃特效按钮

直接上图看效果

直接上compose代码

kotlin 复制代码
/**
 * 自定义毛玻璃效果按钮 - 背景模糊,文字清晰
 */
@Composable
fun GlassButton(
    onClick: () -> Unit,
    text: String,
    backgroundColor: Color = Color.White,
    txetColor: Color = Color.White,
    modifier: Modifier = Modifier
) {
    val isHigherAndroidVersion = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S
    
    Box(modifier = modifier) {
        // 毛玻璃背景层
        Card(
            modifier = Modifier.fillMaxSize(),
            shape = RoundedCornerShape(25.dp),
            // 半透明背景
            colors = CardDefaults.cardColors(
                containerColor = backgroundColor.copy(alpha = 0.12f)
            ),
            // 增加细微白色边框增强玻璃感
            border = BorderStroke(0.5.dp, backgroundColor.copy(alpha = 0.3f))
        ) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .background(
                        brush = Brush.verticalGradient(
                            colors = listOf(
                                backgroundColor.copy(alpha = 0.2f),
                                backgroundColor.copy(alpha = 0.1f)
                            )
                        )
                    )
                    .then(
                        // 只对背景应用模糊效果
                        if (isHigherAndroidVersion) {
                            Modifier.blur(radius = 20.dp)
                        } else {
                            Modifier
                        }
                    )
            )
        }
        
        // 前景内容层 - 不应用模糊效果
        Box(
            modifier = Modifier
                .clip(RoundedCornerShape(25.dp))
                .fillMaxSize()
                .clickable(onClick = onClick),
            contentAlignment = Alignment.Center
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.Center
            ) {
                // 星星图标
                Icon(
                    imageVector = Icons.Filled.Star,
                    contentDescription = null,
                    tint = Color.White,
                    modifier = Modifier.size(20.dp)
                )
                
                Spacer(modifier = Modifier.width(8.dp))
                
                // 文字内容 - 保持清晰
                Text(
                    text = text,
                    fontSize = 16.sp,
                    color = txetColor,
                    fontWeight = androidx.compose.ui.text.font.FontWeight.Medium
                )
            }
        }
    }
}

根据需求改代码,比如改成下面这样的

代码贴上了,具体根据需求改。

博主幸劳,转载记得标注原出处链接,支持原创。

相关推荐
心平气和量大福大4 小时前
android-控件-搜索框SearchView
android·gitee
2601_962177134 小时前
【MySQL篇】聚合查询,联合查询
android·java·mysql
壮哥_icon4 小时前
【Android】批量 VectorDrawable 动态分别修改 fillColor 和 strokeColor 的踩坑与终极解决方案
android
齐天qaq5 小时前
Android 系统 APK 分区与权限
android
心平气和量大福大5 小时前
android-控件-多选框CheckBox
android·gitee
我命由我123456 小时前
Linux - Linux/POSIX 路径斜杠折叠规则
linux·运维·服务器·android studio·android jetpack·android-studio·android runtime
pengyu7 小时前
【Kotlin 协程修仙录 · 大乘境 · 初阶】 | 跳出三界:协程在 KMP 与后端开发中的跨平台之道
android·kotlin
XiaoLeisj7 小时前
Android Memory Profiler:堆内存指标、内存抖动与泄漏定位
android·性能优化·内存抖动·内存泄露·memory profiler
未来猫咪花7 小时前
Everything is ViewModel:让状态管理回到对象世界
android·flutter·ios