英文:
How show animation with clipboard in android compose
问题
I use Clipboard:
val clipboardManager: ClipboardManager = LocalClipboardManager.current
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_copy_track_number),
contentDescription = "Copy",
modifier = Modifier
.padding(start = 4.dp)
.size(16.dp)
.clickable {
clipboardManager.setText(AnnotatedString(trackNumber))
}
)
And I had a question: how to make a temporary animation that changes color for the duration of a user's click (in this case, briefly replace it with another icon), for example, for 2 seconds?
英文:
I use Clipboard:
val clipboardManager: ClipboardManager = LocalClipboardManager.current
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_copy_track_number),
contentDescription = "Copy",
modifier = Modifier
.padding(start = 4.dp)
.size(16.dp)
.clickable {
clipboardManager.setText(AnnotatedString(trackNumber))
}
)
And I had a question: how to make a temporary animation that changes color for the duration of a user's click (in this case, briefly replace it with another icon), for example, for 2 seconds?
答案1
得分: 2
你需要使用一个 InteractionSource
对象。你可以在这里找到一个示例。
而不是使用 AnimatedVisibility
,你需要为你的图标颜色添加一个无限动画:
@Composable
private fun ColourAnimatedButton(interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }) {
val isPressed by interactionSource.collectIsPressedAsState()
val infiniteTransition = rememberInfiniteTransition(label = "")
val idleColor = Color.Blue
val animatedColor by infiniteTransition.animateColor(
initialValue = Color.Blue, targetValue = Color.Red, animationSpec = infiniteRepeatable(
animation = keyframes {
durationMillis = 1000 // 颜色变化的速度
}, repeatMode = RepeatMode.Restart
), label = ""
)
Icon(imageVector = ImageVector.vectorResource(id = R.drawable.ic_copy_track_number),
contentDescription = "Copy", modifier = Modifier
.padding(start = 4.dp)
.size(16.dp).clickable(interactionSource, LocalIndication.current) {
clipboardManager.setText(AnnotatedString(trackNumber))
},
tint = if (isPressed) animatedColor else idleColor
)
}
编辑(一次性点击时的动画):
(移除 isPressed
、infiniteTransition
、idleColor
和 animatedColor
,然后添加以下内容:
val color = remember { Animatable(Color.Black) }
点击监听器更改为以下内容:
.clickable(interactionSource, LocalIndication.current) {
scope.launch {
color.animateTo(Color.Blue, tween(500))
color.animateTo(Color.Black, tween(500))
}
},
最后将颜色设置如下:tint = color.value
英文:
You need to use an InteractionSource
object. You can find an example over here.
Instead of the AnimatedVisibility you'll need an infinite animation for your icon's colour:
@Composable
private fun ColourAnimatedButton(interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }) {
val isPressed by interactionSource.collectIsPressedAsState()
val infiniteTransition = rememberInfiniteTransition(label = "")
val idleColor = Color.Blue
val animatedColor by infiniteTransition.animateColor(
initialValue = Color.Blue, targetValue = Color.Red, animationSpec = infiniteRepeatable(
animation = keyframes {
durationMillis = 1000 // the speed at which colours will change
}, repeatMode = RepeatMode.Restart
), label = ""
)
Icon(imageVector = ImageVector.vectorResource(id = R.drawable.ic_copy_track_number),
contentDescription = "Copy", modifier = Modifier
.padding(start = 4.dp)
.size(16.dp).clickable(interactionSource, LocalIndication.current) {
clipboardManager.setText(AnnotatedString(trackNumber))
},
tint = if (isPressed) animatedColor else idleColor
)
}
EDIT (onClick one-off animation)
(remove the isPressed
, infiniteTransition
, idleColor
, and animatedColor
and add this instead:
val color = remember { Animatable(Color.Black) }
Click listener changes to this:
.clickable(interactionSource, LocalIndication.current) {
scope.launch {
color.animateTo(Color.Blue, tween(500))
color.animateTo(Color.Black, tween(500))
}
},
and finally set the colour like this: tint = color.value
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论