当前位置:网站首页>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
边栏推荐
- Convex optimization notes
- User analysis aarrr model (pirate model)
- Game asset reuse: a new way to find required game assets faster
- Naacl 2022 finds | byte proposes MTG: multilingual text generation data set
- Uniswap founder: no independent token will be issued for genie, and Genie products will be integrated into the uniswap interface
- qgis导入WMS OR WMTS
- 80% of people will be wrong about the three counter intuitive questions?
- Development of block hash quiz game system (DAPP)
- Save: software analysis, verification and test platform
- Comparison and evaluation of digicert and globalsign single domain ov SSL certificates
猜你喜欢
随机推荐
如何避免基因领域“黑天鹅”事件:一场预防性“召回”背后的安全保卫战
Advanced network planning notes (IX)
【云动向】华为云云商店品牌全新发布 4大亮点都在这儿
Tutorial on installing SSL certificates in Microsoft Exchange Server 2007
FlagAI飞智:AI基础模型开源项目,支持一键调用OPT等模型
游戏资产复用:更快找到所需游戏资产的新方法
Hotline salon issue 26 - cloud security session
打新债好不好 打新债安全吗
LeetCode 473. 火柴拼正方形
Netseer: stream event telemetry notes for programmable data plane
手把手写深度学习(15):在Hugging Face上构建自己的语料库
基于SSM实现微博系统
深入理解和把握数字经济的基本特征
Advanced network accounting notes (IV)
JDBC 在性能測試中的應用
Principles of microcomputer Chapter 5 notes arrangement
ElastricSearch第二弹之分片原理
Timertasks notes
官宣.NET 7 预览版5
Uniswap founder: no independent token will be issued for genie, and Genie products will be integrated into the uniswap interface








