android - 无法在分离 View 上启动动画师

标签 android kotlin

我已经在 Kotlin 中创建了扩展 PopupView 的类,并尝试使用 CircleReveal 库为其显示动画。这是我类(class)的功能

fun show(root: View) {
    showAtLocation(root, Gravity.CENTER, 0, 0)

    val cx = (mainView.left + mainView.right) / 2
    val cy = (mainView.top + mainView.bottom) / 2
    val dx = Math.max(cx, mainView.width - cx)
    val dy = Math.max(cy, mainView.height - cy)
    val finalRadius = Math.hypot(dx.toDouble(), dy.toDouble()).toFloat()

    with (ViewAnimationUtils.createCircularReveal(mainView, cx, cy, 0f, finalRadius)) {
        interpolator = AccelerateDecelerateInterpolator()
        duration = 1500
        start()
    }
}

该代码给我以下错误

java.lang.IllegalStateException: Cannot start this animator on a detached view!
                                                            at android.view.RenderNode.addAnimator(RenderNode.java:817)
                                                            at android.view.RenderNodeAnimator.setTarget(RenderNodeAnimator.java:300)
                                                            at android.view.RenderNodeAnimator.setTarget(RenderNodeAnimator.java:282)
                                                            at android.animation.RevealAnimator.<init>(RevealAnimator.java:37)
                                                            at android.view.ViewAnimationUtils.createCircularReveal(ViewAnimationUtils.java:53)
                                                            at io.codetail.animation.ViewAnimationUtils.createCircularReveal(ViewAnimationUtils.java:74)
                                                            at io.codetail.animation.ViewAnimationUtils.createCircularReveal(ViewAnimationUtils.java:39)

类初始化

class MemberMenu(ctx: Context, val member: Member): PopupWindow(ctx), View.OnClickListener {
val mainView: View

init {
    contentView = LayoutInflater.from(ctx).inflate(R.layout.member_menu_layout, null)
    mainView = contentView.findViewById(R.id.member_menu_view)
    val size = Helpers.dipToPixels(ctx, 240f)
    width = size; height = size
    setBackgroundDrawable(ColorDrawable())
    isOutsideTouchable = true
    isTouchable = true
}
.......

最佳答案

不确定,如果它是正确的解决方案,但我只是将该代码移至 OnAttachStateChangeListener

    fun show(root: View) {
        showAtLocation(root, Gravity.CENTER, 0, 0)
        backgroundView.addOnAttachStateChangeListener(this)
    }

    override fun onViewAttachedToWindow(v: View?) {
        if (v==null) return

        with(ViewAnimationUtils.createCircularReveal(v, 500, 500, 0f, 500f)) {
            interpolator = AccelerateDecelerateInterpolator()
            duration = 2500
            start()
        }
    } override fun onViewDetachedFromWindow(v: View?) {}

关于android - 无法在分离 View 上启动动画师,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39817101/

相关文章:

android - moshi 自定义 JsonAdapter 跳过坏元素的问题

android - 如何使用Kotlin在 Volley 请求中添加自定义 header

android - 使用Android Studio时如何获取Android库的源代码?

android - 如何以编程方式刷新线性布局

collections - 从列表中的每个对象中选择属性

java - 在 Kotlin 中显式转换 Android 的 RecyclerView 的 ViewHolder 时出错 : 'This cast can never succeed'

java - Telephonymanager.EXTRA_INCOMING_NUMBER 在 API 级别 29 中已弃用

java - 重新创建 fragment 后恢复 UI 状态

android - Volley POST 请求 Android token

android - Google Cloud Messaging 适用于发布/订阅位置系统