compose开发:去除点击水波纹阴影效果

  • Post author:
  • Post category:其他




单个view去除

// 举例代码,默认有点击阴影效果
            Column(horizontalAlignment = Alignment.CenterHorizontally) {
                Image(
                    painterResource(R.drawable.ic_toolbar_search),
                    contentDescription = null,
                    modifier = Modifier
                        .size(60.dp)
                        .clickable(
                            onClick = { model.count() })
                )
                Text(
                    text = "compose textView"
                )
                Text(
                    text = "compose textView2"
                )
            }


在clickable 中添加indication = null, interactionSource = remember { MutableInteractionSource() }
// 举例代码,局部去除点击阴影效果
            Column(horizontalAlignment = Alignment.CenterHorizontally) {
                Image(
                    painterResource(R.drawable.ic_toolbar_search),
                    contentDescription = null,
                    modifier = Modifier
                        .size(60.dp)
                        .clickable(
                            onClick = { model.count() },
                            // 去除点击效果
                            indication = null,
                            interactionSource = remember {
                                MutableInteractionSource()
                            })
                )
                Text(
                    text = "compose textView"
                )
                Text(
                    text = "compose textView2"
                )
            }



整个界面去掉水波纹点击效果

针对整个Activity,你可以在最root的compose里设置,通过CompositionLocal(让数据流经界面树的一种隐式方式),属性传递,把children就全部替换了

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            // Composable 根源view,一般用来设置主题
            ComposeTheme() {
                // data
                mViewModel = AboutViewModel()
                initData()
                
                //view
                AboutView(this, mViewModel, goBack = { goBack() })
                ShowCustomDialog(mViewModel, startDebug = { mViewModel.startDebug(this) })
            }
        }
    }


@Composable
fun ComposeTheme(
    content: @Composable () -> Unit
) {
    MaterialTheme(colors = LightColorPalette) {
        
        // 设置全局参数,去除默认点击效果
        CompositionLocalProvider(
            LocalIndication provides NoIndication
        ) {
            ProvideTextStyle(value = MaterialTheme.typography.body1, content = content)
        }
    }
}
 
// null indication
object NoIndication : Indication {
    private object NoIndicationInstance : IndicationInstance {
        override fun ContentDrawScope.drawIndication() {
            drawContent()
        }
    }
 
    @Composable
    override fun rememberUpdatedInstance(interactionSource: InteractionSource): IndicationInstance {
        return NoIndicationInstance
    }
}


CompositionLocal



版权声明:本文为geniushorse原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。