Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View.setVisibility(View.INVISIBLE) does not work for animated view

Tags:

android

I have an activity that (when started) shows some coachmark views (All ImageButtons) on screen that indicate how the app should be used (function of certain buttons, swipe behaviour, etc). A fade out animation is associated with each of these views that triggers after some predefined interval. This works as expected. However, I would like those marks to disappear earlier if the user interacts with the activity in a certain way. When these actions are triggered I cancel the animations and callsetVisibility(View.INVISIBLE); on the coachmark views. However, the visibility of the view does not change. I have experimented with other techniques - removing the view from the parent and setting alpha to 0 and these work fine but altering view visibility does nothing.

The code that sets up the coachmark looks as follows:

private void animateCoachmark(int id) {
    AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
    final View view = findViewById(id);
    animation.setStartOffset(10000);
    animation.setDuration(500);
    animation.setAnimationListener(new AnimationListenerBase(null) {
      @Override
      public void onAnimationEnd(Animation animation) {
        view.setVisibility(View.INVISIBLE);
      }
    });
    view.startAnimation(animation);
    coachmarkViews.add(view);
  }

The problematic code to change visiblity:

for (final View coachmarkView : coachmarkViews) {
  Animation animation = coachmarkView.getAnimation();
  if (animation != null) {
    animation.cancel();
  }
  coachmarkView.setVisibility(View.INVISIBLE);
}
like image 698
pookzilla Avatar asked Dec 06 '22 01:12

pookzilla


1 Answers

This issue seems to be that changes to visibility are not honoured as long as an animation is associated with a view even if the animation has been cancelled. Altering my cleanup code to first set the animation on the view to null allows the view to disappear as expected.

for (final View coachmarkView : coachmarkViews) {
  Animation animation = coachmarkView.getAnimation();
  if (animation != null) {
    animation.cancel();
    coachmarkView.setAnimation(null);
  }
  coachmarkView.setVisibility(View.INVISIBLE);
}
like image 155
pookzilla Avatar answered Dec 10 '22 13:12

pookzilla