java - 从 Java Activity 类调用自定义 View 的 Kotlin 类中的动画

标签 java android kotlin android-animation android-custom-view

我有一个 Java Activity 类,其中包含一个用 Kotlin 编写的自定义 View

@BindView(R.id.icon)
Icon icon;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    icon.showIcon();
}

和自定义 View 类:

fun showIcon() {
    visibility = View.VISIBLE
    rotate()
}

fun rotate() {
    ValueAnimator.ofFloat(0f, 360f).apply {
        interpolator = AccelerateDecelerateInterpolator()
        startDelay = 1000
        duration = 1400
        addUpdateListener {
            val value = it.animatedValue as Float
            squatIV.rotation = value
            Timber.d(iconIV.rotation.toString())
        }
        start()
    }

绑定(bind)工作正常,我可以在创建时看到 Activity 上的图标。甚至动画也会运行,因为它记录了组件 ImageView 的旋转值。但是在屏幕上,动画没有播放。

这有什么具体原因吗?你遇到过这个问题吗?

更新:下面你可以看到虚拟 Activity 页面,自定义 View 需要动画旋转。经过一些后端检查后,自定义 View 变为可见并触发旋转动画。

dummy page

最佳答案

试试这个解决方案:

fun rotate() {
    ValueAnimator.ofFloat(0f, 360f).apply {
        interpolator = AccelerateDecelerateInterpolator()
        startDelay = 1000
        duration = 1400
        addUpdateListener {
            val value = it.animatedValue as Float
            squatIV.rotation = value
            Timber.d(iconIV.rotation.toString())

            invalidate() // must be!
            requestLayout() // check if works without it
        }
        start()
    }

关于java - 从 Java Activity 类调用自定义 View 的 Kotlin 类中的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59321608/

相关文章:

java - 为什么Java静态方法可以调用构造函数,但不能引用this?

Java Swing 更新 JList

java - 如何在后台继续收听 Android 上的推送通知

android - 在Flutter中设置蓝牙设备名称

android - 如何拦截 Xamarin.Android 中的硬件键盘按钮?

带有 kotlin 0.12.213 的 AndroidStudio ExternalSystemException

android-studio - 插件 'org.jetbrains.kotlin' 初始化失败,将被禁用。请重启 Android Studio

java - AspectJ "around"和 "proceed"与 "before/after"

安卓 - TransactionTooLargeException

android - Kotlin - Recycleview ViewHolder OnClick 不工作