微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android,菜单项展开/折叠动画

如何解决Android,菜单项展开/折叠动画

我已将自定义视图设置为 menuitem actionView:

<item
    android:id="@+id/search"
    android:icon="@drawable/ic_baseline_search_24"
    android:title="@string/search_title"
    app:actionViewClass="com.myapplication.SearchBox"
    app:showAsAction="collapseActionView|ifRoom" />

当我点击“搜索”项时,“搜索框”被添加到工具栏,这很好。 现在我想在我的动作视图展开和折叠时设置动画,我该如何实现?

更新

我尝试在 onAttachedToWindow 事件中使用动画,它似乎有效。

... in my SearchBox class ...

override fun onAttachedToWindow() {
    super.onAttachedToWindow()

    if (parent is Toolbar) {
        layoutParams.height = Toolbar.LayoutParams.MATCH_PARENT
        layoutParams.width = Toolbar.LayoutParams.MATCH_PARENT

        // Check if the runtime version is at least Lollipop
        // get the center for the clipping circle
        val cx = width
        val cy = height / 2

        x = 0f

        // get the final radius for the clipping circle
        val finalRadius = hypot(cx.todouble(),cy.todouble()).toFloat()

        // create the animator for this view (the start radius is zero)
        val anim = ViewAnimationUtils.createCircularReveal(this,cx,cy,0f,finalRadius)
        // make the view visible and start the animation
        visibility = View.VISIBLE
        anim.duration = 2000
        anim.start()
    }
}

这是正确的方法吗?

更新 2

实现接口 androidx.appcompat.view.CollapsibleActionView 允许我们从 onActionViewExpanded() 接收 onActionViewCollapsed()androidx.appcompat.widget.Toolbar。 不幸的是,这个接口已被弃用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。