升级到 beta-01 后,用 Jetpack Compose 编写的键盘上的动画和效果损坏

时间:2021-03-02 22:29:29

标签: android android-jetpack-compose

在将完全使用 Jetpack Compose 编写的键盘从 Compose 版本 alpha-11 更新到 beta-01 后,我遇到了问题。

在升级之前,用户界面如您预期的那样运行良好。涟漪显示得很好。 升级后的动画和效果(如按下按钮)无法正确播放(波纹效果似乎卡住)。 看一看:

1

这是期望的行为以及版本升级前的样子:

2

注意:ComposeKeyboardView 之外的相同代码运行良好。此外,相同的代码在 Jetpack Compose alpha-11beta-01 之前工作得非常好。 我不确定这是错误还是我自己可以解决问题。 我感谢任何帮助或提示来恢复所需的行为。

您可以使用以下代码重现该问题:

键盘.kt

@Composable
fun Keyboard() {
    Column(
        Modifier
            .fillMaxWidth()
            .height(200.dp)
            .background(Color.Gray),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(color = Color.Black, text = "This should resemble a keyboard")
        Button(modifier = Modifier.width(250.dp),onClick = {  }) {
            Text(text = "A Button")
        }
    }
}

ComposeKeyboardView.kt

class ComposeKeyboardView constructor(
    context: Context,

    ) : AbstractComposeView(context) {

    @Composable
    override fun Content() {
        Keyboard()
       
    }
}

IMEService.kt


class IMEService : InputMethodService(), LifecycleOwner, ViewModelStoreOwner,
    SavedStateRegistryOwner {

    override fun onCreateInputView(): View {
        val view = ComposeKeyboardView(this)
        window!!.window!!.decorView.let { decorView ->
            ViewTreeLifecycleOwner.set(decorView, this)
            ViewTreeViewModelStoreOwner.set(decorView, this)
            ViewTreeSavedStateRegistryOwner.set(decorView, this)
        }
        return view
    }


    //Lifecycle Methods

    private var lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)

    override fun getLifecycle(): Lifecycle {
        return lifecycleRegistry
    }


    private fun handleLifecycleEvent(event: Lifecycle.Event) =
        lifecycleRegistry.handleLifecycleEvent(event)

    override fun onCreate() {
        super.onCreate()
        savedStateRegistry.performRestore(null)
        handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
    }



    override fun onDestroy() {
        super.onDestroy()
        handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    }


    //ViewModelStore Methods
    private val store = ViewModelStore()

    override fun getViewModelStore(): ViewModelStore = store

    //SaveStateRegestry Methods

    private val savedStateRegistry = SavedStateRegistryController.create(this)

    override fun getSavedStateRegistry(): SavedStateRegistry = savedStateRegistry.savedStateRegistry

不要忘记将 IMEService 添加到您的 AndroidManifest.xml

<application>
[...]
<service
            android:name=".IMEService"
            android:label="Example Comopose IME"

            android:permission="android.permission.BIND_INPUT_METHOD">
            <intent-filter>
                <action android:name="android.view.InputMethod" />
            </intent-filter>

            <meta-data
                android:name="android.view.im"
                android:resource="@xml/method" />
        </service>
</application>

app\build.gradle

[...]
dependencies {
    def compose_version = "1.0.0-beta01"  
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation "androidx.activity:activity-compose:1.3.0-alpha03"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'
}

编辑: compose-beta02 中的同样问题

Or you can find all code here and clone it directly

1 个答案:

答案 0 :(得分:2)

我下载了您的代码并在 Compose 的错误跟踪器中看到了 your comment。 看起来,如果您在 IMEService 类中执行以下操作,动画就会起作用。

override fun onCreate() {
    super.onCreate()
    savedStateRegistry.performRestore(null)
    handleLifecycleEvent(Lifecycle.Event.ON_RESUME) // << here
}

enter image description here