当前位置:网站首页>Rendering of kotlin jetpack compose tab using animatedvisibility
Rendering of kotlin jetpack compose tab using animatedvisibility
2022-06-23 19:36:00 【Ango cannot move】
Continue with the last blog
This lesson realizes
When there is content, the animation appears below tab
And below tab You can choose Switch

Change the file to
package com.anguomob.jecpack.activity.compose.todo.one
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.TweenSpec
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.anguomob.jecpack.activity.compose.todo.bean.ToDoIcon
import com.anguomob.jecpack.activity.compose.todo.bean.TodoItem
@Composable
fun TodoInputText(text: String, onTextChanged: (String) -> Unit, modifier: Modifier = Modifier) {
TextField(
value = text,
onValueChange = onTextChanged,
modifier = modifier,
colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.Transparent),
maxLines = 1,
)
}
@Composable()
fun TodoEditButton(
onClick: () -> Unit,
text: String,
modifier: Modifier = Modifier,
enable: Boolean = true
) {
TextButton(
onClick = onClick,
shape = CircleShape,
colors = ButtonDefaults.buttonColors(),
modifier = modifier,
enabled = enable
) {
Text(text)
}
}
@Composable
fun TodoItemInput(onItemComplete: (TodoItem) -> Unit) {
val (text, setText) = remember {
mutableStateOf("")
}
val (icon, setIcon) = remember {
mutableStateOf(ToDoIcon.Default)
}
//icon Whether it can be one depends on whether the text has content
val iconsVisible = text.isNotBlank()
Column {
Row(
Modifier
.padding(horizontal = 16.dp)
.padding(top = 16.dp)
) {
TodoInputText(
text = text,
modifier = Modifier
.weight(1f)
.padding(end = 8.dp),
onTextChanged = setText
)
TodoEditButton(
onClick = {
onItemComplete(TodoItem(text))
setText("")
},
text = "Add",
modifier = Modifier.align(Alignment.CenterVertically),
enable = text.isNotBlank()
)
}
AnimatedIconRow(
visible = iconsVisible,
icon = icon,
onIconChange = setIcon,
modifier = Modifier.padding(8.dp)
)
}
}
// Row of icons Depending on whether the text box has content Auto bounce and retract
@Composable
fun AnimatedIconRow(
modifier: Modifier = Modifier,
visible: Boolean,
icon: ToDoIcon,
onIconChange: (ToDoIcon) -> Unit
) {
val enter = remember {
fadeIn(animationSpec = TweenSpec(300, easing = FastOutLinearInEasing))
}
val exit = remember {
fadeOut(animationSpec = TweenSpec(100, easing = FastOutSlowInEasing))
}
// Minimum height 16dp
AnimatedVisibility(visible = visible, enter = enter, exit = exit, modifier = modifier) {
IconRow(icon = icon, onIconChange = onIconChange)
}
}
@Composable
fun IconRow(icon: ToDoIcon, onIconChange: (ToDoIcon) -> Unit, modifier: Modifier = Modifier) {
Row(modifier = modifier) {
for (todoIcon in ToDoIcon.values()) {
SelectableIconButton(
icon = todoIcon.imageVector,
iconContentDescription = todoIcon.contentDescription,
onIconSelect = { onIconChange(todoIcon) },
isSelected = todoIcon == icon
)
}
}
}
@Composable
fun SelectableIconButton(
icon: ImageVector,
iconContentDescription: Int,
onIconSelect: () -> Unit,
isSelected: Boolean,
modifier: Modifier = Modifier
) {
// Icons checked and unchecked The colors are different
val tint = if (isSelected) {
MaterialTheme.colors.primary
} else {
MaterialTheme.colors.onSurface.copy(alpha = 0.6f)
}
TextButton(onClick = onIconSelect, shape = CircleShape, modifier = modifier) {
Column {
Icon(
imageVector = icon,
tint = tint,
contentDescription = stringResource(id = iconContentDescription)
)
if (isSelected) {
Box(
Modifier
.padding(top = 3.dp)
.width(icon.defaultWidth)
.height(1.dp)
.background(tint)
)
} else {
Spacer(modifier = Modifier.height(4.dp))
}
}
}
}
Those things are used for disassembly

Add a new status . It contains the default icon The icon also has a selected status
This is used in state improvement .

There are mainly two animations in this method The animation contains system components
AnimatedVisibility
It also contains a customized component
IconRow
Let's continue to look at this
IconRow
Components

Iterate over the enumeration type and give the iterated loop to a new component
Let's take a look at this new component

He's actually a textButton
And added shape. The brothers said it was necessary to add shape ah In fact, there is no So you can delete
Then, whether the bottom underline is selected or not is hidden
The brothers have just finished learning animation Is it possible to live a whole life
Tolerable
@Composable
fun SelectableIconButton(
icon: ImageVector,
iconContentDescription: Int,
onIconSelect: () -> Unit,
isSelected: Boolean,
modifier: Modifier = Modifier
) {
// Icons checked and unchecked The colors are different
val tint = if (isSelected) {
MaterialTheme.colors.primary
} else {
MaterialTheme.colors.onSurface.copy(alpha = 0.6f)
}
val enter = remember {
fadeIn(animationSpec = TweenSpec(300, easing = FastOutLinearInEasing))
}
val exit = remember {
fadeOut(animationSpec = TweenSpec(100, easing = FastOutSlowInEasing))
}
TextButton(onClick = onIconSelect, modifier = modifier) {
Column {
Icon(
imageVector = icon,
tint = tint,
contentDescription = stringResource(id = iconContentDescription)
)
// Minimum height 16dp
AnimatedVisibility(visible = isSelected, enter = enter, exit = exit) {
Box(
Modifier
.padding(top = 3.dp)
.width(icon.defaultWidth)
.height(1.dp)
.background(tint)
)
}
}
}
}
So every time you click, you will get feedback Users may prefer
Of course, you can optimize the animation that you have learned in the future
边栏推荐
- Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率
- 打新债需要具备什么条件 打新债安全吗
- How to avoid the "black swan" incident in the gene field: a security war behind a preventive "recall"
- NAACL 2022 Findings | 字节提出MTG:多语言文本生成数据集
- LeetCode 473. 火柴拼正方形
- Save: software analysis, verification and test platform
- JDBC 在性能测试中的应用
- Learn the basic principles of BLDC in Simulink during a meal
- [one by one series] identityserver4 (III) user name and password
- Advanced network accounting notes (VIII)
猜你喜欢

RStudio 1.4软件安装包和安装教程

Application de JDBC dans les essais de performance

Product design - Requirements Analysis

FlagAI飞智:AI基础模型开源项目,支持一键调用OPT等模型

开源 SPL 重新定义 OLAP Server

CV image classification

Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)

如何使用物联网低代码平台进行流程管理?

Game asset reuse: a new way to find required game assets faster

#20Set介绍与API
随机推荐
Application of JDBC in performance test
【One by One系列】IdentityServer4(七)授权码流程原理之MVC
Development notes of wedding studio applet based on wechat applet
Chaos engineering, learn about it
Advanced network accounting notes (6)
【One by One系列】IdentityServer4(二)使用Client Credentials保护API资源
Helix QAC is updated to 2022.1 and will continue to provide high standard compliance coverage
[one by one series] spa of identityserver4 (VI) authorization code process principle
QGIS import WMS or WMTs
Learn the basic principles of BLDC in Simulink during a meal
盘点四种WiFi加密标准:WEP、WPA、WPA2、WPA3
Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率
从零开发小程序和公众号【第二期】
Advanced network accounting notes (V)
Programmable data plane (paper reading)
在线文本实体抽取能力,助力应用解析海量文本数据
The yuan universe killer is coming! Xiao Zha offered 4 VR head displays to challenge the visual Turing test
墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库
Leetcode daily question - 30 Concatenate substrings of all words
Borui data attends Alibaba cloud observable technology summit, and digital experience management drives sustainable development