前言
目前在Jetpack Compose中不支持修改光标的宽度和动画,但是通过反射可以修改「光标」的宽度; 另外,要修改动画的话,需要利用「双亲委派」机制提前拦截动画类的加载。
修改光标宽度
// 动态修改输入框的宽度
val textFieldCursorKt = Class.forName("androidx.compose.foundation.text.TextFieldCursorKt")
val cursorWidth = textFieldCursorKt.getDeclaredField("DefaultCursorThickness")
cursorWidth.isAccessible = true
cursorWidth.set(null, 1.dp.value)
修改动画
@Stable
fun <T> infiniteRepeatable(
animation: DurationBasedAnimationSpec<T>,
repeatMode: RepeatMode = RepeatMode.Restart,
initialStartOffset: StartOffset = StartOffset(0)
): InfiniteRepeatableSpec<T> {
if (animation is KeyframesSpec) {
/*特征搜索*/
if (animation.config.durationMillis == 1000
&& animation.config.keyframes.size == 4
&& animation.config.keyframes[0]?.value == 1f
&& repeatMode == RepeatMode.Restart
) {
return hookedInfiniteRepeatable(animation, repeatMode, initialStartOffset)
}
}
return InfiniteRepeatableSpec(animation, repeatMode, initialStartOffset)
}