use*_*924 1 android android-animation android-jetpack-compose jetpack-compose-animation android-jetpack-compose-animation
初始状态看起来像这样 ( animatedOffset = 0f)
我想得到1f反向梯度:
当前代码:
val transition = rememberInfiniteTransition(label = "NavIconGradientAnim")
val animatedOffset by transition.animateFloat(
initialValue = 0f, targetValue = 1f,
label = "NavIconGradientAnimOffset",
animationSpec = infiniteRepeatable(
animation = tween(1500, easing = LinearEasing),
repeatMode = RepeatMode.Reverse
)
)
Image(
painterResource(id = item.icon),
contentDescription = null,
modifier = Modifier.drawWithCache {
onDrawWithContent {
with(drawContext.canvas.nativeCanvas) {
val angle = 45f
val endX = drawContext.size.width
val endY = (endX * kotlin.math.tan(Math.toRadians(angle.toDouble()))).toFloat()
val checkPoint = saveLayer(null, null)
drawContent()
val gradient = Brush.linearGradient(
colors = listOf(Color.Blue, Color.Yellow),
start = Offset(animatedOffset * endX, animatedOffset * endY),
end = Offset(endX, endX)
)
drawRect(
brush = gradient,
blendMode = BlendMode.SrcIn
)
restoreToCount(checkPoint)
}
}
}
)
Run Code Online (Sandbox Code Playgroud)
我尝试设置
end = Offset(endX - animatedOffset * endX, endY - animatedOffset * endY),
但看起来不太好(GIF 预览):
它突然改变颜色
有多种方法可以做到这一点,并且您不必计算正切。
一种方法是
val limit = 1.5f
val transition = rememberInfiniteTransition(label = "shimmer")
val progressAnimated by transition.animateFloat(
initialValue = -limit,
targetValue = limit,
animationSpec = infiniteRepeatable(
animation = tween(1500, easing = LinearEasing),
repeatMode = RepeatMode.Reverse
), label = "shimmer"
)
Run Code Online (Sandbox Code Playgroud)
并设置刷子
val 宽度 = size.width val 高度 = size.height
val offset = width * progress
val gradientWidth = width
val brush = Brush.linearGradient(
colors = gradientColors,
start = Offset(offset, 0f),
end = Offset(offset + gradientWidth, height)
)
Run Code Online (Sandbox Code Playgroud)
结果
演示
@Preview
@Composable
private fun SingleGradientTest() {
Column(
Modifier.padding(20.dp)
) {
val painterStar = painterResource(id = R.drawable.star_foreground)
val limit = 1.5f
var progress by remember {
mutableStateOf(-limit)
}
val transition = rememberInfiniteTransition(label = "shimmer")
val progressAnimated by transition.animateFloat(
initialValue = -limit,
targetValue = limit,
animationSpec = infiniteRepeatable(
animation = tween(1500, easing = LinearEasing),
repeatMode = RepeatMode.Reverse
), label = "shimmer"
)
Box(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(1f)
.drawWithCache {
val width = size.width
val height = size.height
val offset = width * progress
val gradientWidth = width
val brush = Brush.linearGradient(
colors = gradientColors,
start = Offset(offset, 0f),
end = Offset(offset + gradientWidth, height)
)
onDrawBehind {
drawRect(
brush = brush,
blendMode = BlendMode.SrcIn
)
}
}
)
Box(
modifier = Modifier
.size(100.dp)
.graphicsLayer {
compositingStrategy = CompositingStrategy.Offscreen
}
.drawWithCache {
val width = size.width
val height = size.height
val offset = width * progress
val gradientWidth = width
val brush = Brush.linearGradient(
colors = gradientColors,
start = Offset(offset, 0f),
end = Offset(offset + gradientWidth, height)
)
onDrawBehind {
// Destination
with(painterStar) {
draw(
size = Size(width, width)
)
}
// Source
drawRect(
brush = brush,
blendMode = BlendMode.SrcIn
)
}
}
)
Text("Progress: $progress")
Slider(
value = progress,
onValueChange = { progress = it },
valueRange = -limit..limit
)
Text(text = "Animated progress: $progressAnimated")
Box(
modifier = Modifier
.size(100.dp)
.graphicsLayer {
compositingStrategy = CompositingStrategy.Offscreen
}
.drawWithCache {
val width = size.width
val height = size.height
val offset = width * progressAnimated
val gradientWidth = width
val brush = Brush.linearGradient(
colors = gradientColors,
start = Offset(offset, 0f),
end = Offset(offset + gradientWidth, height)
)
onDrawBehind {
// Destination
with(painterStar) {
draw(
size = Size(width, width)
)
}
// Source
drawRect(
brush = brush,
blendMode = BlendMode.SrcIn
)
}
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1120 次 |
| 最近记录: |