androidandroid-animationandroid-transitionstranslate-animation

View blinks after animation


I have problem with any type of animations. I want to make material banner behavior, but with other animations. Actually I got the result, but the problem is that view is blinking after the animation. My code:

First example:

val anim = TranslateAnimation(1f, 1f, 1f, 0f)
anim.duration = 300
banner.startAnimation(anim)
banner.visibility = View.INVISIBLE

Second example

val mTransition = Slide(Gravity.END)
mTransition.setDuration(300)
mTransition.addTarget(banner)

TransitionManager.beginDelayedTransition(banner, mTransition)
banner.setVisibility(View.GONE)

Can someone explain how to avoid blinking of the view and why it is happening.


Solution

  • I solved problem of blinking of the view by animating it on other way. I used following strategy. First of all I used Guideline component of the ConstraintLayout. I constraint my banner to the top of it and place parameter layout_constraintGuide_begin = "0dp". After that I used ValueAnimator in order to get animated value for my Guideline and changed the guidebegin params of it(see the code).

            val params: ConstraintLayout.LayoutParams = guideline2.layoutParams as ConstraintLayout.LayoutParams
    
            animBanner = ValueAnimator.ofInt(0, banner.height + toolbar.height)
    
            animBanner!!.addUpdateListener {
                params.guideBegin = it.getAnimatedValue() as Int
                guideline2.layoutParams = params
            } 
    

    This is the declaration of animation. At the end it is enough to use animBanner.start() for starting the animation and animBanner.reverse() for reverse animation (hiding banner).