user2524597
user2524597

Reputation: 129

Android ObjectAnimator Flicker on Second Animation

I have a view (sendSidePanel) which I hide when the view is rendered:

public void setListnerToRootView(){
   final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content); 
   final RelativeLayout sendSlidePanelFinal = (RelativeLayout)findViewById(R.id.sendSlidePanel);
   final ViewTreeObserver observer = activityRootView.getViewTreeObserver();
   observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
       @Override
       public void onGlobalLayout() {
           ViewHelper.setTranslationY(sendSlidePanel, sendSlidePanelFinal.getHeight());
       }
   });
}

I them use ObjectAnimator to animate the View up and down upon button press:

if(panelDown){
    ObjectAnimator anim = ObjectAnimator.ofFloat(sendSlidePanel, "translationY", 0);
    panelDown=false;
}
else{
    ObjectAnimator anim = ObjectAnimator.ofFloat(sendSlidePanel, "translationY", chatPanel.getHeight());
    panelDown=true;
 }

Problem is, when i want to animate the panel down (aka panelDown==false), there will be a slight flicker of the view (sendSlidePanel) before it animates down.

I did try, however, not hiding the view at first, and animating it down on button click. The flicker doesn't happen in this case. It appears that only after an initial animation that the flicker happens.

I have even tried, but to no avail:

public void onAnimationEnd(Animator arg0) {
    //Attempt to get rid of flicker
    sendSlidePanel.clearAnimation();
    sendSlidePanel.requestLayout();                 
}

And even this, but to no avail: Android Animation Flicker

Please help, thanks!

Upvotes: 1

Views: 1187

Answers (1)

Nikola Despotoski
Nikola Despotoski

Reputation: 50538

Try using AnimatorUpdateListener to update view's y like:

anim.addUpdateListener(mUpdateListener);

private AnimatorUpdateListener mUpdateListener = new AnimatorUpdateListener(){

        @Override
        public void onAnimationUpdate(ValueAnimator arg0) {
            Float animatedValue = (Float)arg0.getAnimatedValue();
            ViewHelper.setY(mView,animatedValue.floatValue());
        }

    };

Upvotes: 1

Related Questions