2017-10-03 2 views
3

버튼 바로 아래에 숨겨진 버튼이있는 간단한 프로그램을 만들려고합니다. 사용자가 첫 번째 버튼을 클릭하면 부드러운 애니메이션을 사용하여 두 번째 버튼이 나타납니다.Android Fade In, 페이드 아웃 애니메이션 문제

처음 시간 : 사용자가 다시 첫 번째 버튼을 클릭하면, 두 번째 버튼이 원활 내가 등장하고 사라지는 동작을 애니메이션하는 Alpha Animation를 사용하고 있지만, 나는 다음과 같은 문제로 실행하고 등, 사라 버튼을 클릭해도 아무 일도 일어나지 않습니다. 버튼을 다시 클릭하면 즉시 나타나고 사라집니다. 세 번째 버튼 버튼을 클릭하면 "페이드 인"동작이 부드럽게 움직입니다. 내가 그것을 네 번째를 클릭 할 때 그리고, 그것은 부드럽게 "페이드 아웃"운동, 등 ...

다음

enter image description here enter image description here

입니다 애니메이션이 간단한 프로그램의 코드 :

public class MainActivity extends AppCompatActivity { 
public Button toggleButton; 
public Button helloButton; 
public boolean isVisible = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    helloButton = (Button)findViewById(R.id.helloButton); 

    toggleButton = (Button)findViewById(R.id.toggleButton); 
    toggleButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (!isVisible) { 
       //helloButton.setVisibility(View.VISIBLE); 
       AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1); // start alpha, end alpha 
       fadeInAnimation.setDuration(250); // time for animation in milliseconds 
       fadeInAnimation.setFillAfter(true); // make the transformation persist 
       fadeInAnimation.setAnimationListener(new Animation.AnimationListener() { 
        @Override 
        public void onAnimationEnd(Animation animation) { 
         helloButton.setVisibility(View.VISIBLE); 
        } 

        @Override 
        public void onAnimationRepeat(Animation animation) { } 

        @Override 
        public void onAnimationStart(Animation animation) { 
         helloButton.setVisibility(View.GONE); 
        } 
       }); 
       helloButton.startAnimation(fadeInAnimation); 
       isVisible = true; 
       System.out.println("Button is invisible... Time to Fade IN"); 
      } 
      else { 
       //helloButton.setVisibility(View.GONE); 
       AlphaAnimation fadeOutAnimation = new AlphaAnimation(1, 0); // start alpha, end alpha 
       fadeOutAnimation.setDuration(250); // time for animation in milliseconds 
       fadeOutAnimation.setFillAfter(true); // make the transformation persist 
       fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() { 
        @Override 
        public void onAnimationEnd(Animation animation) { 
         helloButton.setVisibility(View.GONE); 
        } 

        @Override 
        public void onAnimationRepeat(Animation animation) { } 

        @Override 
        public void onAnimationStart(Animation animation) { 
         helloButton.setVisibility(View.VISIBLE); 
        } 
       }); 
       helloButton.startAnimation(fadeOutAnimation); 
       isVisible = false; 
       System.out.println("Button is visible... Time to Fade OUT"); 
      } 
     } 
    }); 

    } 
} 

매우 단순한 느낌입니다. 그러나 이것은 안드로이드 개발에서 애니메이션을 만난 첫 번째입니다. 어떤 제안이나 조언도 대단히 감사하겠습니다!

답변

1

변수가 isVisible 일 필요는 없습니다. 버튼이 표시되는지 확인하십시오.
먼저 fadeInAnimation을 보려면보기를 VISIBLE로 설정해야합니다.

helloButton = findViewById(R.id.helloButton); 
toggleButton = findViewById(R.id.toggleButton); 
toggleButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     if (helloButton.getVisibility() == View.GONE) { 
      AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1); 
      fadeInAnimation.setDuration(2500); 
      fadeInAnimation.setFillAfter(true); 
      fadeInAnimation.setAnimationListener(new Animation.AnimationListener() { 
       @Override 
       public void onAnimationEnd(Animation animation) { 
       } 

       @Override 
       public void onAnimationRepeat(Animation animation) { } 

       @Override 
       public void onAnimationStart(Animation animation) { 
        helloButton.setVisibility(View.VISIBLE); 
       } 
      }); 
      helloButton.startAnimation(fadeInAnimation); 
     } else { 
      AlphaAnimation fadeOutAnimation = new AlphaAnimation(1, 0); 
      fadeOutAnimation.setDuration(2500); 
      fadeOutAnimation.setFillAfter(true); 
      fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() { 
       @Override 
       public void onAnimationEnd(Animation animation) { 
        helloButton.setVisibility(View.GONE); 
       } 

       @Override 
       public void onAnimationRepeat(Animation animation) { } 

       @Override 
       public void onAnimationStart(Animation animation) { 
       } 
      }); 
      helloButton.startAnimation(fadeOutAnimation); 
     } 
    } 
}); 
:

또한이 코드를 확인, 모두 애니메이션의 지속 시간을 변경
관련 문제