尝试打开自定义对话框时,ViewAnimationUtils.createCircularReveal崩溃

时间:2019-07-16 15:17:52

标签: android view crash dialog android-animation

我试图显示一个涵盖所有屏幕的对话框,但以循环模式显示。

此刻我有:

class FilterMap constructor(
     context: Context,
    var a: Boolean,
    var b: Boolean,
    var c: Boolean) : Dialog(context, R.style.DialogCustomStyle) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.custom_map_filter)
        animateShowView()
    }

    private fun animateShowView() {
        map_filter_root.visibility = View.VISIBLE
        val centerX = map_filter_root.width / 2
        val allX = map_filter_root.width
        val allY = map_filter_root.height

        val startRadius = 0
        val endRadius = Math.hypot(allX.toDouble(), allY.toDouble()).toInt()
        val animator = ViewAnimationUtils.createCircularReveal(map_filter_root, centerX, 0, startRadius.toFloat(), endRadius.toFloat())
        animator.start()
    }

}

视图是ConstraintLayout

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map_filter_root"
    android:visibility="invisible">
    ....
</androidx.constraintlayout.widget.ConstraintLayout>

自定义样式:

<style name="DialogCustomStyle">
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowBackground">@color/transparent</item>
    </style>

但是当我尝试像这样打开对话框时。

val filterMapsDialog = FilterMap(context!!, a, b, c)
filterMapsDialog.setCanceledOnTouchOutside(true)
filterMapsDialog.show()

应用程序崩溃并引发以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.awsomeapp.debug, PID: 23729
    java.lang.IllegalStateException: Cannot start this animator on a detached view!
        at android.view.RenderNode.addAnimator(RenderNode.java:824)
        at android.view.RenderNodeAnimator.setTarget(RenderNodeAnimator.java:296)
        at android.view.RenderNodeAnimator.setTarget(RenderNodeAnimator.java:281)
        at android.animation.RevealAnimator.<init>(RevealAnimator.java:37)
        at android.view.ViewAnimationUtils.createCircularReveal(ViewAnimationUtils.java:71)
        at com.awsomeapp.dialog.FilterMap.animateShowView(FilterMap.kt:32)
        at com.awsomeapp.dialog.FilterMap.onCreate(FilterMap.kt:21)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:550)
        at android.app.Dialog.show(Dialog.java:391)
        at com.awsomeapp.map.MapFragment$setupFilterButtonUI$1.onClick(MapFragment.kt:600)
        at android.view.View.performClick(View.java:7333)
        at android.view.View.performClickInternal(View.java:7299)
        at android.view.View.access$3200(View.java:846)
        at android.view.View$PerformClick.run(View.java:27773)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:6986)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

如果我尝试不调用animation方法,则效果很好。

所以..目前我不知道如何解决崩溃。

1 个答案:

答案 0 :(得分:0)

最可能的原因是:val animator = ViewAnimationUtils.createCircularReveal(map_filter_root, centerX, 0, startRadius.toFloat(), endRadius.toFloat())是在实际设置布局之前执行的。

尝试将您的onCreate更改为以下内容:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.custom_map_filter)
        map_filter_root.post { animateShowView() }        
    }