Coder Social home page Coder Social logo

Comments (2)

eliasbellido avatar eliasbellido commented on May 15, 2024

I just came across with this also. That's because during rotation somehow the width and height are 0. I haven't tried any solution yet, but just in case did you already?

from material-components-android-examples.

DaireJN avatar DaireJN commented on May 15, 2024

Hi I Have modified the extension functions to use view tree observer to ensure the layout is ready before operating on it try, it out.

fun BottomNavigationView.show() {
    if (visibility == VISIBLE) return
    viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            viewTreeObserver.removeOnGlobalLayoutListener(this)
            val parent = parent as ViewGroup
            // View needs to be laid out to create a snapshot & know position to animate. If view isn't
            // laid out yet, need to do this manually.
            if (!isLaidOut) {
                measure(
                    MeasureSpec.makeMeasureSpec(parent.width, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(parent.height, MeasureSpec.AT_MOST)
                )
                layout(parent.left, parent.height - measuredHeight, parent.right, parent.height)
            }

            val drawable = BitmapDrawable(context.resources, drawToBitmap())
            drawable.setBounds(left, parent.height, right, parent.height + height)
            parent.overlay.add(drawable)
            ValueAnimator.ofInt(parent.height, top).apply {
                startDelay = 100L
                duration = 300L
                interpolator = AnimationUtils.loadInterpolator(
                    context,
                    android.R.interpolator.linear_out_slow_in
                )
                addUpdateListener {
                    val newTop = it.animatedValue as Int
                    drawable.setBounds(left, newTop, right, newTop + height)
                }
                doOnEnd {
                    parent.overlay.remove(drawable)
                    visibility = VISIBLE
                }
                start()
            }
        }
    })
}
fun BottomNavigationView.hide() {
    if (visibility == GONE) return
    viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            viewTreeObserver.removeOnGlobalLayoutListener(this)
            val drawable = BitmapDrawable(context.resources, drawToBitmap())
            val parent = parent as ViewGroup
            drawable.setBounds(left, top, right, bottom)
            parent.overlay.add(drawable)
            visibility = GONE
            ValueAnimator.ofInt(top, parent.height).apply {
                startDelay = 100L
                duration = 200L
                interpolator = AnimationUtils.loadInterpolator(
                    context,
                    android.R.interpolator.fast_out_linear_in
                )
                addUpdateListener {
                    val newTop = it.animatedValue as Int
                    drawable.setBounds(left, newTop, right, newTop + height)
                }
                doOnEnd {
                    parent.overlay.remove(drawable)
                }
                start()
            }
        }
    })
}

from material-components-android-examples.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.