Skip to content

Commit 1171789

Browse files
committed
improvements.
1 parent 5397b3a commit 1171789

4 files changed

Lines changed: 79 additions & 2 deletions

File tree

animatable-compose/src/main/java/com/commandiron/animatable_compose/AnimatableIcon.kt

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.commandiron.animatable_compose
22

3+
import androidx.annotation.FloatRange
34
import androidx.compose.foundation.layout.height
45
import androidx.compose.foundation.layout.offset
56
import androidx.compose.foundation.layout.width
67
import androidx.compose.material3.Icon
78
import androidx.compose.material3.LocalContentColor
89
import androidx.compose.runtime.Composable
910
import androidx.compose.ui.Modifier
11+
import androidx.compose.ui.draw.alpha
1012
import androidx.compose.ui.graphics.Color
1113
import androidx.compose.ui.graphics.vector.ImageVector
1214
import androidx.compose.ui.unit.Dp
1315
import androidx.compose.ui.unit.DpOffset
1416
import androidx.compose.ui.unit.DpSize
17+
import com.commandiron.animatable_compose.state.AnimatableState
1518
import com.commandiron.animatable_compose.state.AnimatableStateTag
1619
import com.commandiron.animatable_compose.state.SharedAnimatableState
1720

@@ -25,11 +28,13 @@ fun AnimatableIcon(
2528
stateIndex: Int = 0,
2629
fixedWidth: Dp = Dp.Unspecified,
2730
fixedHeight: Dp = Dp.Unspecified,
31+
@FloatRange(from = 0.0, to = 1.0)
32+
fixedAlpha: Float? = null,
2833
fixedOffset: DpOffset = DpOffset.Unspecified,
2934
) {
3035
val stateIn = state.getState(AnimatableStateTag.ICON, stateIndex) ?: throw (
3136
IllegalArgumentException("no animatableState has this index: $stateIndex")
32-
)
37+
)
3338
Icon(
3439
imageVector = imageVector,
3540
contentDescription = contentDescription,
@@ -62,6 +67,58 @@ fun AnimatableIcon(
6267
else -> fixedOffset.y
6368
}
6469
)
70+
.alpha(fixedAlpha ?: stateIn.animatedAlpha)
71+
.then(modifier),
72+
tint = tint
73+
)
74+
}
75+
76+
@Composable
77+
fun AnimatableIcon(
78+
imageVector: ImageVector,
79+
contentDescription: String?,
80+
modifier: Modifier = Modifier,
81+
tint: Color = LocalContentColor.current,
82+
state: AnimatableState,
83+
fixedWidth: Dp = Dp.Unspecified,
84+
fixedHeight: Dp = Dp.Unspecified,
85+
@FloatRange(from = 0.0, to = 1.0)
86+
fixedAlpha: Float? = null,
87+
fixedOffset: DpOffset = DpOffset.Unspecified,
88+
) {
89+
Icon(
90+
imageVector = imageVector,
91+
contentDescription = contentDescription,
92+
modifier = Modifier
93+
.width(
94+
when (fixedWidth) {
95+
Dp.Unspecified -> if (state.animatedSize == DpSize.Unspecified) {
96+
Dp.Unspecified
97+
} else state.animatedSize.width
98+
Dp.Infinity -> state.screenWidth
99+
else -> fixedWidth
100+
}
101+
)
102+
.height(
103+
when (fixedHeight) {
104+
Dp.Unspecified -> if (state.animatedSize == DpSize.Unspecified) {
105+
Dp.Unspecified
106+
} else state.animatedSize.height
107+
Dp.Infinity -> state.screenHeight
108+
else -> fixedHeight
109+
}
110+
)
111+
.offset(
112+
x = when (fixedOffset) {
113+
DpOffset.Unspecified -> state.animatedOffset.x
114+
else -> fixedOffset.x
115+
},
116+
y = when (fixedOffset) {
117+
DpOffset.Unspecified -> state.animatedOffset.y
118+
else -> fixedOffset.y
119+
}
120+
)
121+
.alpha(fixedAlpha ?: state.animatedAlpha)
65122
.then(modifier),
66123
tint = tint
67124
)

animatable-compose/src/main/java/com/commandiron/animatable_compose/AnimatableText.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.commandiron.animatable_compose
22

3+
import androidx.annotation.FloatRange
34
import androidx.compose.foundation.layout.offset
45
import androidx.compose.material3.LocalTextStyle
56
import androidx.compose.material3.Text
67
import androidx.compose.runtime.Composable
78
import androidx.compose.ui.Modifier
9+
import androidx.compose.ui.draw.alpha
810
import androidx.compose.ui.graphics.Color
911
import androidx.compose.ui.text.TextLayoutResult
1012
import androidx.compose.ui.text.TextStyle
@@ -39,6 +41,8 @@ fun AnimatableText(
3941
style: TextStyle = LocalTextStyle.current,
4042
state: SharedAnimatableState,
4143
stateIndex: Int = 0,
44+
@FloatRange(from = 0.0, to = 1.0)
45+
fixedAlpha: Float? = null,
4246
fixedOffset: DpOffset = DpOffset.Unspecified,
4347
) {
4448
val stateIn = state.getState(AnimatableStateTag.TEXT, stateIndex) ?: throw (
@@ -57,6 +61,7 @@ fun AnimatableText(
5761
else -> fixedOffset.y
5862
}
5963
)
64+
.alpha(fixedAlpha ?: stateIn.animatedAlpha)
6065
.then(modifier),
6166
color = color,
6267
fontSize = stateIn.animatedFontSize,
@@ -93,6 +98,8 @@ fun AnimatableText(
9398
onTextLayout: (TextLayoutResult) -> Unit = {},
9499
style: TextStyle = LocalTextStyle.current,
95100
state: AnimatableState,
101+
@FloatRange(from = 0.0, to = 1.0)
102+
fixedAlpha: Float? = null,
96103
fixedOffset: DpOffset = DpOffset.Unspecified,
97104
) {
98105
Text(
@@ -108,6 +115,7 @@ fun AnimatableText(
108115
else -> fixedOffset.y
109116
}
110117
)
118+
.alpha(fixedAlpha ?: state.animatedAlpha)
111119
.then(modifier),
112120
color = color,
113121
fontSize = state.animatedFontSize,

animatable-compose/src/main/java/com/commandiron/animatable_compose/state/AnimatableState.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ fun rememberAnimatableIconState(
181181
toTargetSizeAnimationSpec: AnimationSpec<Size> = tween(500),
182182
toInitialSizeAnimationSpec: AnimationSpec<Size> = tween(500),
183183
onSizeAnimation: (AnimationState) -> Unit = {},
184+
initialAlpha: Float? = null,
185+
targetAlpha: Float? = null,
186+
toTargetAlphaAnimationSpec: AnimationSpec<Float> = tween(500),
187+
toInitialAlphaAnimationSpec: AnimationSpec<Float> = tween(500),
188+
onAlphaAnimation: (AnimationState) -> Unit = {},
184189
initialOffset: DpOffset? = null,
185190
targetOffset: DpOffset? = null,
186191
toTargetOffsetAnimationSpec: AnimationSpec<Size> = tween(500),
@@ -195,6 +200,11 @@ fun rememberAnimatableIconState(
195200
targetSize = targetSize,
196201
toTargetSizeAnimationSpec = toTargetSizeAnimationSpec,
197202
toInitialSizeAnimationSpec = toInitialSizeAnimationSpec,
203+
initialAlpha = initialAlpha,
204+
targetAlpha = targetAlpha,
205+
toTargetAlphaAnimationSpec = toTargetAlphaAnimationSpec,
206+
toInitialAlphaAnimationSpec = toInitialAlphaAnimationSpec,
207+
onAlphaAnimation = onAlphaAnimation,
198208
onSizeAnimation = onSizeAnimation,
199209
initialOffset = initialOffset,
200210
targetOffset = targetOffset,

app/src/main/java/com/commandiron/animatablecompose/Show4.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ fun Show4() {
3131
initialShape = RoundedCornerShape(32.dp),
3232
targetShape = RoundedCornerShape(0.dp),
3333
toTargetShapeAnimationSpec = tween(500, 500),
34-
toTargetAlphaAnimationSpec = tween(500, 500),
3534
initialOffset = DpOffset(0.dp, 0.dp),
3635
targetOffset = DpOffset(0.dp, - Dp.Infinity),
3736
toInitialOffsetAnimationSpec = tween(500, 500),
@@ -40,6 +39,9 @@ fun Show4() {
4039
initialSize = DpSize(40.dp, 40.dp),
4140
targetSize = DpSize(80.dp, 80.dp),
4241
toTargetSizeAnimationSpec = tween(500,500),
42+
initialAlpha = 0.3f,
43+
targetAlpha = 1f,
44+
toTargetAlphaAnimationSpec = tween(5000, 500),
4345
initialOffset = DpOffset(0.dp, 0.dp),
4446
targetOffset = DpOffset((-50).dp, 0.dp),
4547
toTargetOffsetAnimationSpec = tween(500, 500)

0 commit comments

Comments
 (0)